Skip to content

Commit

Permalink
feat(kit): allow addServerHandler to use method suffix of the file …
Browse files Browse the repository at this point in the history
…path (nuxt#5465)
  • Loading branch information
huang-julien authored Jun 15, 2022
1 parent e10517d commit ccec0a3
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/kit/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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))
}

/**
Expand Down

0 comments on commit ccec0a3

Please sign in to comment.