You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@hono/tsyringe

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hono/tsyringe

The tsyringe dependency injection middleware for Hono

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
422
66.8%
Maintainers
1
Weekly downloads
 
Created
Source

tsyringe middleware for Hono

The tsyringe middleware provides a way to use dependency injection in Hono.

Usage

import "reflect-metadata" // tsyringe requires reflect-metadata or polyfill
import { container, inject, injectable } from 'tsyringe'
import { tsyringe } from '@hono/tsyringe'
import { Hono } from 'hono'

@injectable()
class Hello {
  constructor(@inject('name') private name: string) {}

  greet() {
    return `Hello, ${this.name}!`
  }
}

const app = new Hono()

app.use('*', tsyringe((container) => {
    container.register('name', { useValue: 'world' })
}))

app.get('/', (c) => {
    const hello = container.resolve(Hello)
    return c.text(hello.greet())
})

export default app

With providers

const app = new Hono()

app.use('/tenant/:name/*', async (c, next) => {
    await tsyringe((container) => {
        // Allowing to inject `c.var` or `c.req.param` in the providers
        const tenantName = c.req.param('name')

        container.register(Config, { useFactory: () => new Config(tenantName) })
    })(c, next)
})

Author

Aotokitsuruya https://github.com/elct9620

License

MIT

FAQs

Package last updated on 18 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts