Skip to content

Commit

Permalink
chore: nest-jayson을 별도 라이브러리로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungJu committed May 22, 2024
1 parent 5a5304d commit 21bb284
Show file tree
Hide file tree
Showing 23 changed files with 210 additions and 19 deletions.
7 changes: 3 additions & 4 deletions apps/server/src/exception-filter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DomainException } from './exception/domain-exception';
import { JsonRpcExceptionFilter } from './json-rpc/json-rpc.decorator';
import { ExceptionFilter } from './json-rpc/exceptions/exception-filter';
import { JsonRpcDomainException } from '../../../libs/types/src/exceptions/json-rpc-domain-exception';
import { JsonRpcDomainException } from '@vacgom/types';
import { ExceptionFilter, JsonRpcExceptionFilter } from 'nestjs-jayson';

@JsonRpcExceptionFilter(DomainException)
export class DomainExceptionFilter implements ExceptionFilter<DomainException> {
Expand All @@ -14,7 +13,7 @@ export class DomainExceptionFilter implements ExceptionFilter<DomainException> {
data: exception.data,
},
};

callback(error);
}
}
5 changes: 0 additions & 5 deletions apps/server/src/json-rpc/error.ts

This file was deleted.

Empty file removed apps/server/src/json-rpc/types.ts
Empty file.
67 changes: 67 additions & 0 deletions libs/nestjs-jayson/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"ignorePatterns": [
"**/*"
],
"plugins": [
"@nx"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {
"@nx/enforce-module-boundaries": [
"error",
{
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": [
{
"sourceTag": "*",
"onlyDependOnLibsWithTags": [
"*"
]
}
]
}
]
}
},
{
"files": [
"*.ts",
"*.tsx"
],
"extends": [
"plugin:@nx/typescript"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"extends": [
"plugin:@nx/javascript"
],
"rules": {}
},
{
"files": [
"*.spec.ts",
"*.spec.tsx",
"*.spec.js",
"*.spec.jsx"
],
"env": {
"jest": true
},
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions libs/nestjs-jayson/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nestjs-jayson

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test nestjs-jayson` to execute the unit tests via [Jest](https://jestjs.io).
13 changes: 13 additions & 0 deletions libs/nestjs-jayson/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable */
const nxPreset = require('@nx/jest/preset').default;

export default {
displayName: 'nestjs-jayson',
preset: nxPreset,
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: 'tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: './coverage/',
};
16 changes: 16 additions & 0 deletions libs/nestjs-jayson/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "nestjs-jayson",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/nestjs-jayson/src",
"projectType": "library",
"tags": [],
"targets": {
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/nestjs-jayson/jest.config.ts"
}
}
}
}
1 change: 1 addition & 0 deletions libs/nestjs-jayson/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib';
9 changes: 9 additions & 0 deletions libs/nestjs-jayson/src/lib/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class JsonRpcError<T> extends Error {
constructor(
public code: number,
public override message: string,
public data?: T
) {
super(message);
}
}
2 changes: 2 additions & 0 deletions libs/nestjs-jayson/src/lib/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './exception-filter';
export * from './exception-handler';
8 changes: 8 additions & 0 deletions libs/nestjs-jayson/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './error';
export * from './json-rpc.decorator';
export * from './json-rpc.module';
export * from './json-rpc.service';
export * from './json-rpc-middleware.interface';
export * from './middleware';
export * from './middleware-manager';
export * from './exceptions';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { DiscoveryModule } from '@nestjs/core';
import { JsonRpcService } from './json-rpc.service';

import { JsonRpcExceptionHandler } from './exceptions/exception-handler';
import { JsonRpcExceptionHandler } from './exceptions';
import { MiddlewareManager } from './middleware-manager';
import { JsonRpcMiddlewareInterface } from './json-rpc-middleware.interface';
import { JsonRpcServerMiddleware } from './middleware';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import {
RequestContext,
} from './json-rpc.decorator';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { ExceptionFilter } from './exceptions/exception-filter';
import { JsonRpcExceptionHandler } from './exceptions/exception-handler';
import { ExceptionFilter, JsonRpcExceptionHandler } from './exceptions';
import { JsonRpcMiddlewareInterface } from './json-rpc-middleware.interface';
import { AuthMiddleware } from '../password-reset/auth.middleware';
import { MiddlewareManager } from './middleware-manager';

@Injectable()
Expand Down Expand Up @@ -69,7 +67,7 @@ export class JsonRpcService implements OnModuleInit {
instance,
methodName,
`${controllerName}.${methodName}`,
[new AuthMiddleware()]
[]
);
this.logger.log(
`Registered method ${controllerName}.${methodName}`,
Expand Down Expand Up @@ -109,7 +107,7 @@ export class JsonRpcService implements OnModuleInit {
}

private buildParam(params: JsonRpcParam[], requestContext: RequestContext) {
const args = [];
const args: any[] = [];

params.forEach((param) => {
if (param.type === 'REQ') {
Expand Down Expand Up @@ -161,6 +159,8 @@ export class JsonRpcService implements OnModuleInit {
const next = () => {
if (ms.length > 0) {
const middleware = ms.pop();
if (!middleware) return resolve(true);

const instance = this.getMiddlewareInstance(middleware);

const promise = instance.use(context.req, callback, next);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export class JsonRpcServerMiddleware implements NestMiddleware {

const server = this.jsonService.getServer();

server.call(req.body, context, function (err, result) {
if (err) return res.send(err);
res.send(result || {});
server.call(req.body, context, function (err, result): void {
if (err) res.send(err);
else res.send(result || {});
});
}
}
43 changes: 43 additions & 0 deletions libs/nestjs-jayson/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"compileOnSave": false,
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": [
"es2020",
"dom"
],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": "."
},
"exclude": [
"node_modules",
"tmp"
]
}

16 changes: 16 additions & 0 deletions libs/nestjs-jayson/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"],
"target": "es2021",
"strictNullChecks": true,
"noImplicitAny": true,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
14 changes: 14 additions & 0 deletions libs/nestjs-jayson/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"baseUrl": ".",
"paths": {
"@vacgom/types": ["libs/types/src/index.ts"],
"@vacgom/vacapi-sdk": ["libs/vacapi-sdk/src/index.ts"]
"@vacgom/vacapi-sdk": ["libs/vacapi-sdk/src/index.ts"],
"nestjs-jayson": ["libs/nestjs-jayson/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit 21bb284

Please sign in to comment.