UNPKG

5.81 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<K extends keyof Cradle>(name: K, resolveOptions?: ResolveOptions): Cradle[K];
63 /**
64 * Resolves the registration with the given name.
65 *
66 * @param {string} name
67 * The name of the registration to resolve.
68 *
69 * @return {*}
70 * Whatever was resolved.
71 */
72 resolve<T>(name: string | symbol, resolveOptions?: ResolveOptions): T;
73 /**
74 * Checks if the registration with the given name exists.
75 *
76 * @param {string | symbol} name
77 * The name of the registration to resolve.
78 *
79 * @return {boolean}
80 * Whether or not the registration exists.
81 */
82 hasRegistration(name: string | symbol): boolean;
83 /**
84 * Recursively gets a registration by name if it exists in the
85 * current container or any of its' parents.
86 *
87 * @param name {string | symbol} The registration name.
88 */
89 getRegistration<K extends keyof Cradle>(name: K): Resolver<Cradle[K]> | null;
90 /**
91 * Recursively gets a registration by name if it exists in the
92 * current container or any of its' parents.
93 *
94 * @param name {string | symbol} The registration name.
95 */
96 getRegistration<T = unknown>(name: string | symbol): Resolver<T> | null;
97 /**
98 * Given a resolver, class or function, builds it up and returns it.
99 * Does not cache it, this means that any lifetime configured in case of passing
100 * a resolver will not be used.
101 *
102 * @param {Resolver|Class|Function} targetOrResolver
103 * @param {ResolverOptions} opts
104 */
105 build<T>(targetOrResolver: ClassOrFunctionReturning<T> | Resolver<T>, opts?: BuildResolverOptions<T>): T;
106 /**
107 * Disposes this container and it's children, calling the disposer
108 * on all disposable registrations and clearing the cache.
109 * Only applies to registrations with `SCOPED` or `SINGLETON` lifetime.
110 */
111 dispose(): Promise<void>;
112}
113/**
114 * Optional resolve options.
115 */
116export interface ResolveOptions {
117 /**
118 * If `true` and `resolve` cannot find the requested dependency,
119 * returns `undefined` rather than throwing an error.
120 */
121 allowUnregistered?: boolean;
122}
123/**
124 * Cache entry.
125 */
126export interface CacheEntry<T = any> {
127 /**
128 * The resolver that resolved the value.
129 */
130 resolver: Resolver<T>;
131 /**
132 * The resolved value.
133 */
134 value: T;
135}
136/**
137 * Register a Registration
138 * @interface NameAndRegistrationPair
139 */
140export declare type NameAndRegistrationPair<T> = {
141 [U in keyof T]?: Resolver<T[U]>;
142};
143/**
144 * Function that returns T.
145 */
146export declare type FunctionReturning<T> = (...args: Array<any>) => T;
147/**
148 * A class or function returning T.
149 */
150export declare type ClassOrFunctionReturning<T> = FunctionReturning<T> | Constructor<T>;
151/**
152 * The options for the createContainer function.
153 */
154export interface ContainerOptions {
155 require?: (id: string) => any;
156 injectionMode?: InjectionModeType;
157}
158/**
159 * Contains a hash of registrations where the name is the key.
160 */
161export declare type RegistrationHash = Record<string | symbol | number, Resolver<any>>;
162/**
163 * Resolution stack.
164 */
165export interface ResolutionStack extends Array<string | symbol> {
166}
167/**
168 * Creates an Awilix container instance.
169 *
170 * @param {Function} options.require
171 * The require function to use. Defaults to require.
172 *
173 * @param {string} options.injectionMode
174 * The mode used by the container to resolve dependencies. Defaults to 'Proxy'.
175 *
176 * @return {AwilixContainer<T>}
177 * The container.
178 */
179export declare function createContainer<T extends object = any, U extends object = any>(options?: ContainerOptions, parentContainer?: AwilixContainer<U>): AwilixContainer<T>;