Skip to content

Commit

Permalink
Merge pull request #10533 from Gustrb/add-search-method
Browse files Browse the repository at this point in the history
feat(core/common): Add @Search decorator to the available HTTP route handlers
  • Loading branch information
kamilmysliwiec committed Apr 17, 2023
2 parents 69fba03 + a7655a0 commit 7b05cd0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/common/decorators/http/request-mapping.decorator.ts
Expand Up @@ -109,3 +109,12 @@ export const Head = createMappingDecorator(RequestMethod.HEAD);
* @publicApi
*/
export const All = createMappingDecorator(RequestMethod.ALL);

/**
* Route handler (method) Decorator. Routes all HTTP requests to the specified path.
*
* @see [Routing](https://docs.nestjs.com/controllers#routing)
*
* @publicApi
*/
export const Search = createMappingDecorator(RequestMethod.SEARCH);
1 change: 1 addition & 0 deletions packages/common/enums/request-method.enum.ts
Expand Up @@ -7,4 +7,5 @@ export enum RequestMethod {
ALL,
OPTIONS,
HEAD,
SEARCH,
}
59 changes: 58 additions & 1 deletion packages/common/test/decorators/route-params.decorator.spec.ts
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { Body, HostParam, Param, Query } from '../../decorators';
import { Body, HostParam, Param, Query, Search } from '../../decorators';
import { RequestMethod } from '../../enums/request-method.enum';
import { All, Delete, Get, Patch, Post, Put } from '../../index';

Expand Down Expand Up @@ -311,6 +311,63 @@ describe('@Patch', () => {
});
});

describe('@Search', () => {
const requestPath = 'test';
const requestProps = {
path: requestPath,
method: RequestMethod.SEARCH,
};

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

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

@Search(requestPathUsingArray)
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(path).to.be.eql(requestPath);
expect(method).to.be.eql(requestProps.method);
expect(pathUsingArray).to.be.eql(requestPathUsingArray);
expect(methodUsingArray).to.be.eql(requestPropsUsingArray.method);
});

it('should set path on "/" by default', () => {
class Test {
@Search()
public static test(
@Query() query,
@Param() params,
@HostParam() hostParams,
) {}

@Search([])
public static testUsingArray(
@Query() query,
@Param() params,
@HostParam() hostParams,
) {}
}

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

describe('Inheritance', () => {
const requestPath = 'test';
const requestProps = {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/adapters/http-adapter.ts
Expand Up @@ -68,6 +68,12 @@ export abstract class AbstractHttpAdapter<
return this.instance.all(...args);
}

public search(port: string | number, callback?: () => void);
public search(port: string | number, hostname: string, callback?: () => void);
public search(port: any, hostname?: any, callback?: any) {
return this.instance.search(port, hostname, callback);
}

public options(handler: RequestHandler);
public options(path: any, handler: RequestHandler);
public options(...args: any[]) {
Expand Down

0 comments on commit 7b05cd0

Please sign in to comment.