Skip to content

Commit

Permalink
fix(nuxt): provide appMiddleware types with universal router
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 16, 2024
1 parent 4925670 commit 87c0678
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/nuxt/src/pages/module.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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 [
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/minimal-types/types.ts
Expand Up @@ -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()
Expand Down

0 comments on commit 87c0678

Please sign in to comment.