UNPKG

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