import { ClassConstructor, InjectableId, Injector } from './injector'; /** * Type definition for functions that return a value. * The function should return a valid value, but may throw an exception if it cannot. */ export type SyncFactory = (injector: Injector) => T; /** * Type definition for functions that return a Promise for a value. * The function *must* not throw and must return a valid Promise (e.g. pending, resolved, rejected). */ export type AsyncFactory = (injector: Injector) => Promise; /** * You may bind an error handler which will be invoked if the bound InjectableId could not be put into service. * An error handler *must* not throw, but may return an Error that will be propagated back up the call chain. * * @param injector The Injector that experienced the error. * @param id The identifier for what was trying to be made. * @param maker The thing that made (or tried to make) the value. Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding. * @param error Identifies the problem that occurred. * @param value If the 'maker' was able to create the thing, but it had an error during post construction, the made thing will be passed here. * @returns one of 3 results... * A substitute thing (kind of like a 'maker' do-over) which must be fully operational (e.g. any `@PostConstruct` will be ignored). * An alternate Error which will be propagated back up the call chain. * Undefined, which means the 'error' parameter will be propagated back up the call chain. */ export type OnErrorCallback = (injector: Injector, id: InjectableId, maker: M, error: unknown, value?: T) => T | Error | void; /** * You may bind a success handler which will be invoked just before the bound InjectableId is put into service. * This is an alternative to the more preferred `@PostConstruct` decorator for scenarios when usage of that decorator is not feasible. * WARNING: * By registering a success handler, you override and nullify any `@PostConstruct` decorator on the class. * In such a scenario, the success handler should perform whatever care and feeding the class expected from the `@PostConstruct` decorator. * A success handler *must* not throw, but may return an Error that will be propagated back up the call chain. * * @param value The thing that was made. * @param injector The Injector that performed the construction. * @param id The identifier for what was made. * @param maker The thing that made. Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding. * @returns one of 3 results... * An Error which will be propagated back up the call chain. * Undefined, which means the object is ready to be placed into service. * A Promise that resolves to one of the above two values (undefined or Error). */ export type OnSuccessCallback = (value: T, injector: Injector, id: InjectableId, maker: M) => Promise | Error | void; /** * Descriptor object used with {@link Container.register} to specify how an id should be bound. * Mirrors the TSyringe registration API. */ export type RegisterDescriptor = { useClass: ClassConstructor; } | { useValue: T; } | { useFactory: SyncFactory; } | { useAsyncFactory: AsyncFactory; }; /** * An interface allowing binding of an error handler. * * @see OnErrorCallback */ export interface BindErrHandler { onError(cb: OnErrorCallback): void; } /** * An interface allowing binding of a post construction handler. * * @see OnSuccessCallback */ export interface BindHandler extends BindErrHandler { onSuccess(cb: OnSuccessCallback): BindErrHandler; } /** * @inheritDoc * This specialization also allows you to specify that the binding is 'Singleton' (e.g. only one in the system). */ export interface BindAs extends BindHandler { asSingleton(): BindHandler; }