-
Notifications
You must be signed in to change notification settings - Fork 13
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
[Feature Request] Add support for "global" middlewares #35
Comments
I think this would be a great feature to add considering that it would and reduce a lot of repetitive code result in better DX. |
Thank you for filing a feature request, @R1D3R175. A while ago, I decided against implementing such a feature, but I will reconsider this decision in the near future. Meanwhile, I encourage everyone interested in this to upvote the head issue so I can gauge the demand for this feature. I might favor the implementation to not create a separate // routes/api/database
...
export const get = () => {}
export const put = () => {}
export const middlewares = [
isLogged, hasPermissions
] But I am open for a discussion. |
Example ScenarioSuppose we have a Twitter-like app that has the following endpoints:
In our Twitter-like app, an user has to be logged in for seeing other posts in their feed, so we would use the following middlewares to protect the interested routes: Because of that, we remove the ConsiderationIf we view the routes as a tree, it's quite obvious that the parent middlewares gets "bound" before the child middleware. But what if we want to make an exception? ConclusionWhat I'm saying could be quite obvious or kinda stupid, however, IMHO these are the things that could introduce the most bugs if not well-defined beforehand. |
Thank you for the insight @R1D3R175. I think it takes some more clarification for me. My idea of a named middlewares export would be that those middlewares would only apply to the method exports inside that route file, not for sibling or child routes. For example: // routes/api/database.ts or routes/api/database/index.ts
export const get = () => {}
export const post = () => {}
export const middlewares = [isLogged, canViewDatabase] // routes/api/database/user.ts
export const get = () => {}
export const put = [canUpdateUser, () => {}]
export const middlewares = [isLogged, canViewUser] The named middlewares export in this concept would only apply these middlewares to the method exports defined in that route file. So the example would be equivalent to the following snippet, where the middlewares are called in their order/priority of definition: app.get("/api/database", isLogged, canViewDatabase, () => {})
app.post("/api/database", isLogged, canViewDatabase, () => {})
app.get("/api/database/user", isLogged, canViewUser, () => {})
app.put("/api/database/user", isLogged, canViewUser, canUpdateUser, () = {}) In this concept, the middlewares export is just a shorthand for applying middlewares to all method exports, so it's not a truly global middleware. For a truly global middleware that also matches all its subsequent child routes, the idea of a dedicated file feels like the right way. |
Sounds good to me @matthiaaas. Thanks for considering this feature request. At this point I think we got everything all settled (i.e. both global and per route middleware boundings), IMHO a good starting point could be updating the docs accordingly and see if things make sense; if they do, it's a matter of implementing the feature according to what's written in the docs (something TDD, except it's docs driven). |
As per #11, it would be nice to have the equivalent of the following (snippet of an hypothetical
index.ts
)via some files/folder structure
Why?
Right now, here's how my
index.ts
looks like:It works correctly, however, what if I rename the
database
folder? The middleware won't be registered anymore.IMHO this shouldn't be happening since one of the main purpose of file routing should be hassle-free route managing.
Possible implementation
Like there is
index.ts
(i.e.src/routes/database/index.ts
would correspond to/api/database
according to my example), there could be amiddleware.ts
with content similar to anHandler
matching all methods:The text was updated successfully, but these errors were encountered: