#Type errors chaining middleware together.

8 messages · Page 1 of 1 (latest)

hidden locust
#

In the docs, it suggests you can chain middleware together while building up state in context. But in this case, the context is not typed unlike the majority of Start because the middleware can be used in different spots.

Maybe we should add an explicit typing for the context that each middleware supports?

Something like createMiddleware().context( z.object( { authUser: AuthUser.optional() } ).passthrough() ).validator(.....).server(...)

So that one can put in compile time guarantees on the context required. I believe it would get rid of the type errors in the middleware functions and also validate you combined multiple middlewares correctly in a server function. Bomb.

FYI: I'm using TanStack/Start+Router v1.120.13.

dusty sinew
#

It's not a type error, it's just a specific example that was included.

#

``

#

const loggingMiddleware = createMiddleware({ type: "function" }) .middleware([awesomeMiddleware]) .server(async ({ next, context }) => { console.log("Is awesome?", context.isAwesome); return next(); });

You have to wrap an additional middleware method before that to be able to get the standard context types

hidden locust
#

That makes sense. So the docs were incorrect then. I get it now. You chain the middleware explicitly in each new middleware.

Thank you!

dusty sinew
#

Your welcome