-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core,common): 🐛 missing registration handling of
SEARCH
http verb
fixes #12998
- Loading branch information
1 parent
d6e404d
commit cb6664c
Showing
3 changed files
with
29 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |