Extending the Context type
So, I've made a function to work to with
@arrirpc/schema
(yes, it's a validator), so I can use it like this The "problem" is that it feels kinda weird, having to pass in the context that way. It does't really feel like the rest of the hono stuff (respondWith(c, schema, data)
vs c.respondWith(schema, data)
or even better, as a middleware, but I don't know how to do that on hono)
Is there a way I can make it better user experience or do I need to create a validator middleware there?19 Replies
hard to say exactly without knowing how
respondWith
is implemented
but hono's createMiddleware
helper might do the trickwhatever tf this is
don't ask how I did this, I have no idea
That's the problemSchema thingy, btw, don't also ask how I did it
yeah, just use
createMidleware
But that wouldn't rly work though, as I have no control over the data at the middleware level
id I were to do middlewares, I'd need to somehow validate after sending stuff, dunno
and if I did that, the types would kinda need to match, but then I'd have no context (thx javascript)
ah, right, it's a handler, my bad
same principle though (higher order function)
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
can we get back to prototype stuff, I could just do
Context.prototype.validated = (data) => ...
:dit's just simple
what happens if I forget the
.use("*", responseWithMiddleware())
line? bc that's 100% gonna happenusing
c.set
/c.get
is probably preferred
then your tests will failno idea what that is and I don't really care about best practices rn
what tests :d
jk, but then I don't really thing I'd like that
I'll try to do a middleware later, something like the
zValidator
I've seen, dunno
I hate ts types though, dunno how I'm making that workyou can just use this, it's only register the typings when you import the file,.. i think, if you didn't use the middleware it makes the
ctx.respondWith
undefinednono, ts makes it global, so even if I don't have that line registers it, it'll still think it's there
and the types are gonna be messed up
anyways, that's a future me problem
welp, just don't forget to use it,. if you get undefined then you've forgot to use the middleware
put the middleware at the top level, so it will call before anything else
that's a future prod problem, lol
and, i think chained type on middleware isn't possible?
yeah. if you use
c.set
(and correctly type + chain the middleware) then downstream handlers will only be able to (get typed) access the stored value if the middleware exists on the route