fix(nuxt): do not accept arbitrary strings for MiddlewareKey (#28676)

This commit is contained in:
Thimo Sietsma 2024-08-26 21:57:58 +02:00 committed by GitHub
parent 21345c343b
commit 76f3ab400d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 2 deletions

View File

@ -1,16 +1,21 @@
// For pnpm typecheck:docs to generate correct types // For pnpm typecheck:docs to generate correct types
import { addPluginTemplate } from 'nuxt/kit' import { addPluginTemplate, addRouteMiddleware } from 'nuxt/kit'
export default defineNuxtConfig({ export default defineNuxtConfig({
typescript: { shim: process.env.DOCS_TYPECHECK === 'true' }, typescript: { shim: process.env.DOCS_TYPECHECK === 'true' },
pages: process.env.DOCS_TYPECHECK === 'true', pages: process.env.DOCS_TYPECHECK === 'true',
modules: [ modules: [
function () { function () {
if (!process.env.DOCS_TYPECHECK) { return }
addPluginTemplate({ addPluginTemplate({
filename: 'plugins/my-plugin.mjs', filename: 'plugins/my-plugin.mjs',
getContents: () => 'export default defineNuxtPlugin({ name: \'my-plugin\' })', getContents: () => 'export default defineNuxtPlugin({ name: \'my-plugin\' })',
}) })
addRouteMiddleware({
name: 'auth',
path: '#build/auth.js',
})
}, },
], ],
}) })

View File

@ -515,7 +515,7 @@ export default defineNuxtModule({
const namedMiddleware = app.middleware.filter(mw => !mw.global) const namedMiddleware = app.middleware.filter(mw => !mw.global)
return [ return [
'import type { NavigationGuard } from \'vue-router\'', 'import type { NavigationGuard } from \'vue-router\'',
`export type MiddlewareKey = ${namedMiddleware.map(mw => genString(mw.name)).join(' | ') || 'string'}`, `export type MiddlewareKey = ${namedMiddleware.map(mw => genString(mw.name)).join(' | ') || 'never'}`,
`declare module ${genString(composablesFile)} {`, `declare module ${genString(composablesFile)} {`,
' interface PageMeta {', ' interface PageMeta {',
' middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>', ' middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>',