-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for global middleware (#938)
- Loading branch information
Showing
7 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
import type { Nuxt, NuxtApp } from '@nuxt/schema' | ||
import { genArrayFromRaw, genImport, genSafeVariableName } from 'knitwork' | ||
import { resolveFiles } from '@nuxt/kit' | ||
import { getNameFromPath, hasSuffix } from './utils/names' | ||
|
||
interface TemplateContext { | ||
nuxt: Nuxt | ||
app: NuxtApp & { templateVars: Record<string, any> } | ||
} | ||
|
||
export const globalMiddlewareTemplate = { | ||
filename: 'global-middleware.mjs', | ||
getContents: async ({ nuxt, app }: TemplateContext) => { | ||
app.middleware = [] | ||
const middlewareDir = nuxt.options.dir.middleware || 'middleware' | ||
const middlewareFiles = await resolveFiles(nuxt.options.srcDir, `${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`) | ||
app.middleware.push(...middlewareFiles.map((file) => { | ||
const name = getNameFromPath(file) | ||
return { name, path: file, global: hasSuffix(file, '.global') } | ||
})) | ||
|
||
const globalMiddleware = app.middleware.filter(mw => mw.global) | ||
|
||
return [ | ||
...globalMiddleware.map(mw => genImport(mw.path, genSafeVariableName(mw.name))), | ||
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => genSafeVariableName(mw.name)))}` | ||
].join('\n') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { basename, dirname, extname, normalize } from 'pathe' | ||
import { kebabCase, pascalCase, splitByCase } from 'scule' | ||
import { withTrailingSlash } from 'ufo' | ||
|
||
export function getNameFromPath (path: string, relativeTo?: string) { | ||
const relativePath = relativeTo | ||
? normalize(path).replace(withTrailingSlash(normalize(relativeTo)), '') | ||
: basename(path) | ||
const prefixParts = splitByCase(dirname(relativePath)) | ||
const fileName = basename(relativePath, extname(relativePath)) | ||
return kebabCase(resolveComponentName(fileName.toLowerCase() === 'index' ? '' : fileName, prefixParts)).replace(/["']/g, '') | ||
} | ||
|
||
export function hasSuffix (path: string, suffix: string) { | ||
return basename(path).replace(extname(path), '').endsWith(suffix) | ||
} | ||
|
||
export function resolveComponentName (fileName: string, prefixParts: string[]) { | ||
/** | ||
* Array of fileName parts splitted by case, / or - | ||
* @example third-component -> ['third', 'component'] | ||
* @example AwesomeComponent -> ['Awesome', 'Component'] | ||
*/ | ||
const fileNameParts = splitByCase(fileName) | ||
const fileNamePartsContent = fileNameParts.join('/').toLowerCase() | ||
const componentNameParts: string[] = [...prefixParts] | ||
let index = prefixParts.length - 1 | ||
const matchedSuffix: string[] = [] | ||
while (index >= 0) { | ||
matchedSuffix.unshift(...splitByCase(prefixParts[index] || '').map(p => p.toLowerCase())) | ||
const matchedSuffixContent = matchedSuffix.join('/') | ||
if ((fileNamePartsContent === matchedSuffixContent || fileNamePartsContent.startsWith(matchedSuffixContent + '/')) || | ||
// e.g Item/Item/Item.vue -> Item | ||
(prefixParts[index].toLowerCase() === fileNamePartsContent && | ||
prefixParts[index + 1] && | ||
prefixParts[index] === prefixParts[index + 1])) { | ||
componentNameParts.length = index | ||
} | ||
index-- | ||
} | ||
|
||
return pascalCase(componentNameParts) + pascalCase(fileNameParts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { withoutTrailingSlash } from 'ufo' | ||
|
||
export default defineNuxtRouteMiddleware((to) => { | ||
if (useRequestHeaders(['trailing-slash'])['trailing-slash'] && to.fullPath.endsWith('/')) { | ||
return navigateTo(withoutTrailingSlash(to.fullPath), { redirectCode: 307 }) | ||
} | ||
if (to.path.startsWith('/redirect/')) { | ||
return navigateTo('/navigation-target') | ||
} | ||
if (to.path === '/navigate-to-external') { | ||
return navigateTo('/', { external: true }) | ||
} | ||
if (to.path === '/navigate-to-false') { | ||
return false | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters