-
I am curious how the library handles Middleware with an async function. your examples here (https://itty.dev/itty-router/middleware) are not async. Usually, we will do something like: const user = await verifyUser(token) in middleware. How does itty handle this? As we don't return anything on the successful verification because the return would end the handler in itty, we just add to the request (i.e. request.user = user). But, won't the router continue to process without actually getting the new information from the user because this is an async? We can't just return user because we have other functions to process after the middleware. Hope this question make sense. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Sounds like I need to address that in the docs (https://github.com/kwhitley/itty.dev/blob/main/src/routes/itty-router/middleware/+page.md)! So true to the claim, these both work equally: // sync middleware
const middleware1 = (request) => {
request.foo = 'bar'
}
// async middleware
const middleware2 = async (request) => {
request.foo = await db.getItems()
} This is because the value of every middleware is simply awaited (because even sync functions can be awaited)... if it is undefined/void, the middleware is assumed to have not returned, and the flow continues to the next handler until that route is exhausted, at which point the router continues to match the next routes. If the value Hope this helps! |
Beta Was this translation helpful? Give feedback.
Sounds like I need to address that in the docs (https://github.com/kwhitley/itty.dev/blob/main/src/routes/itty-router/middleware/+page.md)!
So true to the claim, these both work equally:
This is because the value of every middleware is simply awaited (because even sync functions can be awaited)... if it is undefined/void, the middleware is assumed to have not returned, and the flow continues to the next handler until that route is exhausted, at which point the router continues to match the next routes. If the value
…