Skip to content

Commit f6e18f1

Browse files
update(@nestjs/core) adjust code style
1 parent fd3d745 commit f6e18f1

8 files changed

Lines changed: 97 additions & 13 deletions

File tree

src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const PARAMTYPES_METADATA = 'design:paramtypes';
1111
export const SELF_DECLARED_DEPS_METADATA = 'self:paramtypes';
1212
export const METHOD_METADATA = 'method';
1313
export const ROUTE_ARGS_METADATA = '__routeArguments__';
14+
export const CUSTOM_ROUTE_AGRS_METADATA = '__customRouteArgs__';
1415
export const EXCEPTION_FILTERS_METADATA = '__exceptionFilters__';
1516
export const FILTER_CATCH_EXCEPTIONS = '__filterCatchExceptions__';
1617
export const PIPES_METADATA = '__pipes__';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type CustomParamFactory = (data, req) => any;

src/common/interfaces/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export * from './can-activate.interface';
1919
export * from './exceptions/rpc-exception-filter.interface';
2020
export * from './exceptions/ws-exception-filter.interface';
2121
export * from './execution-context.interface';
22-
export * from './nest-interceptor.interface';
22+
export * from './nest-interceptor.interface';
23+
export * from './custom-route-param-factory.interface';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect } from 'chai';
2+
import { createRouteParamDecorator } from '../../utils/decorators/create-route-param-metadata.decorator';
3+
import { CUSTOM_ROUTE_AGRS_METADATA } from '../../constants';
4+
5+
describe('createRouteParamDecorator', () => {
6+
let key;
7+
let reflector;
8+
let result;
9+
10+
beforeEach(() => {
11+
key = 'key';
12+
reflector = (data, req, res, next) => true;
13+
result = createRouteParamDecorator(reflector);
14+
});
15+
it('should return a function as a first element', () => {
16+
expect(result).to.be.a('function');
17+
});
18+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ROUTE_ARGS_METADATA, CUSTOM_ROUTE_AGRS_METADATA } from '../../constants';
2+
import { CustomParamFactory } from '../../interfaces/custom-route-param-factory.interface';
3+
import { RouteParamsMetadata, ParamData } from './route-params.decorator';
4+
5+
const assignCustomMetadata = (
6+
args: RouteParamsMetadata,
7+
paramtype: number|string,
8+
index: number,
9+
factory: CustomParamFactory,
10+
data?: ParamData,
11+
) => ({
12+
...args,
13+
[`${index}:${paramtype}${CUSTOM_ROUTE_AGRS_METADATA}:${index}`]: {
14+
index,
15+
factory,
16+
data,
17+
},
18+
});
19+
20+
const randomString = () => Math.random().toString(36).substring(2, 15);
21+
22+
/**
23+
* Create route params custom decorator
24+
* @param factory
25+
*/
26+
export const createRouteParamDecorator = (factory: CustomParamFactory) => {
27+
const paramtype = randomString() + randomString();
28+
return (data?: ParamData): ParameterDecorator => (target, key, index) => {
29+
const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, target, key) || {};
30+
Reflect.defineMetadata(
31+
ROUTE_ARGS_METADATA,
32+
assignCustomMetadata(args, paramtype, index, factory, data),
33+
target,
34+
key,
35+
);
36+
};
37+
};

src/common/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export * from './decorators/reflect-metadata.decorator';
1515
export * from './decorators/use-interceptors.decorator';
1616
export * from './decorators/http-code.decorator';
1717
export * from './decorators/bind.decorator';
18-
export * from './forward-ref.util';
18+
export * from './forward-ref.util';
19+
export * from './decorators/create-route-param-metadata.decorator';

src/core/router/router-execution-context.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'reflect-metadata';
2-
import { ROUTE_ARGS_METADATA, PARAMTYPES_METADATA, HTTP_CODE_METADATA } from '@nestjs/common/constants';
3-
import { isUndefined } from '@nestjs/common/utils/shared.utils';
2+
import { ROUTE_ARGS_METADATA, PARAMTYPES_METADATA, HTTP_CODE_METADATA, CUSTOM_ROUTE_AGRS_METADATA } from '@nestjs/common/constants';
3+
import { isUndefined, isFunction } from '@nestjs/common/utils/shared.utils';
44
import { RouteParamtypes } from '@nestjs/common/enums/route-paramtypes.enum';
55
import { Controller, Transform } from '@nestjs/common/interfaces';
66
import { RouteParamsMetadata } from '@nestjs/common/utils';
@@ -73,7 +73,7 @@ export class RouterExecutionContext {
7373
};
7474
}
7575

