1 | import { FactoryType } from "../utils/factory_type";
|
2 | declare namespace interfaces {
|
3 | export type DynamicValue<T> = (context: interfaces.Context) => T | Promise<T>;
|
4 | export type ContainerResolution<T> = T | Promise<T> | (T | Promise<T>)[];
|
5 | type AsyncCallback<TCallback> = TCallback extends (...args: infer TArgs) => infer TResult ? (...args: TArgs) => Promise<TResult> : never;
|
6 | export type BindingScope = "Singleton" | "Transient" | "Request";
|
7 | export type BindingType = "ConstantValue" | "Constructor" | "DynamicValue" | "Factory" | "Function" | "Instance" | "Invalid" | "Provider";
|
8 | export type TargetType = "ConstructorArgument" | "ClassProperty" | "Variable";
|
9 | export interface BindingScopeEnum {
|
10 | Request: interfaces.BindingScope;
|
11 | Singleton: interfaces.BindingScope;
|
12 | Transient: interfaces.BindingScope;
|
13 | }
|
14 | export interface BindingTypeEnum {
|
15 | ConstantValue: interfaces.BindingType;
|
16 | Constructor: interfaces.BindingType;
|
17 | DynamicValue: interfaces.BindingType;
|
18 | Factory: interfaces.BindingType;
|
19 | Function: interfaces.BindingType;
|
20 | Instance: interfaces.BindingType;
|
21 | Invalid: interfaces.BindingType;
|
22 | Provider: interfaces.BindingType;
|
23 | }
|
24 | export interface TargetTypeEnum {
|
25 | ConstructorArgument: interfaces.TargetType;
|
26 | ClassProperty: interfaces.TargetType;
|
27 | Variable: interfaces.TargetType;
|
28 | }
|
29 | export type Newable<T> = new (...args: never[]) => T;
|
30 | export interface Abstract<T> {
|
31 | prototype: T;
|
32 | }
|
33 | export type ServiceIdentifier<T = unknown> = (string | symbol | Newable<T> | Abstract<T>);
|
34 | export interface Clonable<T> {
|
35 | clone(): T;
|
36 | }
|
37 | export type BindingActivation<T> = (context: interfaces.Context, injectable: T) => T | Promise<T>;
|
38 | export type BindingDeactivation<T> = (injectable: T) => void | Promise<void>;
|
39 | export interface Binding<TActivated> extends Clonable<Binding<TActivated>> {
|
40 | id: number;
|
41 | moduleId: ContainerModuleBase["id"];
|
42 | activated: boolean;
|
43 | serviceIdentifier: ServiceIdentifier<TActivated>;
|
44 | constraint: ConstraintFunction;
|
45 | dynamicValue: DynamicValue<TActivated> | null;
|
46 | scope: BindingScope;
|
47 | type: BindingType;
|
48 | implementationType: Newable<TActivated> | TActivated | null;
|
49 | factory: FactoryCreator<unknown> | null;
|
50 | provider: ProviderCreator<unknown> | null;
|
51 | onActivation: BindingActivation<TActivated> | null;
|
52 | onDeactivation: BindingDeactivation<TActivated> | null;
|
53 | cache: null | TActivated | Promise<TActivated>;
|
54 | }
|
55 | export type SimpleFactory<T, U extends unknown[] = unknown[]> = (...args: U) => T;
|
56 | export type MultiFactory<T, U extends unknown[] = unknown[], V extends unknown[] = unknown[]> = (...args: U) => SimpleFactory<T, V>;
|
57 | export type Factory<T, U extends unknown[] = unknown[], V extends unknown[] = unknown[]> = SimpleFactory<T, U> | MultiFactory<T, U, V>;
|
58 | export type FactoryCreator<T, U extends unknown[] = unknown[], V extends unknown[] = unknown[]> = (context: Context) => Factory<T, U, V>;
|
59 | export type AutoNamedFactory<T> = SimpleFactory<T, [string]>;
|
60 | export type AutoFactory<T> = SimpleFactory<T, []>;
|
61 | export type FactoryTypeFunction<T = unknown> = (context: interfaces.Context) => T | Promise<T>;
|
62 | export interface FactoryDetails {
|
63 | factoryType: FactoryType;
|
64 | factory: FactoryTypeFunction | null;
|
65 | }
|
66 | export type Provider<T> = (...args: any[]) => (((...args: any[]) => Promise<T>) | Promise<T>);
|
67 | export type ProviderCreator<T> = (context: Context) => Provider<T>;
|
68 | export interface NextArgs<T = unknown> {
|
69 | avoidConstraints: boolean;
|
70 | contextInterceptor: ((contexts: Context) => Context);
|
71 | isMultiInject: boolean;
|
72 | targetType: TargetType;
|
73 | serviceIdentifier: interfaces.ServiceIdentifier<T>;
|
74 | key?: string | number | symbol;
|
75 | value?: unknown;
|
76 | }
|
77 | export type Next = (args: NextArgs) => (any | any[]);
|
78 | export type Middleware = (next: Next) => Next;
|
79 | export type ContextInterceptor = (context: interfaces.Context) => interfaces.Context;
|
80 | export interface Context {
|
81 | id: number;
|
82 | container: Container;
|
83 | plan: Plan;
|
84 | currentRequest: Request;
|
85 | addPlan(plan: Plan): void;
|
86 | setCurrentRequest(request: Request): void;
|
87 | }
|
88 | export type MetadataOrMetadataArray = Metadata | Metadata[];
|
89 | export interface Metadata<TValue = unknown> {
|
90 | key: string | number | symbol;
|
91 | value: TValue;
|
92 | }
|
93 | export interface Plan {
|
94 | parentContext: Context;
|
95 | rootRequest: Request;
|
96 | }
|
97 | export interface QueryableString {
|
98 | startsWith(searchString: string): boolean;
|
99 | endsWith(searchString: string): boolean;
|
100 | contains(searchString: string): boolean;
|
101 | equals(compareString: string): boolean;
|
102 | value(): string;
|
103 | }
|
104 | export type ResolveRequestHandler = (request: interfaces.Request) => unknown;
|
105 | export type RequestScope = Map<unknown, unknown>;
|
106 | export interface Request {
|
107 | id: number;
|
108 | serviceIdentifier: ServiceIdentifier;
|
109 | parentContext: Context;
|
110 | parentRequest: Request | null;
|
111 | childRequests: Request[];
|
112 | target: Target;
|
113 | bindings: Binding<unknown>[];
|
114 | requestScope: RequestScope | null;
|
115 | addChildRequest(serviceIdentifier: ServiceIdentifier, bindings: (Binding<unknown> | Binding<unknown>[]), target: Target): Request;
|
116 | }
|
117 | export interface Target {
|
118 | id: number;
|
119 | serviceIdentifier: ServiceIdentifier;
|
120 | type: TargetType;
|
121 | name: QueryableString;
|
122 | identifier: string | symbol;
|
123 | metadata: Metadata[];
|
124 | getNamedTag(): interfaces.Metadata<string> | null;
|
125 | getCustomTags(): interfaces.Metadata[] | null;
|
126 | hasTag(key: string | number | symbol): boolean;
|
127 | isArray(): boolean;
|
128 | matchesArray(name: interfaces.ServiceIdentifier): boolean;
|
129 | isNamed(): boolean;
|
130 | isTagged(): boolean;
|
131 | isOptional(): boolean;
|
132 | matchesNamedTag(name: string): boolean;
|
133 | matchesTag(key: string | number | symbol): (value: unknown) => boolean;
|
134 | }
|
135 | export interface ContainerOptions {
|
136 | autoBindInjectable?: boolean;
|
137 | defaultScope?: BindingScope;
|
138 | skipBaseClassChecks?: boolean;
|
139 | }
|
140 | export interface Container {
|
141 | id: number;
|
142 | parent: Container | null;
|
143 | options: ContainerOptions;
|
144 | bind<T>(serviceIdentifier: ServiceIdentifier<T>): BindingToSyntax<T>;
|
145 | rebind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T>;
|
146 | rebindAsync<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): Promise<interfaces.BindingToSyntax<T>>;
|
147 | unbind(serviceIdentifier: ServiceIdentifier): void;
|
148 | unbindAsync(serviceIdentifier: interfaces.ServiceIdentifier): Promise<void>;
|
149 | unbindAll(): void;
|
150 | unbindAllAsync(): Promise<void>;
|
151 | isBound(serviceIdentifier: ServiceIdentifier): boolean;
|
152 | isCurrentBound<T>(serviceIdentifier: ServiceIdentifier<T>): boolean;
|
153 | isBoundNamed(serviceIdentifier: ServiceIdentifier, named: string | number | symbol): boolean;
|
154 | isBoundTagged(serviceIdentifier: ServiceIdentifier, key: string | number | symbol, value: unknown): boolean;
|
155 | get<T>(serviceIdentifier: ServiceIdentifier<T>): T;
|
156 | getNamed<T>(serviceIdentifier: ServiceIdentifier<T>, named: string | number | symbol): T;
|
157 | getTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T;
|
158 | getAll<T>(serviceIdentifier: ServiceIdentifier<T>): T[];
|
159 | getAllTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string | number | symbol, value: unknown): T[];
|
160 | getAllNamed<T>(serviceIdentifier: ServiceIdentifier<T>, named: string | number | symbol): T[];
|
161 | getAsync<T>(serviceIdentifier: ServiceIdentifier<T>): Promise<T>;
|
162 | getNamedAsync<T>(serviceIdentifier: ServiceIdentifier<T>, named: string | number | symbol): Promise<T>;
|
163 | getTaggedAsync<T>(serviceIdentifier: ServiceIdentifier<T>, key: string | number | symbol, value: unknown): Promise<T>;
|
164 | getAllAsync<T>(serviceIdentifier: ServiceIdentifier<T>): Promise<T[]>;
|
165 | getAllTaggedAsync<T>(serviceIdentifier: ServiceIdentifier<T>, key: string | number | symbol, value: unknown): Promise<T[]>;
|
166 | getAllNamedAsync<T>(serviceIdentifier: ServiceIdentifier<T>, named: string | number | symbol): Promise<T[]>;
|
167 | onActivation<T>(serviceIdentifier: ServiceIdentifier<T>, onActivation: BindingActivation<T>): void;
|
168 | onDeactivation<T>(serviceIdentifier: ServiceIdentifier<T>, onDeactivation: BindingDeactivation<T>): void;
|
169 | resolve<T>(constructorFunction: interfaces.Newable<T>): T;
|
170 | load(...modules: ContainerModule[]): void;
|
171 | loadAsync(...modules: AsyncContainerModule[]): Promise<void>;
|
172 | unload(...modules: ContainerModuleBase[]): void;
|
173 | unloadAsync(...modules: ContainerModuleBase[]): Promise<void>;
|
174 | applyCustomMetadataReader(metadataReader: MetadataReader): void;
|
175 | applyMiddleware(...middleware: Middleware[]): void;
|
176 | snapshot(): void;
|
177 | restore(): void;
|
178 | createChild(): Container;
|
179 | }
|
180 | export type Bind = <T>(serviceIdentifier: ServiceIdentifier<T>) => BindingToSyntax<T>;
|
181 | export type Rebind = <T>(serviceIdentifier: ServiceIdentifier<T>) => BindingToSyntax<T>;
|
182 | export type Unbind = <T>(serviceIdentifier: ServiceIdentifier<T>) => void;
|
183 | export type UnbindAsync = <T>(serviceIdentifier: ServiceIdentifier<T>) => Promise<void>;
|
184 | export type IsBound = <T>(serviceIdentifier: ServiceIdentifier<T>) => boolean;
|
185 | export interface ContainerModuleBase {
|
186 | id: number;
|
187 | }
|
188 | export interface ContainerModule extends ContainerModuleBase {
|
189 | registry: ContainerModuleCallBack;
|
190 | }
|
191 | export interface AsyncContainerModule extends ContainerModuleBase {
|
192 | registry: AsyncContainerModuleCallBack;
|
193 | }
|
194 | export interface ModuleActivationHandlers {
|
195 | onActivations: Lookup<BindingActivation<unknown>>;
|
196 | onDeactivations: Lookup<BindingDeactivation<unknown>>;
|
197 | }
|
198 | export interface ModuleActivationStore extends Clonable<ModuleActivationStore> {
|
199 | addDeactivation(moduleId: ContainerModuleBase["id"], serviceIdentifier: ServiceIdentifier<unknown>, onDeactivation: interfaces.BindingDeactivation<unknown>): void;
|
200 | addActivation(moduleId: ContainerModuleBase["id"], serviceIdentifier: ServiceIdentifier<unknown>, onActivation: interfaces.BindingActivation<unknown>): void;
|
201 | remove(moduleId: ContainerModuleBase["id"]): ModuleActivationHandlers;
|
202 | }
|
203 | export type ContainerModuleCallBack = (bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind, unbindAsync: interfaces.UnbindAsync, onActivation: interfaces.Container["onActivation"], onDeactivation: interfaces.Container["onDeactivation"]) => void;
|
204 | export type AsyncContainerModuleCallBack = AsyncCallback<ContainerModuleCallBack>;
|
205 | export interface ContainerSnapshot {
|
206 | bindings: Lookup<Binding<unknown>>;
|
207 | activations: Lookup<BindingActivation<unknown>>;
|
208 | deactivations: Lookup<BindingDeactivation<unknown>>;
|
209 | middleware: Next | null;
|
210 | moduleActivationStore: interfaces.ModuleActivationStore;
|
211 | }
|
212 | export interface Lookup<T> extends Clonable<Lookup<T>> {
|
213 | add(serviceIdentifier: ServiceIdentifier, value: T): void;
|
214 | getMap(): Map<interfaces.ServiceIdentifier, T[]>;
|
215 | get(serviceIdentifier: ServiceIdentifier): T[];
|
216 | remove(serviceIdentifier: interfaces.ServiceIdentifier): void;
|
217 | removeByCondition(condition: (item: T) => boolean): T[];
|
218 | removeIntersection(lookup: interfaces.Lookup<T>): void;
|
219 | hasKey(serviceIdentifier: ServiceIdentifier): boolean;
|
220 | clone(): Lookup<T>;
|
221 | traverse(func: (key: interfaces.ServiceIdentifier, value: T[]) => void): void;
|
222 | }
|
223 | export interface BindingOnSyntax<T> {
|
224 | onActivation(fn: (context: Context, injectable: T) => T | Promise<T>): BindingWhenSyntax<T>;
|
225 | onDeactivation(fn: (injectable: T) => void | Promise<void>): BindingWhenSyntax<T>;
|
226 | }
|
227 | export interface BindingWhenSyntax<T> {
|
228 | when(constraint: (request: Request) => boolean): BindingOnSyntax<T>;
|
229 | whenTargetNamed(name: string | number | symbol): BindingOnSyntax<T>;
|
230 | whenTargetIsDefault(): BindingOnSyntax<T>;
|
231 | whenTargetTagged(tag: string | number | symbol, value: unknown): BindingOnSyntax<T>;
|
232 | whenInjectedInto(parent: (NewableFunction | string)): BindingOnSyntax<T>;
|
233 | whenParentNamed(name: string | number | symbol): BindingOnSyntax<T>;
|
234 | whenParentTagged(tag: string | number | symbol, value: unknown): BindingOnSyntax<T>;
|
235 | whenAnyAncestorIs(ancestor: (NewableFunction | string)): BindingOnSyntax<T>;
|
236 | whenNoAncestorIs(ancestor: (NewableFunction | string)): BindingOnSyntax<T>;
|
237 | whenAnyAncestorNamed(name: string | number | symbol): BindingOnSyntax<T>;
|
238 | whenAnyAncestorTagged(tag: string | number | symbol, value: unknown): BindingOnSyntax<T>;
|
239 | whenNoAncestorNamed(name: string | number | symbol): BindingOnSyntax<T>;
|
240 | whenNoAncestorTagged(tag: string | number | symbol, value: unknown): BindingOnSyntax<T>;
|
241 | whenAnyAncestorMatches(constraint: (request: Request) => boolean): BindingOnSyntax<T>;
|
242 | whenNoAncestorMatches(constraint: (request: Request) => boolean): BindingOnSyntax<T>;
|
243 | }
|
244 | export interface BindingWhenOnSyntax<T> extends BindingWhenSyntax<T>, BindingOnSyntax<T> {
|
245 | }
|
246 | export interface BindingInSyntax<T> {
|
247 | inSingletonScope(): BindingWhenOnSyntax<T>;
|
248 | inTransientScope(): BindingWhenOnSyntax<T>;
|
249 | inRequestScope(): BindingWhenOnSyntax<T>;
|
250 | }
|
251 | export interface BindingInWhenOnSyntax<T> extends BindingInSyntax<T>, BindingWhenOnSyntax<T> {
|
252 | }
|
253 | export interface BindingToSyntax<T> {
|
254 | to(constructor: new (...args: never[]) => T): BindingInWhenOnSyntax<T>;
|
255 | toSelf(): BindingInWhenOnSyntax<T>;
|
256 | toConstantValue(value: T): BindingWhenOnSyntax<T>;
|
257 | toDynamicValue(func: DynamicValue<T>): BindingInWhenOnSyntax<T>;
|
258 | toConstructor<T2>(constructor: Newable<T2>): BindingWhenOnSyntax<T>;
|
259 | toFactory<T2, T3 extends unknown[] = unknown[], T4 extends unknown[] = unknown[]>(factory: FactoryCreator<T2, T3, T4>): BindingWhenOnSyntax<T>;
|
260 | toFunction(func: T): BindingWhenOnSyntax<T>;
|
261 | toAutoFactory<T2>(serviceIdentifier: ServiceIdentifier<T2>): BindingWhenOnSyntax<T>;
|
262 | toAutoNamedFactory<T2>(serviceIdentifier: ServiceIdentifier<T2>): BindingWhenOnSyntax<T>;
|
263 | toProvider<T2>(provider: ProviderCreator<T2>): BindingWhenOnSyntax<T>;
|
264 | toService(service: ServiceIdentifier<T>): void;
|
265 | }
|
266 | export interface ConstraintFunction {
|
267 | metaData?: Metadata;
|
268 | (request: Request | null): boolean;
|
269 | }
|
270 | export interface MetadataReader {
|
271 | getConstructorMetadata(constructorFunc: NewableFunction): ConstructorMetadata;
|
272 | getPropertiesMetadata(constructorFunc: NewableFunction): MetadataMap;
|
273 | }
|
274 | export interface MetadataMap {
|
275 | [propertyNameOrArgumentIndex: string | symbol]: Metadata[];
|
276 | }
|
277 | export interface ConstructorMetadata {
|
278 | compilerGeneratedMetadata: NewableFunction[] | undefined;
|
279 | userGeneratedMetadata: MetadataMap;
|
280 | }
|
281 | export {};
|
282 | }
|
283 | export { interfaces };
|