UNPKG

hono-simple-di

Version:

A small, type-safe DI library optimized for hono.js.

55 lines (50 loc) 1.56 kB
import { Context, MiddlewareHandler } from 'hono'; type Scope = "default" | "request"; /** T | Promise<T> */ type MaybePromise<T> = T | Promise<T>; /** * A Dependency class used for injecting services into hono.js context. */ declare class Dependency<Service> { /** A function to initialize the service. */ private serviceInitializer; private opts?; constructor( /** A function to initialize the service. */ serviceInitializer: (c: Context) => MaybePromise<Service>, opts?: { /** * The scope of the dependency. * @default 'default' * @remarks * - 'default': Service will be initialized only once. * - 'request': Service is initialized once per request and reused across requests. */ scope?: Scope; } | undefined); private serviceInjected?; private serviceCached?; private lastRequest?; /** * Injects a service instance directly. Useful for overriding the default service. */ injection(service: Service): this; /** * Clear injected service. */ clearInjected(): this; /** * Resolve service. */ resolve(c: Context): Promise<Service>; /** * Creates a middleware that injects the service into the context. */ middleware<ContextKey extends string>( /** The key used to store the service in the context. */ contextKey: ContextKey): MiddlewareHandler<{ Variables: { [key in ContextKey]: Service; }; }>; } export { Dependency, type Scope };