76-
public mapParamType(key: string): RouteParamtypes {
76+
public mapParamType(key: string): RouteParamtypes | number {
7777
const keyPair = key.split(':');
7878
return Number(keyPair[0]);
7979
}
@@ -100,9 +100,17 @@ export class RouterExecutionContext {
100100

101101
public exchangeKeysForValues(keys: string[], metadata: RouteParamsMetadata): ParamProperties[] {
102102
return keys.map(key => {
103-
const type = this.mapParamType(key);
104103
const { index, data, pipes } = metadata[key];
104+
const type = this.mapParamType(key);
105105

106+
if (key.includes(CUSTOM_ROUTE_AGRS_METADATA)) {
107+
const { factory } = metadata[key];
108+
const customExtractValue = !isUndefined(factory) && isFunction(factory)
109+
? (req, res, next) => factory(data, req)
110+
: () => ({});
111+
112+
return { index, extractValue: customExtractValue, type, data, pipes };
113+
}
106114
const extractValue = (req, res, next) => this.paramsFactory.exchangeKeyForValue(type, data, { req, res, next });
107115
return { index, extractValue, type, data, pipes };
108116
});

src/core/test/router/router-execution-context.spec.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as sinon from 'sinon';
22
import { expect } from 'chai';
33
import { RouteParamtypes } from '../../../common/enums/route-paramtypes.enum';
4+
import { CUSTOM_ROUTE_AGRS_METADATA } from '../../../common/constants';
5+
import { createRouteParamDecorator } from '../../../common/utils/decorators/create-route-param-metadata.decorator';
46
import { RouterExecutionContext } from '../../router/router-execution-context';
57
import { RouteParamsMetadata, Request, Body } from '../../../index';
68
import { RouteParamsFactory } from '../../router/route-params-factory';
@@ -104,12 +106,15 @@ describe('RouterExecutionContext', () => {
104106
});
105107
});
106108
describe('reflectCallbackMetadata', () => {
109+
const CustomDecorator = createRouteParamDecorator(() => {});
107110
class TestController {
108-
public callback(@Request() req, @Body() body) {}
111+
public callback(@Request() req, @Body() body, @CustomDecorator() custom) {}
109112
}
110113
it('should returns ROUTE_ARGS_METADATA callback metadata', () => {
111114
const instance = new TestController();
112115
const metadata = contextCreator.reflectCallbackMetadata(instance, 'callback');
116+
console.log(metadata);
117+
113118
const expectedMetadata = {
114119
[`${RouteParamtypes.REQUEST}:0`]: {
115120
index: 0,
@@ -121,8 +126,22 @@ describe('RouterExecutionContext', () => {
121126
data: undefined,
122127
pipes: [],
123128
},
129+
[`custom${CUSTOM_ROUTE_AGRS_METADATA}:2`]: {
130+
index: 2,
131+
factory: () => {},
132+
data: undefined,
133+
},
124134
};
125-
expect(metadata).to.deep.equal(expectedMetadata);
135+
expect(metadata[`${RouteParamtypes.REQUEST}:0`]).to.deep.equal(expectedMetadata[`${RouteParamtypes.REQUEST}:0`]);
136+
expect(metadata[`${RouteParamtypes.REQUEST}:1`]).to.deep.equal(expectedMetadata[`${RouteParamtypes.REQUEST}:1`]);
137+
138+
const keys = Object.keys(metadata);
139+
const custom = keys.find((key) => key.includes(CUSTOM_ROUTE_AGRS_METADATA));
140+
141+
expect(metadata[custom]).to.be.an('object');
142+
expect(metadata[custom].index).to.be.eq(2);
143+
expect(metadata[custom].data).to.be.eq(undefined);
144+
expect(metadata[custom].factory).to.be.a('function');
126145
});
127146
});
128147
describe('getArgumentsLength', () => {
@@ -153,17 +172,15 @@ describe('RouterExecutionContext', () => {
153172
it('should exchange arguments keys for appropriate values', () => {
154173
const metadata = {
155174
[RouteParamtypes.REQUEST]: { index: 0, data: 'test', pipes: [] },
156-
[RouteParamtypes.BODY]: {
157-
index: 2,
158-
data: 'test',
159-
pipes: [],
160-
},
175+
[RouteParamtypes.BODY]: { index: 2, data: 'test', pipes: [] },
176+
[`key${CUSTOM_ROUTE_AGRS_METADATA}`]: { index: 3, data: 'custom', pipes: [] },
161177
};
162178
const keys = Object.keys(metadata);
163179
const values = contextCreator.exchangeKeysForValues(keys, metadata);
164180
const expectedValues = [
165181
{ index: 0, type: RouteParamtypes.REQUEST, data: 'test' },
166182
{ index: 2, type: RouteParamtypes.BODY, data: 'test' },
183+
{ index: 3, type: `key${CUSTOM_ROUTE_AGRS_METADATA}`, data: 'custom' },
167184
];
168185
expect(values[0]).to.deep.include(expectedValues[0]);
169186
expect(values[1]).to.deep.include(expectedValues[1]);

0 commit comments

Comments
 (0)