Skip to content
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

Merged
merged 11 commits into from
Feb 6, 2023
Merged

feat(kit): add addRouteMiddleware method #18553

merged 11 commits into from
Feb 6, 2023

Conversation

xtoolkit
Copy link
Contributor

@xtoolkit xtoolkit commented Jan 26, 2023

πŸ”— Linked issue

resolves #3934

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This method help to modules for add middlewares

addRouteMiddleware({
  name: 'test',
  path: 'path-to-middleware'
})

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link

codesandbox bot commented Jan 26, 2023

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders


export function extendMiddleware (cb: (middleware: NuxtMiddleware[]) => void) {
const nuxt = useNuxt()
nuxt.hook('app:resolve', app => cb(app.middleware))
Copy link
Member

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

Copy link
Contributor Author

@xtoolkit xtoolkit Jan 28, 2023

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.

Copy link
Member

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).

Copy link
Member

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.)

Copy link
Contributor Author

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

Copy link
Member

@danielroe danielroe Feb 5, 2023

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.

Copy link
Contributor Author

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.

Copy link
Member

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 ❀️

packages/kit/src/pages.ts Outdated Show resolved Hide resolved
packages/kit/src/pages.ts Outdated Show resolved Hide resolved
@xtoolkit xtoolkit changed the title feat(kit): add extendMiddleware method feat(kit): add addRouteMiddleware method Feb 5, 2023
packages/kit/src/pages.ts Outdated Show resolved Hide resolved
packages/kit/src/pages.ts Outdated Show resolved Hide resolved
packages/kit/src/pages.ts Outdated Show resolved Hide resolved
packages/kit/src/pages.ts Outdated Show resolved Hide resolved
packages/kit/src/pages.ts Outdated Show resolved Hide resolved
xtoolkit and others added 5 commits February 5, 2023 21:41
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>
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! Thank you.

@danielroe danielroe merged commit 76a08e3 into nuxt:main Feb 6, 2023
@danielroe danielroe mentioned this pull request Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add addRouteMiddleware utility from @nuxt/kit
3 participants