Skip to content

Commit

Permalink
feature(core): allow Array in request mapping decorator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Elyes BA committed Jan 5, 2019
1 parent 2a5e22e commit ea700d8
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 50 deletions.
5 changes: 3 additions & 2 deletions packages/common/decorators/http/request-mapping.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const defaultMetadata = {
export const RequestMapping = (
metadata: RequestMappingMetadata = defaultMetadata,
): MethodDecorator => {
const path = metadata[PATH_METADATA] || '/';
const pathMetaData = metadata[PATH_METADATA];
const path = pathMetaData && pathMetaData.length ? pathMetaData : false || '/';
const requestMethod = metadata[METHOD_METADATA] || RequestMethod.GET;

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

const createMappingDecorator = (method: RequestMethod) => (
path?: string,
path?: string | string[],
): MethodDecorator => {
return RequestMapping({
[PATH_METADATA]: path,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestMethod } from '../enums/request-method.enum';

export interface RequestMappingMetadata {
path?: string;
path?: string | string[];
method?: RequestMethod;
}
31 changes: 25 additions & 6 deletions packages/common/test/decorators/request-mapping.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,55 @@ describe('@RequestMapping', () => {
method: RequestMethod.ALL,
};

const requestPropsUsingArray = {
path: ['foo', 'bar'],
method: RequestMethod.ALL,
};

it('should enhance class with expected request metadata', () => {
class Test {
@RequestMapping(requestProps)
public static test() {}
public static test() { }

@RequestMapping(requestPropsUsingArray)
public static testUsingArray() { }
}

const path = Reflect.getMetadata('path', Test.test);
const method = Reflect.getMetadata('method', Test.test);
const pathUsingArray = Reflect.getMetadata('path', Test.testUsingArray);
const methodUsingArray = Reflect.getMetadata('method', Test.testUsingArray);

expect(method).to.be.eql(requestProps.method);
expect(path).to.be.eql(requestProps.path);
expect(method).to.be.eql(requestProps.method);
expect(pathUsingArray).to.be.eql(requestPropsUsingArray.path);
expect(methodUsingArray).to.be.eql(requestPropsUsingArray.method);
});

it('should set request method on GET by default', () => {
class Test {
@RequestMapping({ path: '' })
public static test() {}
public static test() { }
}

const method = Reflect.getMetadata('method', Test.test);

expect(method).to.be.eql(RequestMethod.GET);
});

it('should set path on "/" by default', () => {
class Test {
@RequestMapping({})
public static test() {}
public static test() { }

@RequestMapping({ path: [] })
public static testUsingArray() { }
}

const method = Reflect.getMetadata('path', Test.test);
expect(method).to.be.eql('/');
const path = Reflect.getMetadata('path', Test.test);
const pathUsingArray = Reflect.getMetadata('path', Test.testUsingArray);

expect(path).to.be.eql('/');
expect(pathUsingArray).to.be.eql('/');
});
});
Loading

0 comments on commit ea700d8

Please sign in to comment.