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

docs(routing): add note about middleware returns #1884

Merged
merged 2 commits into from
Nov 6, 2023
Merged

Conversation

oof2win2
Copy link
Contributor

@oof2win2 oof2win2 commented Nov 4, 2023

Better describe middleware and how returning within it behaves

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 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)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This improves on documentation. Before it stated that they should not return anything, which is untrue - returning from middleware returns the value as a response.

πŸ“ Checklist

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

Better describe middleware and how returning within it behaves
Copy link
Contributor

nuxt-studio bot commented Nov 4, 2023

βœ… Live Preview ready!

Name Edit Preview Latest Commit
nitro Edit on Studio β†—οΈŽ View Live Preview 9ebb232

@pi0 pi0 changed the title docs(routing): Note on middleware returns docs(routing): add note about middleware returns Nov 6, 2023
Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

Thanks!

@pi0 pi0 merged commit c5cfca1 into nitrojs:main Nov 6, 2023
4 checks passed
@oof2win2
Copy link
Contributor Author

oof2win2 commented Nov 6, 2023

Why is this not recommended to do? Unauthenticated reauests can be ended before they reach other handlers with this.

@pi0
Copy link
Member

pi0 commented Nov 6, 2023

Yes, also this breaks the nitro request handling lifecycle (i am planning also to introduce defineMiddlewareHandler in h3 that essentially disallows return value as a convention)

@oof2win2
Copy link
Contributor Author

oof2win2 commented Nov 6, 2023

Oh okay. Is there any method that would allow for this, to just directly respond to unauthenticated requests with 401s in one place?

@pi0
Copy link
Member

pi0 commented Nov 6, 2023

You can throw an error like throw createError({ statusCode: 401 }) this will still preserve lifecycle and totally valid from a middleware or any composable. (if feel missing in docs, PR welcome to add this note ❀️ )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants