You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wrote a simple hono.js middleware to integrate di-wise into your routing!
import{createMiddleware}from'hono/factory';import{Token}from'di-wise';// your custom global containerimport{container}from'@/container';typeExtractType<T>=TextendsToken<infer U> ? U : never;exportconstinject=<T,KextendsRecord<string,Token<T>>>(tokens: K)=>{returncreateMiddleware<{Variables: {[keyinkeyofK]: ExtractType<K[key]>}}>(async(ctx,next)=>{for(const[key,token]ofObject.entries(tokens)){// @ts-ignorectx.set(key,container.resolve(token));}returnnext();});}
usage
import{Hono}from'hono';import{tokens}from'@/container';import{inject}from'@/middleware';constapp=newHono();app.get('/login',inject({userRepo: tokens.UserRepository}),async(ctx)=>{ctx.var.userRepo.findByUsername(...)});// or you can make it a global middleware!app.use(inject({userRepo: tokens.UserRepository}));
If you have any suggestions/improvements to make, please share them!
The text was updated successfully, but these errors were encountered:
I wrote a simple hono.js middleware to integrate
di-wise
into your routing!usage
If you have any suggestions/improvements to make, please share them!
The text was updated successfully, but these errors were encountered: