UNPKG

5.06 kBTypeScriptView Raw
1import { AwilixContainer, FunctionReturning } from './container';
2import { InjectionModeType } from './injection-mode';
3import { LifetimeType } from './lifetime';
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 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 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 * True if this resolver should be excluded from lifetime leak checking. Used by resolvers that
73 * wish to uphold the anti-leakage contract themselves. Defaults to false.
74 */
75 isLeakSafe?: boolean;
76}
77/**
78 * Builder resolver options.
79 */
80export interface BuildResolverOptions<T> extends ResolverOptions<T>, DisposableResolverOptions<T> {
81 /**
82 * Resolution mode.
83 */
84 injectionMode?: InjectionModeType;
85 /**
86 * Injector function to provide additional parameters.
87 */
88 injector?: InjectorFunction;
89}
90/**
91 * A class constructor. For example:
92 *
93 * class MyClass {}
94 *
95 * container.registerClass('myClass', MyClass)
96 * ^^^^^^^
97 */
98export type Constructor<T> = {
99 new (...args: any[]): T;
100};
101/**
102 * Creates a simple value resolver where the given value will always be resolved. The value is
103 * marked as leak-safe since in strict mode, the value will only be resolved when it is not leaking
104 * upwards from a child scope to a parent singleton.
105 *
106 * @param {string} name The name to register the value as.
107 *
108 * @param {*} value The value to resolve.
109 *
110 * @return {object} The resolver.
111 */
112export declare function asValue<T>(value: T): Resolver<T>;
113/**
114 * Creates a factory resolver, where the given factory function
115 * will be invoked with `new` when requested.
116 *
117 * @param {string} name
118 * The name to register the value as.
119 *
120 * @param {Function} fn
121 * The function to register.
122 *
123 * @param {object} opts
124 * Additional options for the resolver.
125 *
126 * @return {object}
127 * The resolver.
128 */
129export declare function asFunction<T>(fn: FunctionReturning<T>, opts?: BuildResolverOptions<T>): BuildResolver<T> & DisposableResolver<T>;
130/**
131 * Like a factory resolver, but for classes that require `new`.
132 *
133 * @param {string} name
134 * The name to register the value as.
135 *
136 * @param {Class} Type
137 * The function to register.
138 *
139 * @param {object} opts
140 * Additional options for the resolver.
141 *
142 * @return {object}
143 * The resolver.
144 */
145export declare function asClass<T = object>(Type: Constructor<T>, opts?: BuildResolverOptions<T>): BuildResolver<T> & DisposableResolver<T>;
146/**
147 * Resolves to the specified registration. Marked as leak-safe since the alias target is what should
148 * be checked for lifetime leaks.
149 */
150export declare function aliasTo<T>(name: Parameters<AwilixContainer['resolve']>[0]): Resolver<T>;
151/**
152 * Given an options object, creates a fluid interface
153 * to manage it.
154 *
155 * @param {*} obj
156 * The object to return.
157 *
158 * @return {object}
159 * The interface.
160 */
161export declare function createBuildResolver<T, B extends Resolver<T>>(obj: B): BuildResolver<T> & B;
162/**
163 * Given a resolver, returns an object with methods to manage the disposer
164 * function.
165 * @param obj
166 */
167export declare function createDisposableResolver<T, B extends Resolver<T>>(obj: B): DisposableResolver<T> & B;