refactor: move nestjs internal code to separate file

This commit is contained in:
Shinwoo PARK 2025-03-21 09:04:47 +09:00
parent 17ce22a57d
commit 1196cf7001
3 changed files with 46 additions and 45 deletions

44
src/internals/nest.ts Normal file
View File

@ -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<RouteParamtypes, Record<number, RouteParamMetadata>>(
args,
paramtype,
index,
data,
),
target.constructor,
key,
);
};
};
export const createPipesRouteParamDecorator =
(paramtype: RouteParamtypes) =>
(
data?: any,
...pipes: (Type<PipeTransform> | 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,
);
};

View File

@ -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 = [

View File

@ -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<RouteParamtypes, Record<number, RouteParamMetadata>>(
args,
paramtype,
index,
data,
),
target.constructor,
key,
);
};
}
const createPipesRouteParamDecorator =
(paramtype: RouteParamtypes) =>
(
data?: any,
...pipes: (Type<PipeTransform> | 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[];