-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nats): implements NatsParser and test cases (#47)
Similarly to the MqttParser, to get the IP address out of the NATS request, it has to be sent over with the payload. Otherwise, even more straightforward parser than the Mqtt one as Nats has a function to get the subject, because it accetps wildcards. fix #19
- Loading branch information
Showing
10 changed files
with
181 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
# `@ogma/platform-nats` | ||
|
||
> TODO: description | ||
The `NatsParser` parser for the `OgmaInterceptor`. This plugin class parses NATS request and response object to be able to successfully log the data about the request. For more information, check out [the @ogma/nestjs-module](../nestjs-module/README.md) documentation. | ||
|
||
## Installation | ||
|
||
Nothing special, standard `npm i @ogma/platform-nats` or `yarn add @ogma/platform-nats` | ||
|
||
## Usage | ||
|
||
``` | ||
const platformNats = require('@ogma/platform-nats'); | ||
This plugin is to be used in the `OgmaInterceptorOptions` portion of the `OgmaModule` during `forRoot` or `forRootAsync` registration. It can be used like so: | ||
|
||
// TODO: DEMONSTRATE API | ||
```ts | ||
@Module( | ||
OgmaModule.forRoot({ | ||
interceptor: { | ||
rpc: NatsParser | ||
} | ||
}) | ||
) | ||
export class AppModule {} | ||
``` | ||
|
||
## Important Notes | ||
|
||
Because of how NATS requests are sent and the data available in them, to get the IP address in the request log, an IP property must be sent in the payload. This is the only way to get the IP address. If an IP property is not sent, the interceptor will use an empty string. This is for the same reason as in the [@ogma/platform-mqtt](../platform-mqtt) docs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './nats-interceptor.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { AbstractInterceptorService } from '@ogma/nestjs-module'; | ||
|
||
@Injectable() | ||
export class NatsParser extends AbstractInterceptorService { | ||
getCallPoint(context: ExecutionContext) { | ||
const client = this.getClient(context); | ||
return client.getSubject(); | ||
} | ||
|
||
getCallerIp(context: ExecutionContext) { | ||
const data = this.getData(context); | ||
return data.ip || ''; | ||
} | ||
|
||
getMethod() { | ||
return 'NATS'; | ||
} | ||
|
||
getProtocol() { | ||
return 'nats'; | ||
} | ||
|
||
getStatus( | ||
context: ExecutionContext, | ||
inColor: boolean, | ||
error?: Error | ExecutionContext, | ||
): string { | ||
const status = error ? 500 : 200; | ||
return inColor ? this.wrapInColor(status) : status.toString(); | ||
} | ||
|
||
private getClient(context: ExecutionContext) { | ||
return context.switchToRpc().getContext(); | ||
} | ||
|
||
private getData(context: ExecutionContext): any { | ||
return context.switchToRpc().getData(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
packages/platform-nats/test/nats-interceptor.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { NatsParser } from '../src'; | ||
import { color } from '@ogma/logger'; | ||
|
||
describe('NatsParser', () => { | ||
let parser: NatsParser; | ||
|
||
beforeEach(async () => { | ||
const modRef = await Test.createTestingModule({ | ||
providers: [NatsParser], | ||
}).compile(); | ||
parser = modRef.get(NatsParser); | ||
}); | ||
describe('getCallPoint', () => { | ||
it('should get the subject from the client', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getContext: () => ({ | ||
getSubject: () => JSON.stringify({ cmd: 'message' }), | ||
}), | ||
}), | ||
}); | ||
expect(parser.getCallPoint(ctxMock)).toBe( | ||
JSON.stringify({ cmd: 'message' }), | ||
); | ||
}); | ||
}); | ||
describe('getCallerIp', () => { | ||
it('should return an ip from data', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getData: () => ({ | ||
ip: '127.0.0.1', | ||
}), | ||
}), | ||
}); | ||
expect(parser.getCallerIp(ctxMock)).toBe('127.0.0.1'); | ||
}); | ||
it('should return a blank string', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getData: () => ({}), | ||
}), | ||
}); | ||
expect(parser.getCallerIp(ctxMock)).toBe(''); | ||
}); | ||
}); | ||
describe('getMethod', () => { | ||
it('should return "NATS"', () => { | ||
expect(parser.getMethod()).toBe('NATS'); | ||
}); | ||
}); | ||
describe('getStatus', () => { | ||
it('should return a 200', () => { | ||
expect(parser.getStatus(createMock<ExecutionContext>(), false)).toBe( | ||
'200', | ||
); | ||
}); | ||
it('should return a 500', () => { | ||
expect( | ||
parser.getStatus(createMock<ExecutionContext>(), false, new Error()), | ||
).toBe('500'); | ||
}); | ||
it('should return a 200 in color', () => { | ||
expect(parser.getStatus(createMock<ExecutionContext>(), true)).toBe( | ||
color.green(200), | ||
); | ||
}); | ||
}); | ||
describe('getProtocol', () => { | ||
it('should return "nats"', () => { | ||
expect(parser.getProtocol()).toBe('nats'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters