import { Container, ContainerBuilder, type Finalizer, type IContainer, type IContainerBuilder, type ResolutionContext, ScopedContainer, ScopedContainerBuilder, } from './container.js'; import { UnusedDependencyResolvedError } from './errors.js'; import { captureDefinitionSite, type DefinitionSite, type LayerNode, LayerNodeKey, type LeafNode, resolveProviders, } from './layer-graph.js'; import type { Root } from './requirements.js'; import { AnyTag, ServiceTag, Tag, TagType, ValueTag } from './tag.js'; import { Contravariant, Covariant } from './types.js'; /** * Replaces the TTags type parameter in a container type with a new type. * Preserves the concrete container type (Container, ScopedContainer, or IContainer). * @internal */ export type WithContainerTags< TContainer, TNewTags extends AnyTag, > = TContainer extends ScopedContainer ? ScopedContainer : TContainer extends Container ? Container : IContainer; /** * Replaces the TTags type parameter in a builder type with a new type. * Preserves the concrete builder type (ContainerBuilder or ScopedContainerBuilder). * @internal */ export type WithBuilderTags< TBuilder, TNewTags extends AnyTag, > = TBuilder extends ScopedContainerBuilder ? ScopedContainerBuilder : TBuilder extends ContainerBuilder ? ContainerBuilder : IContainerBuilder; /** * Defines what constitutes a valid dependency for a given parameter type T. * A valid dependency is either: * - A ServiceTag (class) whose non-null instances are assignable to T * - A ValueTag whose value type is assignable to T * - A raw value of type T * * The conditional uses `[NonNullable] extends [object]` (tuple-wrapped) * to prevent distribution over union types while still allowing * `ServiceTag>` for nullable service params (e.g. * `Cache | null`). Without the tuple wrap, a parameter typed as a string * union (e.g. `'SANDBOX' | 'PRODUCTION'`) would distribute into * `ValueTag | ValueTag`, and a * `ValueTag` would not be assignable to * either branch (ValueTag is invariant in its value parameter). Without * the `NonNullable` stripping, a nullable class param would skip the * object branch entirely and reject `ServiceTag` dependencies. * @internal */ /* eslint-disable @typescript-eslint/no-explicit-any */ type ValidDepFor = | ([NonNullable] extends [object] ? ServiceTag> | ValueTag : ValueTag) | T; /* eslint-enable @typescript-eslint/no-explicit-any */ /** * Maps constructor parameters to valid dependency types. * Each parameter type T becomes ValidDepFor. * @internal */ type ValidDepsFor = { readonly [K in keyof TParams]: ValidDepFor; }; /** * Extracts only the tags from a dependency array (filters out raw values). * Used to determine layer requirements. * @internal */ type ExtractTags = { [K in keyof T]: T[K] extends AnyTag ? T[K] : never; }[number]; /** * The most generic layer type that accepts any concrete layer. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export type AnyLayer = Layer; /** * The type ID for the Layer interface. */ export const LayerTypeId: unique symbol = Symbol.for('sandly/Layer'); /** * Helper type that extracts the union of all requirements from an array of layers. * @internal */ type UnionOfRequires = { // eslint-disable-next-line @typescript-eslint/no-explicit-any [K in keyof T]: T[K] extends Layer ? R : never; }[number]; /** * Helper type that extracts the union of all provisions from an array of layers. * @internal */ type UnionOfProvides = { // eslint-disable-next-line @typescript-eslint/no-explicit-any [K in keyof T]: T[K] extends Layer ? P : never; }[number]; /** * A dependency layer represents a reusable, composable unit of dependency registrations. * Layers allow you to organize your dependency injection setup into logical groups * that can be combined and reused across different contexts. * * ## Type Variance * * - **TRequires (covariant)**: A layer requiring fewer dependencies can substitute one requiring more * - **TProvides (contravariant)**: A layer providing more services can substitute one providing fewer * * ## Build Semantics * * Building a container flattens the layer into one provider per tag, so the * result does not depend on composition order: * * - A layer object reached through several composition paths is applied once. * - Two different layers providing the same tag throw {@link DuplicateProviderError}. * - {@link Layer.override} replaces a provider deliberately, wherever it sits. * - A layer containing a {@link Layer.singleton} builds only into a root container. * * @template TRequires - The union of tags this layer requires * @template TProvides - The union of tags this layer provides * * @example * ```typescript * // Create layers using Layer.service(), Layer.value(), or Layer.create() * const dbLayer = Layer.service(Database, []); * const userLayer = Layer.service(UserService, [Database]); * * // Compose layers * const appLayer = userLayer.provide(dbLayer); * * // Create container from layer * const container = Container.from(appLayer); * ``` */ export interface Layer { readonly [LayerTypeId]?: { readonly _TRequires: Covariant; readonly _TProvides: Contravariant; }; /** * The layer's node in the composition graph. * @internal */ readonly [LayerNodeKey]: LayerNode; /** * Registers this layer's providers, one per tag, on a container builder. * Works with both ContainerBuilder and ScopedContainerBuilder. * * @throws {SingletonInChildScopeError} If the builder creates a child scope and the layer contains a singleton * @throws {DuplicateProviderError} If two different layers provide the same tag, or the builder already has one of the tags * @throws {DuplicateOverrideError} If two different overrides replace the same tag * @throws {OverrideTargetNotFoundError} If an override replaces a tag that no other layer provides */ apply: >( builder: TBuilder ) => WithBuilderTags; /** * Provides a dependency layer to this layer, creating a pipeline. * The result only exposes this layer's provisions (not the dependency's). * * Requirements satisfied internally (by either layer's provisions) are * subtracted from the result's requirements. * * @example * ```typescript * const appLayer = apiLayer * .provide(serviceLayer) * .provide(databaseLayer); * ``` */ provide: ( dependency: Layer ) => Layer< Exclude, TProvides >; /** * Merges this layer with another layer, exposing both layers' provisions. * * Requirements satisfied internally (by either layer's provisions) are * subtracted from the result's requirements. This means a merge of two * layers where one provides what the other requires produces a layer with * those requirements already satisfied. * * @example * ```typescript * // Independent layers * const infraLayer = persistenceLayer.merge(loggingLayer); * * // Self-satisfying merge: dbLayer requires Config, configLayer provides it * const fullInfra = dbLayer.merge(configLayer); * // Result: requires nothing, provides Database | Config * ``` */ merge: ( other: Layer ) => Layer< Exclude, TProvides | TOtherProvides >; /** * Replaces the providers of the replacement's tags anywhere in this * layer's composition, regardless of nesting depth or position in the chain. * * Only the replacement's exposed provisions replace anything; the layers it * `provide()`s internally are ordinary providers. * * The result keeps this layer's requirements, including those of the * providers being replaced. Satisfy requirements that the replacement made * unreachable with {@link Layer.unused}. * * Building throws {@link OverrideTargetNotFoundError} if nothing else in the * build provides an overridden tag, and {@link DuplicateOverrideError} if two * different overrides replace the same tag. * * @example * ```typescript * const testLayer = appLayer * .override(Layer.value(PaymentGateway, fakeGateway)) * .provide(Layer.unused(PaymentGatewayConfig)); * ``` */ override: < TReplacementRequires extends AnyTag, TReplacementProvides extends AnyTag, >( replacement: Layer ) => Layer, TProvides>; } /** * Creates a layer around a composition graph node. * * Type parameters are not checked against the node; callers state them. * @internal */ function createLayer( node: LayerNode ): Layer { const layer: Layer = { [LayerNodeKey]: node, apply(builder) { const childScope = builder instanceof ScopedContainerBuilder && builder._isChildScope; let current: IContainerBuilder = builder; for (const provider of resolveProviders(layer, { childScope })) { current = current.add(provider.tag, provider.spec); } // Builders return themselves from add(); only the tag type changes. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return return current as any; }, provide(dependency) { return createLayer({ kind: 'provide', target: layer, dependency }); }, merge(other) { return createLayer({ kind: 'merge', layers: [layer, other] }); }, override(replacement) { return createLayer({ kind: 'override', base: layer, replacement }); }, }; return layer; } /** * Captures the site that called the given `Layer` factory. * @internal */ function definitionSiteOf(factory: keyof typeof Layer): DefinitionSite { return captureDefinitionSite(Layer[factory]); } /** * Creates a leaf layer that registers its providers itself. * @internal */ function createLeafLayer( leaf: Omit ): Layer { return createLayer({ kind: 'leaf', ...leaf }); } /** * Creates a leaf layer that binds a tag to a value. Bindings of one tag to * `Object.is`-equal values count as one provider. * @internal */ function valueLayer( tag: T, value: unknown, site: DefinitionSite ): Layer { // Values are checked against the tag by the public signatures; `Layer.mock` // binds partial implementations. const registeredTag: AnyTag = tag; return createLeafLayer({ site, binding: { value }, register: (builder) => { builder.add(registeredTag, () => value); }, }); } /** * `[tag, value]` pairs, each value checked against its tag's type. * @internal */ type ValueBindings = { readonly [K in keyof TTags]: readonly [ tag: TTags[K], value: TagType, ]; }; /** * Registers a class constructed from its dependency list: tags are resolved, * raw values are passed through. * @internal */ function serviceRegistration( cls: TClass, deps: readonly unknown[], options: { cleanup?: Finalizer> } | undefined ): LeafNode['register'] { return (builder) => { builder.add(cls, { create: async ( ctx: ResolutionContext ): Promise> => { // Resolve tags, pass through raw values const args = await Promise.all( deps.map((dep) => (Tag.isTag(dep) ? ctx.resolve(dep) : dep)) ); return new cls(...args) as InstanceType; }, cleanup: options?.cleanup, }); }; } /** * Creates a layer for a dependency that must have exactly one instance per * application, such as a connection pool, a client holding a cache, or an SDK * wrapper. Takes the same arguments as `Layer.service()`. * * The layer carries a {@link Root} requirement that only `Container.from` and * `ScopedContainer.from` satisfy, so any composition that contains it builds * only into a root container. Passing it to `childFrom` is a compile error, * and building it into a child scope throws {@link SingletonInChildScopeError}. * Layers built in child scopes depend on the singleton's tag and resolve the * root's instance. * * @example * ```typescript * const database = Layer.singleton(Database, [DatabaseConfig], { * cleanup: (db) => db.close(), * }); * // Type: Layer, typeof Database> * * const app = ScopedContainer.from('app', database.provide(configLayer)); * const request = app.childFrom('request', userService); // requires Database from the parent * ``` */ function singleton< TClass extends ServiceTag, const TDeps extends readonly unknown[], >( cls: TClass, deps: TDeps & ValidDepsFor>, options?: { cleanup?: Finalizer> } ): Layer | Root, TClass>; /** * Marks an existing layer, typically one from `Layer.create()`, as a * singleton: the layer and any composition containing it build only into a * root container. * * The wrapped layer itself is unchanged; using it unwrapped elsewhere in the * same build is not a conflict. * * @example * ```typescript * const redisClient = Layer.singleton( * Layer.create({ * requires: [RedisConfig], * apply: (builder) => * builder.add(RedisClient, async (ctx) => connect(await ctx.resolve(RedisConfig))), * }) * ); * // Type: Layer, typeof RedisClient> * ``` */ function singleton( layer: Layer ): Layer, TProvides>; function singleton( source: ServiceTag | AnyLayer, deps?: readonly unknown[], options?: { cleanup?: Finalizer } ): Layer { const site = definitionSiteOf('singleton'); const layer = typeof source === 'function' ? createLeafLayer({ site, register: serviceRegistration(source, deps ?? [], options), }) : source; return createLayer({ kind: 'singleton', layer, site }); } /** * Consolidated Layer API for creating and composing dependency layers. * * @example * ```typescript * // Define services * class Database { * query(sql: string) { return []; } * } * * class UserService { * constructor(private db: Database) {} * getUsers() { return this.db.query('SELECT * FROM users'); } * } * * // Create layers * const dbLayer = Layer.service(Database, []); * const userLayer = Layer.service(UserService, [Database]); * * // Compose and create container * const appLayer = userLayer.provide(dbLayer); * const container = Container.from(appLayer); * * const users = await container.resolve(UserService); * ``` */ export const Layer = { /** * Creates a layer that provides a class service with automatic dependency injection. * * The dependencies array must match the constructor parameters exactly (order and types). * This is validated at compile time. * * @param cls - The service class * @param deps - Array of dependencies (tags or raw values) matching constructor params * @param options - Optional cleanup function for the service * * @example * ```typescript * class UserService { * constructor(private db: Database, private apiKey: string) {} * } * * const ApiKeyTag = Tag.of('apiKey')(); * * // Dependencies must match constructor: (Database, string) * const userLayer = Layer.service(UserService, [Database, ApiKeyTag]); * * // Also works with raw values * const userLayer2 = Layer.service(UserService, [Database, 'my-api-key']); * ``` */ service( cls: TClass, deps: TDeps & ValidDepsFor>, options?: { cleanup?: Finalizer> } ): Layer, TClass> { return createLeafLayer({ site: definitionSiteOf('service'), register: serviceRegistration(cls, deps, options), }); }, singleton, /** * Creates a layer that provides a constant value or pre-instantiated instance. * * Works with both ValueTags (for constants) and ServiceTags (for pre-instantiated instances, useful in tests). * * @param tag - The tag (ValueTag or ServiceTag) to register * @param value - The value or instance to provide * * @example ValueTag (constant) * ```typescript * const ApiKeyTag = Tag.of('apiKey')(); * const ConfigTag = Tag.of('config')<{ port: number }>(); * * const configLayer = Layer.value(ApiKeyTag, 'secret-key') * .merge(Layer.value(ConfigTag, { port: 3000 })); * ``` * * @example ServiceTag (pre-instantiated instance, useful for testing) * ```typescript * class UserService { * getUsers() { return []; } * } * * const mockUserService = new UserService(); * const testLayer = Layer.value(UserService, mockUserService); * ``` */ value(tag: T, value: TagType): Layer { return valueLayer(tag, value, definitionSiteOf('value')); }, /** * Creates a layer that binds several tags to constant values or * pre-instantiated instances. Equivalent to merging one `Layer.value()` * per pair. * * Each value is checked against its own tag's type. * * @param bindings - `[tag, value]` pairs * * @example * ```typescript * const configLayer = Layer.values( * [DatabaseConfig, config.database], * [RegionTag, config.region], * [Logger, logger] * ); * // Type: Layer * ``` */ values( ...bindings: ValueBindings ): Layer { const site = definitionSiteOf('values'); return createLayer({ kind: 'merge', layers: bindings.map(([tag, value]) => valueLayer(tag, value, site) ), }); }, /** * Creates a layer with a mock implementation for testing. * * Similar to `Layer.value()`, but allows partial implementations for ServiceTags, * making it easier to create test mocks without satisfying constructor dependencies. * * **Use this for testing only.** For production code, use `Layer.value()` or `Layer.service()`. * * @param tag - The tag (ServiceTag or ValueTag) to register * @param implementation - The mock implementation (can be partial for ServiceTags) * * @example ServiceTag with partial mock * ```typescript * class UserService { * constructor(private db: Database) {} * getUsers() { return this.db.query('SELECT * FROM users'); } * } * * // Mock only the methods you need - no need to satisfy constructor * const testLayer = Layer.mock(UserService, { * getUsers: () => Promise.resolve([{ id: 1, name: 'Alice' }]) * }); * ``` * * @example ServiceTag with full mock instance * ```typescript * const mockUserService = { * getUsers: () => Promise.resolve([]) * } as UserService; * * const testLayer = Layer.mock(UserService, mockUserService); * ``` * * @example ValueTag (works same as Layer.value) * ```typescript * const ConfigTag = Tag.of('config')<{ port: number }>(); * const testLayer = Layer.mock(ConfigTag, { port: 3000 }); * ``` */ mock( tag: T, implementation: T extends ServiceTag ? Partial> | TagType : TagType ): Layer { return valueLayer(tag, implementation, definitionSiteOf('mock')); }, /** * Creates a custom layer with full control over the factory logic. * * Use this when you need custom instantiation logic that can't be expressed * with `Layer.service()` or `Layer.value()`. * * - `TRequires` is inferred from the `requires` array * - `TProvides` is inferred from what `apply` adds to the builder * * @param options.requires - Array of tags this layer requires (use [] for no requirements) * @param options.apply - Function that adds registrations to a builder * * @example * ```typescript * // Layer with dependencies - TProvides inferred from builder.add() * const cacheLayer = Layer.create({ * requires: [Database], * apply: (builder) => builder * .add(Cache, async (ctx) => { * const db = await ctx.resolve(Database); * return new Cache(db, { ttl: 3600 }); * }) * }); * // Type: Layer * * // Layer with no dependencies * const dbLayer = Layer.create({ * requires: [], * apply: (builder) => builder.add(Database, () => new Database()) * }); * // Type: Layer * ``` */ create< const TRequires extends readonly AnyTag[], TAllTags extends AnyTag, >(options: { requires: TRequires; apply: ( builder: IContainerBuilder ) => IContainerBuilder; }): Layer> { return createLeafLayer({ site: definitionSiteOf('create'), register: (builder) => { options.apply(builder); }, }); }, /** * Creates an empty layer with no requirements or provisions. * * @example * ```typescript * const baseLayer = Layer.empty() * .merge(configLayer) * .merge(serviceLayer); * ``` */ empty(): Layer { return createLayer({ kind: 'merge', layers: [] }); }, /** * Provides placeholders for tags that are required by type but never * resolved, typically the dependencies of a provider replaced with * `layer.override()`. * * Resolving a placeholder fails with {@link UnusedDependencyResolvedError} * (as the root cause of a `DependencyCreationError`). * * @param tags - The tags to satisfy with placeholders * * @example * ```typescript * // ExchangeRateService normally requires ExchangeRateConfig * const testLayer = pricingLayer * .override(Layer.value(ExchangeRateService, fakeRates)) * .provide(Layer.unused(ExchangeRateConfig)); * ``` */ unused( ...tags: TTags ): Layer { return createLeafLayer({ site: definitionSiteOf('unused'), register: (builder) => { for (const tag of tags) { builder.add(tag, () => { throw new UnusedDependencyResolvedError(tag); }); } }, }); }, /** * Merges multiple layers at once. * * Requirements satisfied internally (by any merged layer's provisions) * are subtracted from the result's requirements. * * @param layers - At least 2 layers to merge * @returns A layer combining all provisions, with internally-satisfied requirements removed * * @example * ```typescript * const infraLayer = Layer.mergeAll( * persistenceLayer, * messagingLayer, * observabilityLayer * ); * ``` */ mergeAll( ...layers: T ): Layer< Exclude, UnionOfProvides>, UnionOfProvides > { return createLayer({ kind: 'merge', layers }); }, /** * Merges exactly two layers. * Equivalent to `layer1.merge(layer2)`. */ merge< TRequires1 extends AnyTag, TProvides1 extends AnyTag, TRequires2 extends AnyTag, TProvides2 extends AnyTag, >( layer1: Layer, layer2: Layer ): Layer< Exclude, TProvides1 | TProvides2 > { return layer1.merge(layer2); }, };