@adonisjs/fold
Version:
Simplest and straightforward implementation of IoC container in JavaScript
64 lines (63 loc) • 2.91 kB
TypeScript
import type { AbstractConstructor } from '@poppinss/utils/types';
import type { Container } from './container.ts';
import type { BindingCondition, BindingResolver } from './types.ts';
/**
* A fluent builder to register conditional bindings with the
* container.
*
* Bindings registered using this builder are only used when the condition
* returns true. The condition is evaluated on every resolution.
*/
export declare class ConditionalBindingsBuilder<KnownBindings extends Record<any, any>> {
#private;
/**
* Initialize the conditional bindings builder
*
* @param condition - Predicate deciding whether the bindings should be used
* @param container - The container instance to register bindings with
*/
constructor(condition: BindingCondition<KnownBindings>, container: Container<KnownBindings>);
/**
* Register a binding to use when the condition returns true. The factory
* function is called on every resolution.
*
* @param binding - The binding key (string, symbol, or class constructor)
* @param resolver - Factory function that resolves the binding value
*
* @example
* ```ts
* container
* .if((resolver) => resolver.make(FeatureFlags).isEnabled('new-payment'))
* .bind(PaymentGateway, (resolver) => resolver.make(BetaPaymentGateway))
* ```
*/
bind<Binding extends keyof KnownBindings>(
/**
* Need to narrow down the "Binding" for the case where "KnownBindings" are <any, any>
*/
binding: Binding extends string | symbol ? Binding : never, resolver: BindingResolver<KnownBindings, KnownBindings[Binding]>): void;
bind<Binding extends AbstractConstructor<any>>(binding: Binding, resolver: BindingResolver<KnownBindings, InstanceType<Binding>>): void;
/**
* Register a singleton to use when the condition returns true. The factory
* function is called only once and the return value is cached forever.
*
* The condition is still evaluated on every resolution. Only the factory
* function result is cached.
*
* @param binding - The binding key (string, symbol, or class constructor)
* @param resolver - Factory function that resolves the binding value
*
* @example
* ```ts
* container
* .if(() => env.get('SEARCH_DRIVER') === 'typesense')
* .singleton(SearchService, (resolver) => resolver.make(TypesenseSearchService))
* ```
*/
singleton<Binding extends keyof KnownBindings>(
/**
* Need to narrow down the "Binding" for the case where "KnownBindings" are <any, any>
*/
binding: Binding extends string | symbol ? Binding : never, resolver: BindingResolver<KnownBindings, KnownBindings[Binding]>): void;
singleton<Binding extends AbstractConstructor<any>>(binding: Binding, resolver: BindingResolver<KnownBindings, InstanceType<Binding>>): void;
}