mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 01:45:53 +00:00
feat(kit): allow addServerHandler
to use method suffix of the file path (#5465)
This commit is contained in:
parent
e10517d4d7
commit
ccec0a3738
@ -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))
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user