import { type Ctor } from './metadata'; /** * The lifecycle scope of a resolved bean. * * @remarks * - `'singleton'` — one shared instance, cached for the container's lifetime (the default). * - `'transient'` — a fresh instance on every resolve. * - `'request'` — one instance per HTTP request, injected as a proxy that resolves the * current request's cached instance (so it works even inside a longer-lived singleton). */ export type Scope = 'singleton' | 'transient' | 'request'; /** * A token for a non-class dependency (an interface, a config value, a service * with several implementations). Bind it with `container.register(TOKEN, ...)` * and read it with `inject(TOKEN)`. * * ```ts * const LOGGER = new InjectionToken("Logger"); * ``` */ export declare class InjectionToken { /** Human-readable label for this token, shown in error messages and `toString`. */ readonly description: string; /** Phantom field, erased at runtime — carries the resolved type `T` for `inject`. */ readonly _type: T; /** * Create a token. * * @param description - human-readable label for the token, surfaced in error messages and `toString`. */ constructor( /** Human-readable label for this token, shown in error messages and `toString`. */ description: string); /** * Render as `InjectionToken`, for logs and error messages. * * @returns the token formatted as `InjectionToken`. */ toString(): string; } /** Anything that can be resolved: a class, or an {@link InjectionToken}. */ export type Token = Ctor | InjectionToken; /** How a token is provided when resolved. */ export type Provider = { useValue: T; } | { useClass: Ctor; scope?: Scope; } | { useFactory: (container: Container) => T; scope?: Scope; } | { useExisting: Token; }; /** A provider paired with the token it provides (for `createApp({ providers })`). */ export type ProviderDef = { provide: Token; } & Provider; /** * Inspects each freshly constructed class instance and returns the instance to * use — the same object, or a wrapper (e.g. a `Proxy`). This is the seam that * method-level AOP (advice) is built on. Processors chain in registration order. */ export type PostProcessor = (instance: object, token: Ctor) => object; /** * A dependency-injection container. * * A class token auto-constructs (`container.resolve(UserService)` runs * `new UserService()`, wiring any `inject()` in its field initializers). Bind a * token to a value/class/factory/alias with `register()`; the last registration * for a token wins for `resolve()`, while `resolveAll()` returns them all. */ export declare class Container { private readonly classSingletons; private readonly factorySingletons; private readonly providers; private readonly resolving; private readonly initPromises; private readonly disposables; private readonly postProcessors; /** * Register a hook that can wrap/replace each constructed instance. * * @param processor - hook run on each freshly constructed instance; returns the object to use in its place. */ addPostProcessor(processor: PostProcessor): this; /** * Run `fn` with this container active, so `inject()` works inside it. * * @typeParam T - the value `fn` produces. * @param fn - the function to run with this container as the active injection context. * @returns whatever `fn` returns. */ runInContext(fn: () => T): T; /** * Bind `provider` to `token`. Repeated calls stack (see `resolveAll`). * * @typeParam T - the type the token resolves to. * @param token - the token to bind. * @param provider - how the token is provided (value/class/factory/alias). */ register(token: Token, provider: Provider): this; /** * Resolve a token. A registered provider wins; otherwise a class is constructed. * * @typeParam T - the type the token resolves to. * @param token - the token to resolve. * @returns the instance from the winning (last-registered) provider, or a freshly constructed one for an unbound class token; throws for an unbound {@link InjectionToken}. */ resolve(token: Token): T; /** * Resolve every provider bound to a token (for multi-injection). * * @typeParam T - the type the token resolves to. * @param token - the token to resolve. * @returns one instance per registered binding (empty when none is bound and the token is not a class). */ resolveAll(token: Token): T[]; /** * Resolve a token, or return `fallback` if an InjectionToken is unbound. * * @typeParam T - the type the token resolves to. * @param token - the token to resolve. * @param fallback - returned only when an unbound {@link InjectionToken} is resolved; a class token is still constructed, so the fallback never applies to it. * @returns the resolved instance, or `fallback`. */ resolveOptional(token: Token, fallback: T): T; private fromProvider; private construct; /** A proxy delegating to the current request's instance of a request-scoped bean. */ private requestScopedProxy; /** The current request's instance of a request-scoped bean (built + cached once). */ private currentRequestInstance; /** * Run `@postConstruct` and track `@preDestroy`. App-level tracking (awaiting * async init at bootstrap, disposing at shutdown) applies only to singletons — * transient/request beans are short-lived, so tracking them would leak. */ private runLifecycle; /** Await all async `@postConstruct` hooks run so far (called by `createApp`). */ init(): Promise; /** Run every `@preDestroy` hook in reverse construction order (called by `app.stop`). */ dispose(): Promise; } /** * Resolve a dependency from the container currently constructing this object. * Call it in a field initializer or constructor of a class the container * instantiates (an `@injectable` service or a `@controller`). * * ```ts * class UserController { * private users = inject(UserService); * private logger = inject(LOGGER); // an InjectionToken * } * ``` * * @typeParam T - the type the token resolves to. * @param token - the dependency to resolve. * @returns the resolved instance. */ export declare function inject(token: Token): T; /** * Like {@link inject}, but resolves every provider bound to a token. * * @typeParam T - the type the token resolves to. * @param token - the dependency to resolve. * @returns one instance per registered binding. */ export declare function injectAll(token: Token): T[]; /** * Like {@link inject}, but returns `fallback` when an InjectionToken is unbound. * * @typeParam T - the type the token resolves to. * @param token - the dependency to resolve. * @param fallback - returned only when an unbound {@link InjectionToken} is resolved; a class token is still constructed, so the fallback never applies to it. * @returns the resolved instance, or `fallback`. */ export declare function injectOptional(token: Token, fallback: T): T; /** * Mark a class as injectable, optionally setting its scope (default singleton). * * @param options - optional settings; `scope` selects the lifecycle scope (default `'singleton'`). */ export declare function injectable(options?: { scope?: Scope; }): (_value: Ctor, context: ClassDecoratorContext) => void; /** Stereotype alias for `@injectable` — marks a service-layer component. */ export declare const service: typeof injectable; /** * Stereotype for a persistence/DAO component: injectable, and **transactional by * default** — every instance method runs inside the bound `TransactionManager` * (committing on success, rolling back on throw), so a repository is a unit of * work without annotating each method. When no manager is bound there is no * transaction to run and methods pass through unchanged (staying synchronous); * once one is bound, methods run in it and return promises. Use `@service` or * `@injectable` for a non-transactional component. * * @param options - optional settings; `scope` selects the lifecycle scope (default `'singleton'`). */ export declare function repository(options?: { scope?: Scope; }): (value: Ctor, context: ClassDecoratorContext) => void; /** * Method decorator: run this method right after the container constructs the * instance (once field initializers have run). Sync hooks run inline; an async * hook on a **singleton** is awaited at bootstrap via `container.init()` (which * `createApp` calls) — on a transient/request bean it is invoked but not awaited. * Use it for per-service setup (open a pool, warm a cache). * * @param _value - the decorated method (unused; the hook is keyed by name). * @param context - the standard method-decorator context, whose `name` records the hook. */ export declare function postConstruct(_value: unknown, context: ClassMethodDecoratorContext): void; /** * Method decorator: run this method when the app is stopped (`app.stop()` calls * `container.dispose()`), in reverse construction order. Use it to release * resources (close connections, flush buffers). * * @param _value - the decorated method (unused; the hook is keyed by name). * @param context - the standard method-decorator context, whose `name` records the hook. */ export declare function preDestroy(_value: unknown, context: ClassMethodDecoratorContext): void; //# sourceMappingURL=di.d.ts.map