2024-06-26 13:18:05 +00:00
|
|
|
import type { Nitro, NitroDevEventHandler, NitroEventHandler } from 'nitro/types'
|
2023-09-28 10:08:02 +00:00
|
|
|
import type { Import } from 'unimport'
|
2022-11-03 14:04:02 +00:00
|
|
|
import { normalize } from 'pathe'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { useNuxt } from './context'
|
2023-12-23 14:22:58 +00:00
|
|
|
import { toArray } from './utils'
|
2021-11-21 16:14:46 +00:00
|
|
|
|
2024-10-22 13:39:50 +00:00
|
|
|
const HANDLER_METHOD_RE = /\.(get|head|patch|post|put|delete|connect|options|trace)(\.\w+)*$/
|
2022-06-15 11:53:31 +00:00
|
|
|
/**
|
|
|
|
* normalize handler object
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function normalizeHandlerMethod (handler: NitroEventHandler) {
|
|
|
|
// retrieve method from handler file name
|
2024-10-22 13:39:50 +00:00
|
|
|
const [, method = undefined] = handler.handler.match(HANDLER_METHOD_RE) || []
|
2022-06-15 11:53:31 +00:00
|
|
|
return {
|
2024-06-26 13:18:05 +00:00
|
|
|
method: method as 'get' | 'head' | 'patch' | 'post' | 'put' | 'delete' | 'connect' | 'options' | 'trace' | undefined,
|
2022-11-03 14:04:02 +00:00
|
|
|
...handler,
|
2024-04-05 18:08:32 +00:00
|
|
|
handler: normalize(handler.handler),
|
2022-06-15 11:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 13:31:52 +00:00
|
|
|
/**
|
|
|
|
* Adds a nitro server handler
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export function addServerHandler (handler: NitroEventHandler) {
|
2022-06-15 11:53:31 +00:00
|
|
|
useNuxt().options.serverHandlers.push(normalizeHandlerMethod(handler))
|
2022-05-06 13:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a nitro server handler for development-only
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export function addDevServerHandler (handler: NitroDevEventHandler) {
|
|
|
|
useNuxt().options.devServerHandlers.push(handler)
|
|
|
|
}
|
2022-09-15 16:10:50 +00:00
|
|
|
|
2022-11-03 14:54:05 +00:00
|
|
|
/**
|
|
|
|
* Adds a Nitro plugin
|
|
|
|
*/
|
|
|
|
export function addServerPlugin (plugin: string) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
nuxt.options.nitro.plugins = nuxt.options.nitro.plugins || []
|
|
|
|
nuxt.options.nitro.plugins.push(normalize(plugin))
|
|
|
|
}
|
|
|
|
|
2022-11-03 21:03:12 +00:00
|
|
|
/**
|
|
|
|
* Adds routes to be prerendered
|
|
|
|
*/
|
2023-10-02 21:36:00 +00:00
|
|
|
export function addPrerenderRoutes (routes: string | string[]) {
|
2022-11-03 21:03:12 +00:00
|
|
|
const nuxt = useNuxt()
|
2023-12-23 14:22:58 +00:00
|
|
|
|
|
|
|
routes = toArray(routes).filter(Boolean)
|
2022-11-03 21:03:12 +00:00
|
|
|
if (!routes.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nuxt.hook('prerender:routes', (ctx) => {
|
|
|
|
for (const route of routes) {
|
|
|
|
ctx.routes.add(route)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-15 16:10:50 +00:00
|
|
|
/**
|
|
|
|
* Access to the Nitro instance
|
|
|
|
*
|
|
|
|
* **Note:** You can call `useNitro()` only after `ready` hook.
|
|
|
|
*
|
|
|
|
* **Note:** Changes to the Nitro instance configuration are not applied.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* nuxt.hook('ready', () => {
|
|
|
|
* console.log(useNitro())
|
|
|
|
* })
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function useNitro (): Nitro {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
if (!(nuxt as any)._nitro) {
|
|
|
|
throw new Error('Nitro is not initialized yet. You can call `useNitro()` only after `ready` hook.')
|
|
|
|
}
|
|
|
|
return (nuxt as any)._nitro
|
|
|
|
}
|
2023-09-28 10:08:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add server imports to be auto-imported by Nitro
|
|
|
|
*/
|
|
|
|
export function addServerImports (imports: Import[]) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
nuxt.hook('nitro:config', (config) => {
|
|
|
|
config.imports = config.imports || {}
|
2023-12-23 14:22:58 +00:00
|
|
|
config.imports.imports = config.imports.imports || []
|
|
|
|
config.imports.imports.push(...imports)
|
2023-09-28 10:08:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-31 13:16:01 +00:00
|
|
|
* Add directories to be scanned for auto-imports by Nitro
|
2023-09-28 10:08:02 +00:00
|
|
|
*/
|
|
|
|
export function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean } = {}) {
|
|
|
|
const nuxt = useNuxt()
|
2023-12-23 14:22:58 +00:00
|
|
|
const _dirs = toArray(dirs)
|
2023-09-28 10:08:02 +00:00
|
|
|
nuxt.hook('nitro:config', (config) => {
|
2023-10-31 13:16:01 +00:00
|
|
|
config.imports = config.imports || {}
|
|
|
|
config.imports.dirs = config.imports.dirs || []
|
|
|
|
config.imports.dirs[opts.prepend ? 'unshift' : 'push'](..._dirs)
|
2023-09-28 10:08:02 +00:00
|
|
|
})
|
|
|
|
}
|
2023-12-14 17:11:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add directories to be scanned by Nitro. It will check for subdirectories,
|
|
|
|
* which will be registered just like the `~/server` folder is.
|
|
|
|
*/
|
|
|
|
export function addServerScanDir (dirs: string | string[], opts: { prepend?: boolean } = {}) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
nuxt.hook('nitro:config', (config) => {
|
|
|
|
config.scanDirs = config.scanDirs || []
|
|
|
|
|
2023-12-23 14:22:58 +00:00
|
|
|
for (const dir of toArray(dirs)) {
|
2023-12-14 17:11:53 +00:00
|
|
|
config.scanDirs[opts.prepend ? 'unshift' : 'push'](dir)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|