Skip to content

Commit

Permalink
feat(redis): implements the redis parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffminsungkim committed Jun 8, 2020
1 parent ec844ea commit 5f0ca06
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
"@types/ws": "^7.2.4",
"@typescript-eslint/eslint-plugin": "^2.15.0",
"@typescript-eslint/parser": "^2.15.0",
"amqplib": "^0.5.6",
"amqp-connection-manager": "^3.2.0",
"amqplib": "^0.5.6",
"apollo-server-express": "^2.11.0",
"apollo-server-fastify": "^2.11.0",
"bunyan": "^1.8.12",
Expand All @@ -94,14 +94,15 @@
"grpc": "^1.24.2",
"husky": "^4.2.3",
"jest": "^25.1.0",
"kafkajs": "^1.12.0",
"lerna": "^3.20.2",
"lint-staged": "^10.0.8",
"kafkajs": "^1.12.0",
"morgan": "^1.10.0",
"mqtt": "^4.0.1",
"nats": "^1.4.9",
"pino": "^6.2.0",
"prettier": "^2.0.1",
"redis": "^3.0.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.0",
"rxjs": "^6.0.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/platform-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"files": [
"lib"
],
"private": true,
"publishConfig": {
"access": "public"
},
Expand All @@ -27,7 +26,11 @@
"url": "git+https://github.com/jmcdo29/ogma.git"
},
"scripts": {
"test": "echo \"No tests to run\""
"prebuild": "rimraf lib",
"build": "tsc -p tsconfig.build.json",
"postbuild": "mv ./lib/src/* ./lib && rmdir lib/src",
"test": "jest",
"test:cov": "jest --coverage"
},
"bugs": {
"url": "https://github.com/jmcdo29/ogma/issues"
Expand Down
1 change: 1 addition & 0 deletions packages/platform-redis/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './redis-interceptor.service';
38 changes: 38 additions & 0 deletions packages/platform-redis/src/redis-interceptor.service.ts
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 packages/platform-redis/test/redis-interceptor.service.spec.ts
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),
);
});
});
});
32 changes: 32 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4784,6 +4784,11 @@ delegates@^1.0.0:
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=

denque@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/denque/-/denque-1.4.1.tgz#6744ff7641c148c3f8a69c307e51235c1f4a37cf"
integrity sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==

depd@^1.1.2, depd@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
Expand Down Expand Up @@ -10313,6 +10318,33 @@ redent@^2.0.0:
indent-string "^3.0.0"
strip-indent "^2.0.0"

redis-commands@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.5.0.tgz#80d2e20698fe688f227127ff9e5164a7dd17e785"
integrity sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==

redis-errors@^1.0.0, redis-errors@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad"
integrity sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=

redis-parser@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4"
integrity sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=
dependencies:
redis-errors "^1.0.0"

redis@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/redis/-/redis-3.0.2.tgz#bd47067b8a4a3e6a2e556e57f71cc82c7360150a"
integrity sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==
dependencies:
denque "^1.4.1"
redis-commands "^1.5.0"
redis-errors "^1.2.0"
redis-parser "^3.0.0"

reflect-metadata@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"
Expand Down

0 comments on commit 5f0ca06

Please sign in to comment.