-
-
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(redis): implements the redis parser
- Loading branch information
1 parent
ec844ea
commit 5f0ca06
Showing
6 changed files
with
165 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './redis-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,38 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { PATTERN_METADATA } from '@nestjs/microservices/constants'; | ||
import { AbstractInterceptorService } from '@ogma/nestjs-module'; | ||
|
||
@Injectable() | ||
export class RedisParser extends AbstractInterceptorService { | ||
getCallPoint(context: ExecutionContext): string { | ||
return JSON.stringify( | ||
this.reflector.get(PATTERN_METADATA, context.getHandler()), | ||
); | ||
} | ||
|
||
getCallerIp(context: ExecutionContext): any { | ||
const data = this.getData(context); | ||
return data?.ip || ''; | ||
} | ||
|
||
getProtocol(): string { | ||
return 'redis'; | ||
} | ||
|
||
getMethod(): string { | ||
return 'REDIS'; | ||
} | ||
|
||
getStatus( | ||
context: ExecutionContext, | ||
inColor: boolean, | ||
error?: Error | ExecutionContext, | ||
): string { | ||
const status = error ? 500 : 200; | ||
return inColor ? this.wrapInColor(status) : status.toString(); | ||
} | ||
|
||
private getData(context: ExecutionContext): any { | ||
return context.switchToRpc().getData(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/platform-redis/test/redis-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,86 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { PATTERN_METADATA } from '@nestjs/microservices/constants'; | ||
import { Test } from '@nestjs/testing'; | ||
import { color } from '@ogma/logger'; | ||
import { RedisParser } from '../src'; | ||
|
||
describe('RedisParser', () => { | ||
let parser: RedisParser; | ||
let reflector: Reflector; | ||
|
||
beforeEach(async () => { | ||
const mod = await Test.createTestingModule({ | ||
providers: [ | ||
RedisParser, | ||
{ | ||
provide: Reflector, | ||
useValue: { | ||
get: jest.fn(() => 'message'), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
parser = mod.get(RedisParser); | ||
reflector = mod.get(Reflector); | ||
}); | ||
|
||
describe('getCallPoint', () => { | ||
it('should get the reflected message', () => { | ||
const funcMock = () => 'string'; | ||
const ctxMock = createMock<ExecutionContext>({ | ||
getHandler: () => funcMock(), | ||
}); | ||
expect(parser.getCallPoint(ctxMock)).toBe(JSON.stringify('message')); | ||
expect(reflector.get).toBeCalledWith(PATTERN_METADATA, funcMock()); | ||
}); | ||
}); | ||
describe('getCallerIp', () => { | ||
it('should return an empty string', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getData: () => ({}), | ||
}), | ||
}); | ||
expect(parser.getCallerIp(ctxMock)).toBe(''); | ||
}); | ||
it('should return the ip', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getData: () => ({ | ||
ip: '127.0.0.1', | ||
}), | ||
}), | ||
}); | ||
expect(parser.getCallerIp(ctxMock)).toBe('127.0.0.1'); | ||
}); | ||
}); | ||
describe('getProtocol', () => { | ||
it('should return "redis"', () => { | ||
expect(parser.getProtocol()).toBe('redis'); | ||
}); | ||
}); | ||
describe('getMethod', () => { | ||
it('should return REDIS', () => { | ||
expect(parser.getMethod()).toBe('REDIS'); | ||
}); | ||
}); | ||
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), | ||
); | ||
}); | ||
}); | ||
}); |
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