UNPKG

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