mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-12 00:53:55 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import type { Middleware } from 'h3'
|
|
import type { NitroEventHandler, NitroDevEventHandler } from 'nitropack'
|
|
import { useNuxt } from './context'
|
|
|
|
export interface LegacyServerMiddleware {
|
|
route?: string,
|
|
path?: string,
|
|
handle?: Middleware | string
|
|
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.
|
|
*
|
|
* @deprecated Use addServerHandler instead
|
|
*/
|
|
export function addServerMiddleware (middleware: LegacyServerMiddleware) {
|
|
useNuxt().options.serverMiddleware.push(middleware)
|
|
}
|
|
|
|
/**
|
|
* Adds a nitro server handler
|
|
*
|
|
*/
|
|
export function addServerHandler (handler: NitroEventHandler) {
|
|
useNuxt().options.serverHandlers.push(normalizeHandlerMethod(handler))
|
|
}
|
|
|
|
/**
|
|
* Adds a nitro server handler for development-only
|
|
*
|
|
*/
|
|
export function addDevServerHandler (handler: NitroDevEventHandler) {
|
|
useNuxt().options.devServerHandlers.push(handler)
|
|
}
|