refactor: move nestjs internal code to separate file
This commit is contained in:
parent
17ce22a57d
commit
1196cf7001
44
src/internals/nest.ts
Normal file
44
src/internals/nest.ts
Normal 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,
|
||||||
|
);
|
||||||
|
};
|
@ -15,9 +15,9 @@ import {
|
|||||||
nestLoggedMetadata,
|
nestLoggedMetadata,
|
||||||
loggedParam,
|
loggedParam,
|
||||||
scopedLogger,
|
scopedLogger,
|
||||||
createRouteParamDecorator
|
|
||||||
} from "./reflected";
|
} from "./reflected";
|
||||||
import { imObjectContainedLogSync, getItemByPathSync } from "./internals/utils";
|
import { imObjectContainedLogSync, getItemByPathSync } from "./internals/utils";
|
||||||
|
import { createRouteParamDecorator } from "./internals/nest";
|
||||||
import { RequestMethod } from "@nestjs/common";
|
import { RequestMethod } from "@nestjs/common";
|
||||||
|
|
||||||
const RevRequestMethod = [
|
const RevRequestMethod = [
|
||||||
|
@ -1,47 +1,4 @@
|
|||||||
import {RouteParamtypes} from '@nestjs/common/enums/route-paramtypes.enum';
|
import { RouteParamtypes, Type, PipeTransform, createPipesRouteParamDecorator, createRouteParamDecorator } from "./internals/nest";
|
||||||
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,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Path = string | string[];
|
export type Path = string | string[];
|
||||||
export type Paths = Path[];
|
export type Paths = Path[];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user