UNPKG

4.58 kBTypeScriptView Raw
1import { LifetimeType } from './lifetime';
2import { InjectionModeType } from './injection-mode';
3import { AwilixContainer, FunctionReturning } from './container';
4/**
5 * RESOLVER symbol can be used by modules loaded by
6 * `loadModules` to configure their lifetime, injection mode, etc.
7 */
8export declare const RESOLVER: unique symbol;
9/**
10 * Gets passed the container and is expected to return an object
11 * whose properties are accessible at construction time for the
12 * configured resolver.
13 *
14 * @type {Function}
15 */
16export declare type InjectorFunction = <T extends object>(container: AwilixContainer<T>) => object;
17/**
18 * A resolver object returned by asClass(), asFunction() or asValue().
19 */
20export interface Resolver<T> extends ResolverOptions<T> {
21 resolve<U extends object>(container: AwilixContainer<U>): T;
22}
23/**
24 * A resolver object created by asClass() or asFunction().
25 */
26export interface BuildResolver<T> extends Resolver<T>, BuildResolverOptions<T> {
27 injectionMode?: InjectionModeType;
28 injector?: InjectorFunction;
29 setLifetime(lifetime: LifetimeType): this;
30 setInjectionMode(mode: InjectionModeType): this;
31 singleton(): this;
32 scoped(): this;
33 transient(): this;
34 proxy(): this;
35 classic(): this;
36 inject(injector: InjectorFunction): this;
37}
38/**
39 * Options for disposable resolvers.
40 */
41export interface DisposableResolverOptions<T> extends ResolverOptions<T> {
42 dispose?: Disposer<T>;
43}
44/**
45 * Disposable resolver.
46 */
47export interface DisposableResolver<T> extends Resolver<T>, DisposableResolverOptions<T> {
48 disposer(dispose: Disposer<T>): this;
49}
50/**
51 * Disposer function type.
52 */
53export declare type Disposer<T> = (value: T) => any | Promise<any>;
54/**
55 * The options when registering a class, function or value.
56 * @type RegistrationOptions
57 */
58export interface ResolverOptions<T> {
59 /**
60 * Only used for inline configuration with `loadModules`.
61 */
62 name?: string;
63 /**
64 * Lifetime setting.
65 */
66 lifetime?: LifetimeType;
67 /**
68 * Registration function to use. Only used for inline configuration with `loadModules`.
69 */
70 register?: (...args: any[]) => Resolver<T>;
71}
72/**
73 * Builder resolver options.
74 */
75export interface BuildResolverOptions<T> extends ResolverOptions<T>, DisposableResolverOptions<T> {
76 /**
77 * Resolution mode.
78 */
79 injectionMode?: InjectionModeType;
80 /**
81 * Injector function to provide additional parameters.
82 */
83 injector?: InjectorFunction;
84}
85/**
86 * A class constructor. For example:
87 *
88 * class MyClass {}
89 *
90 * container.registerClass('myClass', MyClass)
91 * ^^^^^^^
92 */
93export declare type Constructor<T> = {
94 new (...args: any[]): T;
95};
96/**
97 * Creates a simple value resolver where the given value will always be resolved.
98 *
99 * @param {string} name
100 * The name to register the value as.
101 *
102 * @param {*} value
103 * The value to resolve.
104 *
105 * @return {object}
106 * The resolver.
107 */
108export declare function asValue<T>(value: T): Resolver<T>;
109/**
110 * Creates a factory resolver, where the given factory function
111 * will be invoked with `new` when requested.
112 *
113 * @param {string} name
114 * The name to register the value as.
115 *
116 * @param {Function} fn
117 * The function to register.
118 *
119 * @param {object} opts
120 * Additional options for the resolver.
121 *
122 * @return {object}
123 * The resolver.
124 */
125export declare function asFunction<T>(fn: FunctionReturning<T>, opts?: BuildResolverOptions<T>): BuildResolver<T> & DisposableResolver<T>;
126/**
127 * Like a factory resolver, but for classes that require `new`.
128 *
129 * @param {string} name
130 * The name to register the value as.
131 *
132 * @param {Class} Type
133 * The function to register.
134 *
135 * @param {object} opts
136 * Additional options for the resolver.
137 *
138 * @return {object}
139 * The resolver.
140 */
141export declare function asClass<T = {}>(Type: Constructor<T>, opts?: BuildResolverOptions<T>): BuildResolver<T> & DisposableResolver<T>;
142/**
143 * Resolves to the specified registration.
144 */
145export declare function aliasTo<T>(name: string): Resolver<T>;
146/**
147 * Given an options object, creates a fluid interface
148 * to manage it.
149 *
150 * @param {*} obj
151 * The object to return.
152 *
153 * @return {object}
154 * The interface.
155 */
156export declare function createBuildResolver<T, B extends Resolver<T>>(obj: B): BuildResolver<T> & B;
157/**
158 * Given a resolver, returns an object with methods to manage the disposer
159 * function.
160 * @param obj
161 */
162export declare function createDisposableResolver<T, B extends Resolver<T>>(obj: B): DisposableResolver<T> & B;