Skip to content

Commit

Permalink
fix(core,common): 馃悰 missing registration handling of SEARCH http verb
Browse files Browse the repository at this point in the history
fixes #12998
  • Loading branch information
doronguttman committed Jan 3, 2024
1 parent 585f902 commit 7ad245e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/common/interfaces/http/http-server.interface.ts
Expand Up @@ -51,6 +51,8 @@ export interface HttpServer<
all(handler: RequestHandler<TRequest, TResponse>): any;
options(handler: RequestHandler<TRequest, TResponse>): any;
options(path: string, handler: RequestHandler<TRequest, TResponse>): any;
search(handler: RequestHandler<TRequest, TResponse>): any;
search(path: string, handler: RequestHandler<TRequest, TResponse>): any;
listen(port: number | string, callback?: () => void): any;
listen(port: number | string, hostname: string, callback?: () => void): any;
reply(response: any, body: any, statusCode?: number): any;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/adapters/http-adapter.ts
Expand Up @@ -68,10 +68,10 @@ 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 search(handler: RequestHandler);
public search(path: any, handler: RequestHandler);
public search(...args: any[]) {
return this.instance.search(...args);
}

public options(handler: RequestHandler);
Expand Down
43 changes: 23 additions & 20 deletions packages/core/helpers/router-method-factory.ts
@@ -1,28 +1,31 @@
import { HttpServer } from '@nestjs/common';
import { RequestMethod } from '@nestjs/common/enums/request-method.enum';

/**
* Ensures (via satisfies) there's a mapping between the RequestMethod enum and the HttpServer interface.
* @note This is a compile-time check, so if the interface changes, the compiler will complain.
* This is to resolve a case where a new RequestMethod is added, but the RouterMethodFactory is not updated.
*/
const RequestMethodMap = {
[RequestMethod.GET]: 'get',
[RequestMethod.POST]: 'post',
[RequestMethod.PUT]: 'put',
[RequestMethod.DELETE]: 'delete',
[RequestMethod.PATCH]: 'patch',
[RequestMethod.ALL]: 'all',
[RequestMethod.OPTIONS]: 'options',
[RequestMethod.HEAD]: 'head',
[RequestMethod.SEARCH]: 'search',
} as const satisfies Record<RequestMethod, keyof HttpServer>;

export class RouterMethodFactory {
public get(target: HttpServer, requestMethod: RequestMethod): Function {
switch (requestMethod) {
case RequestMethod.POST:
return target.post;
case RequestMethod.ALL:
return target.all;
case RequestMethod.DELETE:
return target.delete;
case RequestMethod.PUT:
return target.put;
case RequestMethod.PATCH:
return target.patch;
case RequestMethod.OPTIONS:
return target.options;
case RequestMethod.HEAD:
return target.head;
case RequestMethod.GET:
return target.get;
default: {
return target.use;
}
const methodName = RequestMethodMap[requestMethod];
const method = target[methodName];
if (!method) {
// There should probably be a warning message in this case
return target.use;
}
return method;
}
}

0 comments on commit 7ad245e

Please sign in to comment.