-
Notifications
You must be signed in to change notification settings - Fork 173
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
feat(tsyringe): add @hono/tsyringe middleware #785
Conversation
🦋 Changeset detectedLatest commit: aced1ba The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
518382c
to
3721f4f
Compare
Hi @elct9620 Thank you for the PR. One thing. It's better not to receive a context in diff --git a/packages/tsyringe/src/index.ts b/packages/tsyringe/src/index.ts
index d883566..1a0c25e 100644
--- a/packages/tsyringe/src/index.ts
+++ b/packages/tsyringe/src/index.ts
@@ -8,11 +8,11 @@ declare module 'hono' {
}
}
-export type Provider = (c: Context, container: DependencyContainer) => void
+export type Provider = (container: DependencyContainer) => void
export const tsyringe = (...providers: Provider[]): MiddlewareHandler => {
return createMiddleware(async (c, next) => {
const childContainer = container.createChildContainer()
- providers.forEach((provider) => provider(c, childContainer))
+ providers.forEach((provider) => provider(childContainer))
c.set('resolve', <T>(token: InjectionToken<T>) => childContainer.resolve(token))
await next()
}) If you want to access the context, you can use callback pattern: app.use('/tenant/:name/*', async (c, next) => {
await tsyringe((container) => {
const tenantName = c.req.param('name')
container.register(Config, { useFactory: () => new Config(tenantName) })
})(c, next)
}) The advantage of this method is it can infer types such as |
3721f4f
to
b40ffac
Compare
Thanks for your feedback. I have updated it. |
b40ffac
to
aced1ba
Compare
aced1ba
to
828f294
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@elct9620 Thank you! Let's go with it. I'll merge this and release it now. |
Fixed in |
Work correctly, thanks. |
hey @elct9620 sorry to post comment after this is merged since i can't find the original repo. @elct9620 maybe you wanna check this project https://github.com/risen228/tsyringe-neo since tsyringe is likely no longer maintained microsoft/tsyringe#248 (comment) |
@yarabramasta thanks for your information. I will follow the updates to decide whether to switch to tsyringe-neo. |
This middleware uses
tsyringe
to provide dependency injection support for Hono and can construct objects withc.var.resolve(MyService)
Example