From 1196cf7001597a1c9d82490d51967b72f00124d5 Mon Sep 17 00:00:00 2001 From: Shinwoo PARK Date: Fri, 21 Mar 2025 09:04:47 +0900 Subject: [PATCH] refactor: move nestjs internal code to separate file --- src/internals/nest.ts | 44 ++++++++++++++++++++++++++++++++++++++++++ src/logged.ts | 2 +- src/reflected.ts | 45 +------------------------------------------ 3 files changed, 46 insertions(+), 45 deletions(-) create mode 100644 src/internals/nest.ts diff --git a/src/internals/nest.ts b/src/internals/nest.ts new file mode 100644 index 0000000..80f8591 --- /dev/null +++ b/src/internals/nest.ts @@ -0,0 +1,44 @@ +import {RouteParamtypes} from '@nestjs/common/enums/route-paramtypes.enum'; +import {assignMetadata, ParamData, PipeTransform, RouteParamMetadata, Type} from "@nestjs/common"; +import {ROUTE_ARGS_METADATA} from '@nestjs/common/constants'; +import {isNil, isString} from "@nestjs/common/utils/shared.utils"; + +export { RouteParamtypes, PipeTransform, Type, ROUTE_ARGS_METADATA }; + +export function createRouteParamDecorator(paramtype: RouteParamtypes) { + return (data?: ParamData): ParameterDecorator => + (target, key, index) => { + const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {}; + Reflect.defineMetadata( + ROUTE_ARGS_METADATA, + assignMetadata>( + args, + paramtype, + index, + data, + ), + target.constructor, + key, + ); + }; +}; + +export const createPipesRouteParamDecorator = + (paramtype: RouteParamtypes) => + ( + data?: any, + ...pipes: (Type | PipeTransform)[] + ): ParameterDecorator => + (target, key, index) => { + const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {}; + const hasParamData = isNil(data) || isString(data); + const paramData = hasParamData ? data : undefined; + const paramPipes = hasParamData ? pipes : [data, ...pipes]; + + Reflect.defineMetadata( + ROUTE_ARGS_METADATA, + assignMetadata(args, paramtype, index, paramData, ...paramPipes), + target.constructor, + key, + ); + }; \ No newline at end of file diff --git a/src/logged.ts b/src/logged.ts index 57f9a6f..d2add5f 100644 --- a/src/logged.ts +++ b/src/logged.ts @@ -15,9 +15,9 @@ import { nestLoggedMetadata, loggedParam, scopedLogger, - createRouteParamDecorator } from "./reflected"; import { imObjectContainedLogSync, getItemByPathSync } from "./internals/utils"; +import { createRouteParamDecorator } from "./internals/nest"; import { RequestMethod } from "@nestjs/common"; const RevRequestMethod = [ diff --git a/src/reflected.ts b/src/reflected.ts index 41e535e..080fefa 100644 --- a/src/reflected.ts +++ b/src/reflected.ts @@ -1,47 +1,4 @@ -import {RouteParamtypes} from '@nestjs/common/enums/route-paramtypes.enum'; -import {assignMetadata, ParamData, PipeTransform, RouteParamMetadata, Type} from "@nestjs/common"; -import {ROUTE_ARGS_METADATA} from '@nestjs/common/constants'; -import {isNil, isString} from "@nestjs/common/utils/shared.utils"; - -export function createRouteParamDecorator(paramtype: RouteParamtypes) { - return (data?: ParamData): ParameterDecorator => - (target, key, index) => { - const args = - Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {}; - Reflect.defineMetadata( - ROUTE_ARGS_METADATA, - assignMetadata>( - args, - paramtype, - index, - data, - ), - target.constructor, - key, - ); - }; -} - -const createPipesRouteParamDecorator = - (paramtype: RouteParamtypes) => - ( - data?: any, - ...pipes: (Type | PipeTransform)[] - ): ParameterDecorator => - (target, key, index) => { - const args = - Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {}; - const hasParamData = isNil(data) || isString(data); - const paramData = hasParamData ? data : undefined; - const paramPipes = hasParamData ? pipes : [data, ...pipes]; - - Reflect.defineMetadata( - ROUTE_ARGS_METADATA, - assignMetadata(args, paramtype, index, paramData, ...paramPipes), - target.constructor, - key, - ); - }; +import { RouteParamtypes, Type, PipeTransform, createPipesRouteParamDecorator, createRouteParamDecorator } from "./internals/nest"; export type Path = string | string[]; export type Paths = Path[];