feat(kit): allow addServerHandler to use method suffix of the file path (#5465)

This commit is contained in:
Julien Huang 2022-06-15 13:53:31 +02:00 committed by GitHub
parent e10517d4d7
commit ccec0a3738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,19 @@ export interface LegacyServerMiddleware {
handler: Middleware | string
}
/**
* normalize handler object
*
*/
function normalizeHandlerMethod (handler: NitroEventHandler) {
// retrieve method from handler file name
const [, method = undefined] = handler.handler.match(/\.(get|head|patch|post|put|delete|connect|options|trace)(\.\w+)*$/) || []
return {
method,
...handler
}
}
/**
* Adds a new server middleware to the end of the server middleware array.
*
@ -23,7 +36,7 @@ export function addServerMiddleware (middleware: LegacyServerMiddleware) {
*
*/
export function addServerHandler (handler: NitroEventHandler) {
useNuxt().options.serverHandlers.push(handler)
useNuxt().options.serverHandlers.push(normalizeHandlerMethod(handler))
}
/**