From 7ad245ef6779319cff1e6f3d83f9651adcd9501a Mon Sep 17 00:00:00 2001 From: Doron Guttman Date: Tue, 2 Jan 2024 20:00:47 -0500 Subject: [PATCH] =?UTF-8?q?fix(core,common):=20=F0=9F=90=9B=20missing=20re?= =?UTF-8?q?gistration=20handling=20of=20`SEARCH`=20http=20verb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes https://github.com/nestjs/nest/issues/12998 --- .../interfaces/http/http-server.interface.ts | 2 + packages/core/adapters/http-adapter.ts | 8 ++-- .../core/helpers/router-method-factory.ts | 43 ++++++++++--------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/common/interfaces/http/http-server.interface.ts b/packages/common/interfaces/http/http-server.interface.ts index 8bf7e49a965..4f11d8be125 100644 --- a/packages/common/interfaces/http/http-server.interface.ts +++ b/packages/common/interfaces/http/http-server.interface.ts @@ -51,6 +51,8 @@ export interface HttpServer< all(handler: RequestHandler): any; options(handler: RequestHandler): any; options(path: string, handler: RequestHandler): any; + search(handler: RequestHandler): any; + search(path: string, handler: RequestHandler): 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; diff --git a/packages/core/adapters/http-adapter.ts b/packages/core/adapters/http-adapter.ts index 6bfc4add701..0c0ae329fb1 100644 --- a/packages/core/adapters/http-adapter.ts +++ b/packages/core/adapters/http-adapter.ts @@ -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); diff --git a/packages/core/helpers/router-method-factory.ts b/packages/core/helpers/router-method-factory.ts index d9b9a5d979e..c761e2df31f 100644 --- a/packages/core/helpers/router-method-factory.ts +++ b/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; + 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; } }