declare type Function = (...args: any[]) => any; export declare type ExtractFunctions = { [P in keyof T]: T[P] extends Function ? P : never; }[keyof T]; /** * Unwraps the promise */ declare type UnWrapPromise = T extends Promise ? U : T; /** * Shape of the bind callback method */ export declare type BindCallback = (container: Container) => ReturnValue; /** * Shape of the fake callback method */ export declare type FakeCallback = (container: Container, originalValue: ReturnValue) => ReturnValue; /** * Shape of resolved lookup node, resolved using `getResolver().resolve()` * method. */ export declare type IocResolverLookupNode = { namespace: Namespace; type: 'binding' | 'alias'; method: string; }; /** * Shape of class constructor */ export declare type Constructor = new (...args: any[]) => T; /** * Shape of class constructor with `makePlain` property */ export declare type PlainConstructor = { makePlain: true; }; /** * Type of the "withBindings" method */ export interface WithBindings { (namespaces: [...Bindings], cb: (...args: { [M in keyof Bindings]: Bindings[M] extends keyof ContainerBindings ? ContainerBindings[Bindings[M]] : any; }) => void): void; (namespaces: readonly [...Namespace], cb: (...args: { [M in keyof Namespace]: Namespace[M] extends keyof ContainerBindings ? ContainerBindings[Namespace[M]] : any; }) => void): void; } /** * Finding return type of the `ioc.make` method based upon the * input argument. * * - String and LookupNode = Returns any * - Class constructor with "makePlain" are returned as it is * - Otherwise an instance of the class constructor is returned * - All other values are returned as it is */ export declare type InferMakeType = T extends string | LookupNode ? any : T extends PlainConstructor ? T : T extends Constructor ? A : T; /** * Shape of lookup node pulled using `ioc.lookup` method. This node * can be passed to `ioc.use`, or `ioc.make` to skip many checks * and resolve the binding right away. */ export declare type LookupNode = { namespace: Namespace; type: 'binding' | 'alias'; }; /** * Ioc container interface */ export interface IocContract { /** * Registered aliases. The key is the alias and value is the * absolute directory path */ importAliases: { [alias: string]: string; }; /** * Enable/disable proxies. Proxies are mainly required for fakes to * work */ useProxies(enable?: boolean): this; /** * Define the module type for resolving auto import aliases. Defaults * to `cjs` */ module: 'cjs' | 'esm'; /** * Register a binding with a callback. The callback return value will be * used when binding is resolved */ bind(binding: Binding, callback: BindCallback): this; bind(binding: Binding, callback: BindCallback): this; /** * Same as the [[bind]] method, but registers a singleton only. Singleton's callback * is invoked only for the first time and then the cached value is used */ singleton(binding: Binding, callback: BindCallback): this; singleton(binding: Binding, callback: BindCallback): this; /** * Define an import alias */ alias(absolutePath: string, alias: string): this; /** * Register a fake for a namespace. Fakes works both for "bindings" and "import aliases". * Fakes only work when proxies are enabled using "useProxies". */ fake(namespace: Namespace, callback: FakeCallback): this; fake(namespace: Namespace, callback: FakeCallback): this; /** * Clear selected or all the fakes. Calling the method with no arguments * will clear all the fakes */ restore(namespace?: Namespace): this; restore(namespace?: string): this; /** * Find if a fake has been registered for a given namespace */ hasFake(namespace: Namespace): boolean; hasFake(namespace: string): boolean; /** * Find if a binding exists for a given namespace */ hasBinding(namespace: Binding): boolean; hasBinding(namespace: string): boolean; /** * Find if a namespace is part of the auto import aliases. Returns false, when namespace * is an alias path but has an explicit binding too */ isAliasPath(namespace: string): boolean; /** * Lookup a namespace. The output contains the complete namespace, * along with its type. The type is an "alias" or a "binding". * * Null is returned when unable to lookup the namespace inside the container * * Note: This method just checks if a namespace is registered or binding * or can be it resolved from auto import aliases or not. However, * it doesn't check for the module existence on the disk. * * Optionally you can define a prefix namespace * to be used to build the complete namespace. For example: * * - namespace: UsersController * - prefixNamespace: App/Controllers/Http * - Output: App/Controllers/Http/UsersController * * Prefix namespace is ignored for absolute namespaces. For example: * * - namespace: /App/UsersController * - prefixNamespace: App/Controllers/Http * - Output: App/UsersController */ lookup>(namespace: Namespace | LookupNode, prefixNamespace?: string): LookupNode; lookup(namespace: Namespace | LookupNode, prefixNamespace?: string): Namespace extends keyof ContainerBindings ? LookupNode : LookupNode | null; /** * Same as [[lookup]]. But raises exception instead of returning null */ lookupOrFail>(namespace: Namespace | LookupNode, prefixNamespace?: string): LookupNode; lookupOrFail(namespace: Namespace | LookupNode, prefixNamespace?: string): Namespace extends keyof ContainerBindings ? LookupNode : LookupNode; /** * Resolve a binding by invoking the binding factory function. An exception * is raised, if the binding namespace is unregistered. */ resolveBinding>(binding: Binding): ContainerBindings[Binding]; resolveBinding(namespace: Binding): Binding extends keyof ContainerBindings ? ContainerBindings[Binding] : any; /** * Import namespace from the auto import aliases. This method assumes you are * using native ES modules */ import(namespace: string): Promise; /** * Same as the "import" method, but uses CJS for requiring the module from its * path */ require(namespace: string): any; /** * The use method looks up a namespace inside both the bindings and the * auto import aliases */ use>(lookupNode: Binding | LookupNode): ContainerBindings[Binding]; use(lookupNode: Binding | LookupNode): Binding extends keyof ContainerBindings ? ContainerBindings[Binding] : any; /** * Same as the [[use]] method, but instead uses ES modules for resolving * the auto import aliases */ useAsync>(lookupNode: Binding | LookupNode): Promise; useAsync(lookupNode: Binding | LookupNode): Promise; /** * Makes an instance of the class by first resolving it. */ make>(lookupNode: Binding | LookupNode, args?: any[]): ContainerBindings[Binding]; make(value: T | LookupNode, args?: any[]): T extends keyof ContainerBindings ? ContainerBindings[T] : InferMakeType; /** * Same as the [[make]] method, but instead uses ES modules for resolving * the auto import aliases */ makeAsync>(lookupNode: Binding | LookupNode, args?: any[]): Promise; makeAsync(value: T | LookupNode, args?: any[]): Promise>; /** * The "withBindings" method invokes the defined callback when it is * able to resolve all the mentioned bindings. */ withBindings: WithBindings; /** * @deprecated: Use "withBindings" instead */ with: WithBindings; /** * Call a method on an object and automatically inject its depdencies */ call>(target: T, method: Method, args?: any[]): T[Method] extends Function ? ReturnType : any; /** * Call a method on an object and automatically inject its depdencies */ callAsync>(target: T, method: Method, args?: any[]): T[Method] extends Function ? Promise>> : Promise; /** * Trap container lookup calls. It includes * * - Ioc.use * - Ioc.useAsync * - Ioc.make * - Ioc.makeAsync * - Ioc.require * - Ioc.import * - Ioc.resolveBinding */ trap(callback: (namespace: string) => any): this; /** * Returns the resolver instance to resolve Ioc container bindings with * little ease. Since, the IocResolver uses an in-memory cache to * improve the lookup speed, we suggest keeping a reference to * the output of this method to leverage caching */ getResolver(fallbackMethod?: string, rcNamespaceKey?: string, fallbackNamespace?: string): IocResolverContract; } /** * IoC resolver allows resolving IoC container bindings by defining * prefix namespaces */ export interface IocResolverContract { /** * Resolve IoC container binding */ resolve>(namespace: Namespace, prefixNamespace?: string): IocResolverLookupNode; resolve(namespace: Namespace, prefixNamespace?: string): Namespace extends keyof ContainerBindings ? IocResolverLookupNode : IocResolverLookupNode; /** * Call method on an IoC container binding */ call>(namespace: Namespace | string, prefixNamespace?: string, args?: any[] | ((instance: any) => any[])): Promise; call>(namespace: IocResolverLookupNode, prefixNamespace: undefined, args?: any[] | ((instance: any) => any[])): Promise; } export {};