2023-02-06 23:24:56 +00:00
|
|
|
import type { NuxtHooks, NuxtMiddleware } from '@nuxt/schema'
|
2023-01-21 16:54:02 +00:00
|
|
|
import type { NitroRouteConfig } from 'nitropack'
|
|
|
|
import { defu } from 'defu'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { useNuxt } from './context'
|
|
|
|
import { isNuxt2 } from './compatibility'
|
|
|
|
|
|
|
|
export function extendPages (cb: NuxtHooks['pages:extend']) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
if (isNuxt2(nuxt)) {
|
2022-10-27 10:36:37 +00:00
|
|
|
// @ts-expect-error
|
2021-11-21 16:14:46 +00:00
|
|
|
nuxt.hook('build:extendRoutes', cb)
|
|
|
|
} else {
|
|
|
|
nuxt.hook('pages:extend', cb)
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 16:54:02 +00:00
|
|
|
|
|
|
|
export interface ExtendRouteRulesOptions {
|
|
|
|
/**
|
|
|
|
* Override route rule config
|
|
|
|
*
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
override?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export function extendRouteRules (route: string, rule: NitroRouteConfig, options: ExtendRouteRulesOptions = {}) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
for (const opts of [nuxt.options, nuxt.options.nitro]) {
|
|
|
|
if (!opts.routeRules) {
|
|
|
|
opts.routeRules = {}
|
|
|
|
}
|
|
|
|
opts.routeRules[route] = options.override
|
|
|
|
? defu(rule, opts.routeRules[route])
|
|
|
|
: defu(opts.routeRules[route], rule)
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 23:24:56 +00:00
|
|
|
|
|
|
|
export interface AddRouteMiddlewareOptions {
|
|
|
|
/**
|
|
|
|
* Override existing middleware with the same name, if it exists
|
|
|
|
*
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
override?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addRouteMiddleware (input: NuxtMiddleware | NuxtMiddleware[], options: AddRouteMiddlewareOptions = {}) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
const middlewares = Array.isArray(input) ? input : [input]
|
|
|
|
nuxt.hook('app:resolve', (app) => {
|
|
|
|
for (const middleware of middlewares) {
|
|
|
|
const find = app.middleware.findIndex(item => item.name === middleware.name)
|
|
|
|
if (find >= 0) {
|
|
|
|
if (options.override === true) {
|
|
|
|
app.middleware[find] = middleware
|
|
|
|
} else {
|
|
|
|
console.warn(`'${middleware.name}' middleware already exists at '${app.middleware[find].path}'. You can set \`override: true\` to replace it.`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app.middleware.push(middleware)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|