Skip to content

Commit 7ad245e

Browse files
committed
fix(core,common): 🐛 missing registration handling of SEARCH http verb
fixes #12998
1 parent 585f902 commit 7ad245e

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

packages/common/interfaces/http/http-server.interface.ts

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export interface HttpServer<
5151
all(handler: RequestHandler<TRequest, TResponse>): any;
5252
options(handler: RequestHandler<TRequest, TResponse>): any;
5353
options(path: string, handler: RequestHandler<TRequest, TResponse>): any;
54+
search(handler: RequestHandler<TRequest, TResponse>): any;
55+
search(path: string, handler: RequestHandler<TRequest, TResponse>): any;
5456
listen(port: number | string, callback?: () => void): any;
5557
listen(port: number | string, hostname: string, callback?: () => void): any;
5658
reply(response: any, body: any, statusCode?: number): any;

packages/core/adapters/http-adapter.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ export abstract class AbstractHttpAdapter<
6868
return this.instance.all(...args);
6969
}
7070

71-
public search(port: string | number, callback?: () => void);
72-
public search(port: string | number, hostname: string, callback?: () => void);
73-
public search(port: any, hostname?: any, callback?: any) {
74-
return this.instance.search(port, hostname, callback);
71+
public search(handler: RequestHandler);
72+
public search(path: any, handler: RequestHandler);
73+
public search(...args: any[]) {
74+
return this.instance.search(...args);
7575
}
7676

7777
public options(handler: RequestHandler);
+23-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import { HttpServer } from '@nestjs/common';
22
import { RequestMethod } from '@nestjs/common/enums/request-method.enum';
33

4+
/**
5+
* Ensures (via satisfies) there's a mapping between the RequestMethod enum and the HttpServer interface.
6+
* @note This is a compile-time check, so if the interface changes, the compiler will complain.
7+
* This is to resolve a case where a new RequestMethod is added, but the RouterMethodFactory is not updated.
8+
*/
9+
const RequestMethodMap = {
10+
[RequestMethod.GET]: 'get',
11+
[RequestMethod.POST]: 'post',
12+
[RequestMethod.PUT]: 'put',
13+
[RequestMethod.DELETE]: 'delete',
14+
[RequestMethod.PATCH]: 'patch',
15+
[RequestMethod.ALL]: 'all',
16+
[RequestMethod.OPTIONS]: 'options',
17+
[RequestMethod.HEAD]: 'head',
18+
[RequestMethod.SEARCH]: 'search',
19+
} as const satisfies Record<RequestMethod, keyof HttpServer>;
20+
421
export class RouterMethodFactory {
522
public get(target: HttpServer, requestMethod: RequestMethod): Function {
6-
switch (requestMethod) {
7-
case RequestMethod.POST:
8-
return target.post;
9-
case RequestMethod.ALL:
10-
return target.all;
11-
case RequestMethod.DELETE:
12-
return target.delete;
13-
case RequestMethod.PUT:
14-
return target.put;
15-
case RequestMethod.PATCH:
16-
return target.patch;
17-
case RequestMethod.OPTIONS:
18-
return target.options;
19-
case RequestMethod.HEAD:
20-
return target.head;
21-
case RequestMethod.GET:
22-
return target.get;
23-
default: {
24-
return target.use;
25-
}
23+
const methodName = RequestMethodMap[requestMethod];
24+
const method = target[methodName];
25+
if (!method) {
26+
// There should probably be a warning message in this case
27+
return target.use;
2628
}
29+
return method;
2730
}
2831
}

0 commit comments

Comments
 (0)