Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core/common): Add @Search decorator to the available HTTP route handlers #10533

Merged
merged 4 commits into from Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -35,7 +35,7 @@ jobs:
- checkout
- run:
name: Update NPM version
command: 'sudo npm install -g npm@latest'
command: 'sudo npm install -g npm@^8'
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
Expand Down
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