Skip to content

Commit ea700d8

Browse files
author
Elyes BA
committed
feature(core): allow Array in request mapping decorator
This feature was requested in the issue nestjs#1343. When routing, besides using a single string, you can now use an array of strings in the Post, Get, ... decorators.
1 parent 2a5e22e commit ea700d8

7 files changed

Lines changed: 232 additions & 50 deletions

File tree

packages/common/decorators/http/request-mapping.decorator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const defaultMetadata = {
1010
export const RequestMapping = (
1111
metadata: RequestMappingMetadata = defaultMetadata,
1212
): MethodDecorator => {
13-
const path = metadata[PATH_METADATA] || '/';
13+
const pathMetaData = metadata[PATH_METADATA];
14+
const path = pathMetaData && pathMetaData.length ? pathMetaData : false || '/';
1415
const requestMethod = metadata[METHOD_METADATA] || RequestMethod.GET;
1516

1617
return (target, key, descriptor: PropertyDescriptor) => {
@@ -21,7 +22,7 @@ export const RequestMapping = (
2122
};
2223

2324
const createMappingDecorator = (method: RequestMethod) => (
24-
path?: string,
25+
path?: string | string[],
2526
): MethodDecorator => {
2627
return RequestMapping({
2728
[PATH_METADATA]: path,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RequestMethod } from '../enums/request-method.enum';
22

33
export interface RequestMappingMetadata {
4-
path?: string;
4+
path?: string | string[];
55
method?: RequestMethod;
66
}

packages/common/test/decorators/request-mapping.decorator.spec.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,55 @@ describe('@RequestMapping', () => {
88
method: RequestMethod.ALL,
99
};
1010

11+
const requestPropsUsingArray = {
12+
path: ['foo', 'bar'],
13+
method: RequestMethod.ALL,
14+
};
15+
1116
it('should enhance class with expected request metadata', () => {
1217
class Test {
1318
@RequestMapping(requestProps)
14-
public static test() {}
19+
public static test() { }
20+
21+
@RequestMapping(requestPropsUsingArray)
22+
public static testUsingArray() { }
1523
}
1624

1725
const path = Reflect.getMetadata('path', Test.test);
1826
const method = Reflect.getMetadata('method', Test.test);
27+
const pathUsingArray = Reflect.getMetadata('path', Test.testUsingArray);
28+
const methodUsingArray = Reflect.getMetadata('method', Test.testUsingArray);
1929

20-
expect(method).to.be.eql(requestProps.method);
2130
expect(path).to.be.eql(requestProps.path);
31+
expect(method).to.be.eql(requestProps.method);
32+
expect(pathUsingArray).to.be.eql(requestPropsUsingArray.path);
33+
expect(methodUsingArray).to.be.eql(requestPropsUsingArray.method);
2234
});
2335

2436
it('should set request method on GET by default', () => {
2537
class Test {
2638
@RequestMapping({ path: '' })
27-
public static test() {}
39+
public static test() { }
2840
}
2941

3042
const method = Reflect.getMetadata('method', Test.test);
43+
3144
expect(method).to.be.eql(RequestMethod.GET);
3245
});
3346

3447
it('should set path on "/" by default', () => {
3548
class Test {
3649
@RequestMapping({})
37-
public static test() {}
50+
public static test() { }
51+
52+
@RequestMapping({ path: [] })
53+
public static testUsingArray() { }
3854
}
3955

40-
const method = Reflect.getMetadata('path', Test.test);
41-
expect(method).to.be.eql('/');
56+
const path = Reflect.getMetadata('path', Test.test);
57+
const pathUsingArray = Reflect.getMetadata('path', Test.testUsingArray);
58+
59+
expect(path).to.be.eql('/');
60+
expect(pathUsingArray).to.be.eql('/');
4261
});
4362
});

0 commit comments

Comments
 (0)