fix(nuxt): provide appMiddleware types with universal router

This commit is contained in:
Daniel Roe 2024-03-16 15:12:49 -07:00
parent 4925670dcd
commit 87c0678f99
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
2 changed files with 18 additions and 1 deletions

View File

@ -122,6 +122,17 @@ export default defineNuxtModule({
'export const START_LOCATION = Symbol(\'router:start-location\')'
].join('\n')
})
addTypeTemplate({
filename: 'types/middleware.d.ts',
getContents: () => [
'declare module \'nitropack\' {',
' interface NitroRouteConfig {',
' appMiddleware?: string | string[] | Record<string, boolean>',
' }',
'}',
'export {}'
].join('\n')
})
addComponent({
name: 'NuxtPage',
priority: 10, // built-in that we do not expect the user to override
@ -464,7 +475,7 @@ export default defineNuxtModule({
addTypeTemplate({
filename: 'types/middleware.d.ts',
getContents: ({ nuxt, app }: { nuxt: Nuxt, app: NuxtApp }) => {
getContents: ({ nuxt, app }) => {
const composablesFile = relative(join(nuxt.options.buildDir, 'types'), resolve(runtimeDir, 'composables'))
const namedMiddleware = app.middleware.filter(mw => !mw.global)
return [

View File

@ -19,6 +19,12 @@ describe('auto-imports', () => {
// @ts-expect-error Should show error on unknown properties
unknownProp: ''
})
defineNuxtConfig({
routeRules: {
// Should accept any string
'/named': { appMiddleware: 'named' }
}
})
})
it('core composables', () => {
ref()