UNPKG

4.96 kBTypeScriptView Raw
1import { GlobWithOptions } from './list-modules';
2import { LoadModulesOptions } from './load-modules';
3import { Resolver, Constructor, BuildResolverOptions } from './resolvers';
4import { InjectionModeType } from './injection-mode';
5/**
6 * The container returned from createContainer has some methods and properties.
7 * @interface AwilixContainer
8 */
9export interface AwilixContainer<Cradle extends object = any> {
10 /**
11 * Options the container was configured with.
12 */
13 options: ContainerOptions;
14 /**
15 * The proxy injected when using `PROXY` injection mode.
16 * Can be used as-is.
17 */
18 readonly cradle: Cradle;
19 /**
20 * Getter for the rolled up registrations that merges the container family tree.
21 */
22 readonly registrations: RegistrationHash;
23 /**
24 * Resolved modules cache.
25 */
26 readonly cache: Map<string | symbol, CacheEntry>;
27 /**
28 * Creates a scoped container with this one as the parent.
29 */
30 createScope<T extends object = {}>(): AwilixContainer<Cradle & T>;
31 /**
32 * Used by `util.inspect`.
33 */
34 inspect(depth: number, opts?: any): string;
35 /**
36 * Binds `lib/loadModules` to this container, and provides
37 * real implementations of it's dependencies.
38 *
39 * Additionally, any modules using the `dependsOn` API
40 * will be resolved.
41 *
42 * @see src/load-modules.ts documentation.
43 */
44 loadModules<ESM extends boolean = false>(globPatterns: Array<string | GlobWithOptions>, options?: LoadModulesOptions<ESM>): ESM extends false ? this : Promise<this>;
45 /**
46 * Adds a single registration that using a pre-constructed resolver.
47 */
48 register<T>(name: string | symbol, registration: Resolver<T>): this;
49 /**
50 * Pairs resolvers to registration names and registers them.
51 */
52 register(nameAndRegistrationPair: NameAndRegistrationPair<Cradle>): this;
53 /**
54 * Resolves the registration with the given name.
55 *
56 * @param {string} name
57 * The name of the registration to resolve.
58 *
59 * @return {*}
60 * Whatever was resolved.
61 */
62 resolve<T>(name: string | symbol, resolveOptions?: ResolveOptions): T;
63 /**
64 * Checks if the registration with the given name exists.
65 *
66 * @param {string | symbol} name
67 * The name of the registration to resolve.
68 *
69 * @return {boolean}
70 * Whether or not the registration exists.
71 */
72 has(name: string | symbol): boolean;
73 /**
74 * Given a resolver, class or function, builds it up and returns it.
75 * Does not cache it, this means that any lifetime configured in case of passing
76 * a resolver will not be used.
77 *
78 * @param {Resolver|Class|Function} targetOrResolver
79 * @param {ResolverOptions} opts
80 */
81 build<T>(targetOrResolver: ClassOrFunctionReturning<T> | Resolver<T>, opts?: BuildResolverOptions<T>): T;
82 /**
83 * Disposes this container and it's children, calling the disposer
84 * on all disposable registrations and clearing the cache.
85 * Only applies to registrations with `SCOPED` or `SINGLETON` lifetime.
86 */
87 dispose(): Promise<void>;
88}
89/**
90 * Optional resolve options.
91 */
92export interface ResolveOptions {
93 /**
94 * If `true` and `resolve` cannot find the requested dependency,
95 * returns `undefined` rather than throwing an error.
96 */
97 allowUnregistered?: boolean;
98}
99/**
100 * Cache entry.
101 */
102export interface CacheEntry<T = any> {
103 /**
104 * The resolver that resolved the value.
105 */
106 resolver: Resolver<T>;
107 /**
108 * The resolved value.
109 */
110 value: T;
111}
112/**
113 * Register a Registration
114 * @interface NameAndRegistrationPair
115 */
116export declare type NameAndRegistrationPair<T> = {
117 [U in keyof T]?: Resolver<T[U]>;
118};
119/**
120 * Function that returns T.
121 */
122export declare type FunctionReturning<T> = (...args: Array<any>) => T;
123/**
124 * A class or function returning T.
125 */
126export declare type ClassOrFunctionReturning<T> = FunctionReturning<T> | Constructor<T>;
127/**
128 * The options for the createContainer function.
129 * @interface ContainerOptions
130 */
131export interface ContainerOptions {
132 require?: (id: string) => any;
133 injectionMode?: InjectionModeType;
134}
135/**
136 * Contains a hash of registrations where the name is the key.
137 */
138export declare type RegistrationHash = Record<string | symbol | number, Resolver<any>>;
139/**
140 * Resolution stack.
141 */
142export interface ResolutionStack extends Array<string | symbol> {
143}
144/**
145 * Creates an Awilix container instance.
146 *
147 * @param {Function} options.require
148 * The require function to use. Defaults to require.
149 *
150 * @param {string} options.injectionMode
151 * The mode used by the container to resolve dependencies. Defaults to 'Proxy'.
152 *
153 * @return {object}
154 * The container.
155 */
156export declare function createContainer<T extends object = any, U extends object = any>(options?: ContainerOptions, parentContainer?: AwilixContainer<U>): AwilixContainer<T>;