-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(kit): add addRouteMiddleware
method
#18553
Conversation
Β Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
packages/kit/src/pages.ts
Outdated
|
||
export function extendMiddleware (cb: (middleware: NuxtMiddleware[]) => void) { | ||
const nuxt = useNuxt() | ||
nuxt.hook('app:resolve', app => cb(app.middleware)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to:
- Support input argument being directly an array to inject
- Support returning new middleware from cb and push/normalize/dedup them into
app.middleware
<-- this would be much safer than just exposing an array and let developers push into it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you want like this?
function extendMiddleware (newMiddleware: NuxtMiddleware[] | NuxtMiddleware): void;
function extendMiddleware (cb: (middleware: NuxtMiddleware[]) => NuxtMiddleware[]): void;
function extendMiddleware (input: any) {
const nuxt = useNuxt()
nuxt.hook('app:resolve', (app) => {
if (Array.isArray(input)) {
app.middleware = [...app.middleware, ...input]
} else if(typeof input === 'object') {
app.middleware = [...app.middleware, input]
} else {
app.middleware = [...input(app.middleware)]
}
})
}
we can add option argument for override middleware by name, if you think this is good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my suggestion for the API:
function addRouteMiddleware (input: NuxtMiddleware | NuxtMiddleware[], options: { override: boolean }): void
Under the hood, if override is not set, then we will check existing middleware for any of the same name, and warn the user if so (and not replace the existing middleware).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Feel free to ping me on Discord if I can be of any help.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi again
In addRouteMiddleware
api, module can only add middleware. why not use Adding middleware Dynamically helper.
but in extendMiddleware
api, module can manage all middleware. like delete, sort and etc
an this moment, nuxt core team must decide which one to implement
tnx <3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer addRouteMiddleware
, just as we have addLayout
, addPlugin
, etc but do not add convenient helpers for deleting/sorting layouts and plugins. Modules that need to do more advanced things can directly interact with the hook. Adding route middleware dynamically misses out on features like type support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right.
for advance change better to use hooks.
if you are not comment or change this #18553 (comment) api, i can implement that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be perfect, thank you β€οΈ
extendMiddleware
methodaddRouteMiddleware
method
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Daniel Roe <daniel@roe.dev>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful! Thank you.
π Linked issue
resolves #3934
β Type of change
π Description
This method help to modules for add middlewares
π Checklist