feat(kit): add extendRouteRules method (#9771)

This commit is contained in:
Mehdi HosseinZade 2023-01-21 20:24:02 +03:30 committed by GitHub
parent fa129cb83d
commit 585e8be2e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -61,6 +61,7 @@ description: Nuxt Kit provides composable utilities to help interacting with Nux
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/pages.ts)
- `extendPages (callback: pages => void)`
- `extendRouteRules (route: string, rule: NitroRouteConfig, options: ExtendRouteRulesOptions)`
### Plugins

View File

@ -1,4 +1,6 @@
import type { NuxtHooks } from '@nuxt/schema'
import type { NitroRouteConfig } from 'nitropack'
import { defu } from 'defu'
import { useNuxt } from './context'
import { isNuxt2 } from './compatibility'
@ -11,3 +13,24 @@ export function extendPages (cb: NuxtHooks['pages:extend']) {
nuxt.hook('pages:extend', cb)
}
}
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)
}
}