1 | import { GlobWithOptions } from './list-modules';
|
2 | import { LoadModulesOptions } from './load-modules';
|
3 | import { Resolver, Constructor, BuildResolverOptions } from './resolvers';
|
4 | import { InjectionModeType } from './injection-mode';
|
5 | /**
|
6 | * The container returned from createContainer has some methods and properties.
|
7 | * @interface AwilixContainer
|
8 | */
|
9 | export 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 | has(name: string | symbol): boolean;
|
83 | /**
|
84 | * Given a resolver, class or function, builds it up and returns it.
|
85 | * Does not cache it, this means that any lifetime configured in case of passing
|
86 | * a resolver will not be used.
|
87 | *
|
88 | * @param {Resolver|Class|Function} targetOrResolver
|
89 | * @param {ResolverOptions} opts
|
90 | */
|
91 | build<T>(targetOrResolver: ClassOrFunctionReturning<T> | Resolver<T>, opts?: BuildResolverOptions<T>): T;
|
92 | /**
|
93 | * Disposes this container and it's children, calling the disposer
|
94 | * on all disposable registrations and clearing the cache.
|
95 | * Only applies to registrations with `SCOPED` or `SINGLETON` lifetime.
|
96 | */
|
97 | dispose(): Promise<void>;
|
98 | }
|
99 | /**
|
100 | * Optional resolve options.
|
101 | */
|
102 | export interface ResolveOptions {
|
103 | /**
|
104 | * If `true` and `resolve` cannot find the requested dependency,
|
105 | * returns `undefined` rather than throwing an error.
|
106 | */
|
107 | allowUnregistered?: boolean;
|
108 | }
|
109 | /**
|
110 | * Cache entry.
|
111 | */
|
112 | export interface CacheEntry<T = any> {
|
113 | /**
|
114 | * The resolver that resolved the value.
|
115 | */
|
116 | resolver: Resolver<T>;
|
117 | /**
|
118 | * The resolved value.
|
119 | */
|
120 | value: T;
|
121 | }
|
122 | /**
|
123 | * Register a Registration
|
124 | * @interface NameAndRegistrationPair
|
125 | */
|
126 | export declare type NameAndRegistrationPair<T> = {
|
127 | [U in keyof T]?: Resolver<T[U]>;
|
128 | };
|
129 | /**
|
130 | * Function that returns T.
|
131 | */
|
132 | export declare type FunctionReturning<T> = (...args: Array<any>) => T;
|
133 | /**
|
134 | * A class or function returning T.
|
135 | */
|
136 | export declare type ClassOrFunctionReturning<T> = FunctionReturning<T> | Constructor<T>;
|
137 | /**
|
138 | * The options for the createContainer function.
|
139 | * @interface ContainerOptions
|
140 | */
|
141 | export interface ContainerOptions {
|
142 | require?: (id: string) => any;
|
143 | injectionMode?: InjectionModeType;
|
144 | }
|
145 | /**
|
146 | * Contains a hash of registrations where the name is the key.
|
147 | */
|
148 | export declare type RegistrationHash = Record<string | symbol | number, Resolver<any>>;
|
149 | /**
|
150 | * Resolution stack.
|
151 | */
|
152 | export interface ResolutionStack extends Array<string | symbol> {
|
153 | }
|
154 | /**
|
155 | * Creates an Awilix container instance.
|
156 | *
|
157 | * @param {Function} options.require
|
158 | * The require function to use. Defaults to require.
|
159 | *
|
160 | * @param {string} options.injectionMode
|
161 | * The mode used by the container to resolve dependencies. Defaults to 'Proxy'.
|
162 | *
|
163 | * @return {object}
|
164 | * The container.
|
165 | */
|
166 | export declare function createContainer<T extends object = any, U extends object = any>(options?: ContainerOptions, parentContainer?: AwilixContainer<U>): AwilixContainer<T>;
|