clear-router 2.9.1 → 2.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/CoreRouter.cjs +46 -35
- package/dist/core/CoreRouter.d.cts +7 -4
- package/dist/core/CoreRouter.d.mts +7 -4
- package/dist/core/CoreRouter.mjs +46 -35
- package/dist/core/bindings.cjs +197 -38
- package/dist/core/bindings.d.cts +82 -11
- package/dist/core/bindings.d.mts +82 -11
- package/dist/core/bindings.mjs +191 -38
- package/dist/core/index.cjs +13 -0
- package/dist/core/index.d.cts +2 -1
- package/dist/core/index.d.mts +2 -1
- package/dist/core/index.mjs +2 -1
- package/dist/core/plugins.d.cts +4 -4
- package/dist/core/plugins.d.mts +4 -4
- package/dist/decorators/index.cjs +3 -1
- package/dist/decorators/index.d.cts +2 -2
- package/dist/decorators/index.d.mts +2 -2
- package/dist/decorators/index.mjs +2 -2
- package/dist/decorators/setup.cjs +3 -1
- package/dist/decorators/setup.d.cts +2 -2
- package/dist/decorators/setup.d.mts +2 -2
- package/dist/decorators/setup.mjs +2 -2
- package/dist/index.cjs +13 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/types/basic.d.cts +5 -0
- package/dist/types/basic.d.mts +5 -0
- package/package.json +1 -1
package/dist/core/bindings.cjs
CHANGED
|
@@ -1,54 +1,189 @@
|
|
|
1
1
|
const require_Request = require('./Request.cjs');
|
|
2
2
|
const require_Response = require('./Response.cjs');
|
|
3
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
4
|
|
|
4
5
|
//#region src/core/bindings.ts
|
|
5
6
|
const metadataKey = Symbol.for("clear-router:binding-metadata");
|
|
6
7
|
const bindings = /* @__PURE__ */ new WeakMap();
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.
|
|
8
|
+
var InjectionToken = class {
|
|
9
|
+
key;
|
|
10
|
+
constructor(description) {
|
|
11
|
+
this.description = description;
|
|
12
|
+
this.key = Symbol(description);
|
|
13
|
+
}
|
|
14
|
+
toString() {
|
|
15
|
+
return `InjectionToken(${this.description})`;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var ContainerResolutionError = class extends Error {
|
|
19
|
+
constructor(message, path) {
|
|
20
|
+
super(`${message}\nResolution path: ${path.map(describeToken).join(" -> ")}`);
|
|
21
|
+
this.path = path;
|
|
22
|
+
this.name = "ContainerResolutionError";
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var Container = class Container {
|
|
26
|
+
static activeScope = new node_async_hooks.AsyncLocalStorage();
|
|
27
|
+
static containers = /* @__PURE__ */ new Set();
|
|
28
|
+
static globalContainer = new Container();
|
|
29
|
+
registry = /* @__PURE__ */ new Map();
|
|
30
|
+
instances = /* @__PURE__ */ new Map();
|
|
31
|
+
pending = /* @__PURE__ */ new Map();
|
|
32
|
+
constructor(parent, context, requestScope = false) {
|
|
33
|
+
this.parent = parent;
|
|
34
|
+
this.context = context;
|
|
35
|
+
this.requestScope = requestScope;
|
|
36
|
+
}
|
|
37
|
+
static get global() {
|
|
38
|
+
return this.globalContainer;
|
|
39
|
+
}
|
|
40
|
+
static current() {
|
|
41
|
+
return this.activeScope.getStore() ?? this.globalContainer;
|
|
42
|
+
}
|
|
43
|
+
static run(container, callback) {
|
|
44
|
+
return this.activeScope.run(container, callback);
|
|
45
|
+
}
|
|
46
|
+
static create(parent = this.globalContainer) {
|
|
47
|
+
const container = new Container(parent);
|
|
48
|
+
this.containers.add(container);
|
|
49
|
+
return container;
|
|
50
|
+
}
|
|
51
|
+
createRequestScope(ctx) {
|
|
52
|
+
return new Container(this, ctx, true);
|
|
53
|
+
}
|
|
54
|
+
static bind(token, value, options) {
|
|
55
|
+
this.globalContainer.bind(token, value, options);
|
|
11
56
|
}
|
|
12
57
|
static unbind(token) {
|
|
13
|
-
this.
|
|
58
|
+
this.globalContainer.unbind(token);
|
|
14
59
|
}
|
|
15
60
|
static clear() {
|
|
16
|
-
this.
|
|
61
|
+
this.globalContainer.clear();
|
|
62
|
+
for (const container of this.containers) container.clear();
|
|
17
63
|
}
|
|
18
64
|
static has(token) {
|
|
19
|
-
return this.
|
|
65
|
+
return this.current().has(token);
|
|
20
66
|
}
|
|
21
67
|
static bindings() {
|
|
22
|
-
return
|
|
68
|
+
return this.current().bindings();
|
|
23
69
|
}
|
|
24
70
|
static async resolve(token, ctx, autoDiscover = false) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
71
|
+
return this.current().resolve(token, ctx, autoDiscover);
|
|
72
|
+
}
|
|
73
|
+
bind(token, value, options) {
|
|
74
|
+
this.registry.set(token, normalizeProvider(value, options));
|
|
75
|
+
this.instances.delete(token);
|
|
76
|
+
}
|
|
77
|
+
unbind(token) {
|
|
78
|
+
this.registry.delete(token);
|
|
79
|
+
this.instances.delete(token);
|
|
80
|
+
}
|
|
81
|
+
clear() {
|
|
82
|
+
this.registry.clear();
|
|
83
|
+
this.instances.clear();
|
|
84
|
+
this.pending.clear();
|
|
85
|
+
}
|
|
86
|
+
has(token) {
|
|
87
|
+
return Boolean(this.findProvider(token));
|
|
88
|
+
}
|
|
89
|
+
entries() {
|
|
90
|
+
const entries = /* @__PURE__ */ new Map();
|
|
91
|
+
for (const container of this.lineage().reverse()) for (const [token, provider] of container.registry) entries.set(token, provider);
|
|
92
|
+
return entries;
|
|
93
|
+
}
|
|
94
|
+
bindings() {
|
|
95
|
+
return Object.fromEntries(Array.from(this.entries(), ([token, provider]) => [describeToken(token), provider]));
|
|
96
|
+
}
|
|
97
|
+
async resolve(token, ctx = this.context, autoDiscover = false) {
|
|
98
|
+
return this.resolveToken(token, ctx, {
|
|
99
|
+
autoDiscover,
|
|
100
|
+
stack: []
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
async resolveOrFail(token, ctx = this.context, autoDiscover = false) {
|
|
104
|
+
const resolved = await this.resolve(token, ctx, autoDiscover);
|
|
105
|
+
if (typeof resolved === "undefined") throw new ContainerResolutionError(`Cannot resolve ${describeToken(token)}`, [token]);
|
|
106
|
+
return resolved;
|
|
107
|
+
}
|
|
108
|
+
async resolveToken(token, ctx, state) {
|
|
109
|
+
if (token === require_Request.Request) return ctx?.clearRequest;
|
|
110
|
+
if (token === require_Response.Response) return ctx?.clearResponse;
|
|
111
|
+
if (state.stack.includes(token)) throw new ContainerResolutionError(`Circular dependency detected while resolving ${describeToken(token)}`, [...state.stack, token]);
|
|
112
|
+
const nextState = {
|
|
113
|
+
...state,
|
|
114
|
+
stack: [...state.stack, token]
|
|
115
|
+
};
|
|
116
|
+
const found = this.findProvider(token);
|
|
117
|
+
if (found) return this.resolveProvider(found.token, found.provider, found.owner, ctx, nextState);
|
|
118
|
+
if (state.autoDiscover && typeof token === "function") return this.instantiate(token, void 0, ctx, nextState);
|
|
119
|
+
}
|
|
120
|
+
findProvider(token) {
|
|
121
|
+
if (this.registry.has(token)) return {
|
|
122
|
+
token,
|
|
123
|
+
provider: this.registry.get(token),
|
|
124
|
+
owner: this
|
|
125
|
+
};
|
|
33
126
|
const equivalent = this.findEquivalentToken(token);
|
|
34
|
-
|
|
127
|
+
if (equivalent) return {
|
|
128
|
+
token: equivalent,
|
|
129
|
+
provider: this.registry.get(equivalent),
|
|
130
|
+
owner: this
|
|
131
|
+
};
|
|
132
|
+
return this.parent?.findProvider(token);
|
|
133
|
+
}
|
|
134
|
+
async resolveProvider(token, provider, owner, ctx, state) {
|
|
135
|
+
if ("useExisting" in provider) return this.resolveToken(provider.useExisting, ctx, state);
|
|
136
|
+
const cache = provider.scope === "singleton" ? owner : provider.scope === "request" ? this.findRequestScope() : void 0;
|
|
137
|
+
if (cache?.instances.has(token)) return cache.instances.get(token);
|
|
138
|
+
if (cache?.pending.has(token)) return cache.pending.get(token);
|
|
139
|
+
const create = async () => {
|
|
140
|
+
if ("useValue" in provider) return provider.useValue;
|
|
141
|
+
if ("useClass" in provider) return this.instantiate(provider.useClass, provider.dependencies, ctx, state);
|
|
142
|
+
return provider.useFactory(ctx);
|
|
143
|
+
};
|
|
144
|
+
if (!cache) return create();
|
|
145
|
+
const promise = create();
|
|
146
|
+
cache.pending.set(token, promise);
|
|
147
|
+
try {
|
|
148
|
+
const resolved = await promise;
|
|
149
|
+
if (typeof resolved !== "undefined") cache.instances.set(token, resolved);
|
|
150
|
+
return resolved;
|
|
151
|
+
} finally {
|
|
152
|
+
cache.pending.delete(token);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async instantiate(Type, dependencies, ctx, state) {
|
|
156
|
+
const tokens = dependencies ?? getInjectTokens(Type);
|
|
157
|
+
const args = [];
|
|
158
|
+
for (const dependency of tokens) {
|
|
159
|
+
const resolved = await this.resolveToken(dependency, ctx, state);
|
|
160
|
+
if (typeof resolved === "undefined") throw new ContainerResolutionError(`Cannot resolve ${describeToken(dependency)} required by ${describeToken(Type)}`, [...state.stack, dependency]);
|
|
161
|
+
args.push(resolved);
|
|
162
|
+
}
|
|
163
|
+
return new Type(...args);
|
|
35
164
|
}
|
|
36
|
-
|
|
165
|
+
findRequestScope() {
|
|
166
|
+
if (this.requestScope) return this;
|
|
167
|
+
return this.parent?.findRequestScope();
|
|
168
|
+
}
|
|
169
|
+
lineage() {
|
|
170
|
+
return [this, ...this.parent?.lineage() ?? []];
|
|
171
|
+
}
|
|
172
|
+
findEquivalentToken(token) {
|
|
173
|
+
if (typeof token !== "function") return void 0;
|
|
37
174
|
const name = token.name;
|
|
38
|
-
if (!name) return;
|
|
175
|
+
if (!name) return void 0;
|
|
39
176
|
const tokenParent = Object.getPrototypeOf(token);
|
|
40
177
|
const tokenProps = this.getComparableStaticProps(token);
|
|
41
178
|
for (const registered of this.registry.keys()) {
|
|
42
|
-
if (registered === token) continue;
|
|
43
|
-
if (registered.name !== name) continue;
|
|
179
|
+
if (typeof registered !== "function" || registered === token || registered.name !== name) continue;
|
|
44
180
|
const registeredParent = Object.getPrototypeOf(registered);
|
|
45
181
|
if (tokenParent && registeredParent && tokenParent.name !== registeredParent.name) continue;
|
|
46
182
|
const registeredProps = this.getComparableStaticProps(registered);
|
|
47
|
-
if (
|
|
48
|
-
return registered;
|
|
183
|
+
if (this.staticPropsMatch(token, registered, tokenProps, registeredProps)) return registered;
|
|
49
184
|
}
|
|
50
185
|
}
|
|
51
|
-
|
|
186
|
+
getComparableStaticProps(token) {
|
|
52
187
|
return Object.getOwnPropertyNames(token).filter((prop) => {
|
|
53
188
|
return ![
|
|
54
189
|
"length",
|
|
@@ -59,23 +194,38 @@ var Container = class {
|
|
|
59
194
|
].includes(prop);
|
|
60
195
|
});
|
|
61
196
|
}
|
|
62
|
-
|
|
197
|
+
staticPropsMatch(token, registered, tokenProps, registeredProps) {
|
|
63
198
|
if (tokenProps.length !== registeredProps.length) return false;
|
|
64
|
-
for (const prop of tokenProps)
|
|
65
|
-
if (!registeredProps.includes(prop)) return false;
|
|
66
|
-
if (Reflect.get(token, prop) !== Reflect.get(registered, prop)) return false;
|
|
67
|
-
}
|
|
199
|
+
for (const prop of tokenProps) if (!registeredProps.includes(prop) || Reflect.get(token, prop) !== Reflect.get(registered, prop)) return false;
|
|
68
200
|
return true;
|
|
69
201
|
}
|
|
70
|
-
static async resolveBinding(binding, ctx, autoDiscover) {
|
|
71
|
-
if (!binding) return void 0;
|
|
72
|
-
if (typeof binding !== "function") return binding;
|
|
73
|
-
if (isClass(binding)) return new binding();
|
|
74
|
-
const resolved = await binding(ctx);
|
|
75
|
-
if (typeof resolved === "function" && autoDiscover && isClass(resolved)) return new resolved();
|
|
76
|
-
return resolved;
|
|
77
|
-
}
|
|
78
202
|
};
|
|
203
|
+
function normalizeProvider(value, options) {
|
|
204
|
+
if (isProvider(value)) return {
|
|
205
|
+
scope: "transient",
|
|
206
|
+
...value
|
|
207
|
+
};
|
|
208
|
+
if (isClass(value)) return {
|
|
209
|
+
useClass: value,
|
|
210
|
+
scope: options?.scope ?? "transient"
|
|
211
|
+
};
|
|
212
|
+
if (typeof value === "function") return {
|
|
213
|
+
useFactory: value,
|
|
214
|
+
scope: options?.scope ?? "transient"
|
|
215
|
+
};
|
|
216
|
+
return { useValue: value };
|
|
217
|
+
}
|
|
218
|
+
function isProvider(value) {
|
|
219
|
+
return Boolean(value && typeof value === "object" && ("useValue" in value || "useClass" in value || "useFactory" in value || "useExisting" in value));
|
|
220
|
+
}
|
|
221
|
+
function getInjectTokens(Type) {
|
|
222
|
+
return Type.inject ?? getDesignParamTypes(Type);
|
|
223
|
+
}
|
|
224
|
+
function describeToken(token) {
|
|
225
|
+
if (token instanceof InjectionToken) return token.toString();
|
|
226
|
+
if (typeof token === "symbol") return token.description ?? token.toString();
|
|
227
|
+
return token.name || "<anonymous class>";
|
|
228
|
+
}
|
|
79
229
|
function Bind(...tokens) {
|
|
80
230
|
return ((target, propertyKeyOrContext) => {
|
|
81
231
|
if (isStandardClassContext(propertyKeyOrContext)) {
|
|
@@ -103,6 +253,9 @@ function Bind(...tokens) {
|
|
|
103
253
|
});
|
|
104
254
|
});
|
|
105
255
|
}
|
|
256
|
+
function getMetadataKey() {
|
|
257
|
+
return metadataKey;
|
|
258
|
+
}
|
|
106
259
|
function getStandardMetadata(metadata, propertyKey) {
|
|
107
260
|
const store = metadata && metadata[metadataKey];
|
|
108
261
|
if (!store) return void 0;
|
|
@@ -153,7 +306,13 @@ function isClass(value) {
|
|
|
153
306
|
//#endregion
|
|
154
307
|
exports.Bind = Bind;
|
|
155
308
|
exports.Container = Container;
|
|
309
|
+
exports.ContainerResolutionError = ContainerResolutionError;
|
|
310
|
+
exports.InjectionToken = InjectionToken;
|
|
311
|
+
exports.getBindingMetadata = getBindingMetadata;
|
|
156
312
|
exports.getBindingMetadataFromTargets = getBindingMetadataFromTargets;
|
|
157
313
|
exports.getDesignParamTypes = getDesignParamTypes;
|
|
314
|
+
exports.getMetadataKey = getMetadataKey;
|
|
158
315
|
exports.getStandardMetadata = getStandardMetadata;
|
|
159
|
-
exports.isClass = isClass;
|
|
316
|
+
exports.isClass = isClass;
|
|
317
|
+
exports.setBindingMetadata = setBindingMetadata;
|
|
318
|
+
exports.setStandardMetadata = setStandardMetadata;
|
package/dist/core/bindings.d.cts
CHANGED
|
@@ -1,25 +1,96 @@
|
|
|
1
1
|
//#region src/core/bindings.d.ts
|
|
2
|
-
type
|
|
2
|
+
type BindClass<T = any> = abstract new (...args: any[]) => T;
|
|
3
|
+
type BindToken<T = any> = BindClass<T> | InjectionToken<T> | symbol;
|
|
3
4
|
type BindFactory<T = any> = (ctx: any) => T | Promise<T>;
|
|
4
|
-
type
|
|
5
|
+
type BindingScope = 'singleton' | 'request' | 'transient';
|
|
6
|
+
type BindingOptions = {
|
|
7
|
+
scope?: BindingScope;
|
|
8
|
+
};
|
|
9
|
+
type ValueProvider<T = any> = {
|
|
10
|
+
useValue: T;
|
|
11
|
+
};
|
|
12
|
+
type ClassProvider<T = any> = BindingOptions & {
|
|
13
|
+
useClass: BindClass<T>;
|
|
14
|
+
dependencies?: BindToken[];
|
|
15
|
+
};
|
|
16
|
+
type FactoryProvider<T = any> = BindingOptions & {
|
|
17
|
+
useFactory: BindFactory<T>;
|
|
18
|
+
};
|
|
19
|
+
type ExistingProvider<T = any> = {
|
|
20
|
+
useExisting: BindToken<T>;
|
|
21
|
+
};
|
|
22
|
+
type BindProvider<T = any> = ValueProvider<T> | ClassProvider<T> | FactoryProvider<T> | ExistingProvider<T>;
|
|
23
|
+
type BindValue<T = any> = T | BindFactory<T> | BindClass<T> | BindProvider<T>;
|
|
5
24
|
type BindDecorator = MethodDecorator & ClassDecorator & {
|
|
6
25
|
<This, Value extends (this: This, ...args: any[]) => any>(value: Value, context: ClassMethodDecoratorContext<This, Value>): void | Value;
|
|
7
26
|
<Value extends abstract new (...args: any[]) => any>(value: Value, context: ClassDecoratorContext<Value>): void | Value;
|
|
8
27
|
};
|
|
28
|
+
type BindingMetadata = {
|
|
29
|
+
tokens?: BindToken[];
|
|
30
|
+
method?: PropertyKey;
|
|
31
|
+
};
|
|
32
|
+
declare class InjectionToken<T = any> {
|
|
33
|
+
readonly description: string;
|
|
34
|
+
readonly __type?: T;
|
|
35
|
+
readonly key: symbol;
|
|
36
|
+
constructor(description: string);
|
|
37
|
+
toString(): string;
|
|
38
|
+
}
|
|
39
|
+
declare class ContainerResolutionError extends Error {
|
|
40
|
+
readonly path: BindToken[];
|
|
41
|
+
constructor(message: string, path: BindToken[]);
|
|
42
|
+
}
|
|
9
43
|
declare class Container {
|
|
10
|
-
|
|
11
|
-
|
|
44
|
+
readonly parent?: Container | undefined;
|
|
45
|
+
private readonly context?;
|
|
46
|
+
private readonly requestScope;
|
|
47
|
+
private static readonly activeScope;
|
|
48
|
+
private static readonly containers;
|
|
49
|
+
private static readonly globalContainer;
|
|
50
|
+
private readonly registry;
|
|
51
|
+
private readonly instances;
|
|
52
|
+
private readonly pending;
|
|
53
|
+
constructor(parent?: Container | undefined, context?: any | undefined, requestScope?: boolean);
|
|
54
|
+
static get global(): Container;
|
|
55
|
+
static current(): Container;
|
|
56
|
+
static run<T>(container: Container, callback: () => T): T;
|
|
57
|
+
static create(parent?: Container): Container;
|
|
58
|
+
createRequestScope(ctx: any): Container;
|
|
59
|
+
static bind<T>(token: BindToken<T>, value: BindValue<T>, options?: BindingOptions): void;
|
|
12
60
|
static unbind<T>(token: BindToken<T>): void;
|
|
13
61
|
static clear(): void;
|
|
14
62
|
static has<T>(token: BindToken<T>): boolean;
|
|
15
63
|
static bindings<V = any>(): Record<string, BindValue<V>>;
|
|
16
|
-
static resolve<T>(token: BindToken<T>, ctx
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
64
|
+
static resolve<T>(token: BindToken<T>, ctx?: any, autoDiscover?: boolean): Promise<T | undefined>;
|
|
65
|
+
bind<T>(token: BindToken<T>, value: BindValue<T>, options?: BindingOptions): void;
|
|
66
|
+
unbind<T>(token: BindToken<T>): void;
|
|
67
|
+
clear(): void;
|
|
68
|
+
has<T>(token: BindToken<T>): boolean;
|
|
69
|
+
entries(): ReadonlyMap<BindToken, BindProvider>;
|
|
70
|
+
bindings<V = any>(): Record<string, BindValue<V>>;
|
|
71
|
+
resolve<T>(token: BindToken<T>, ctx?: any, autoDiscover?: boolean): Promise<T | undefined>;
|
|
72
|
+
resolveOrFail<T>(token: BindToken<T>, ctx?: any, autoDiscover?: boolean): Promise<T>;
|
|
73
|
+
private resolveToken;
|
|
74
|
+
private findProvider;
|
|
75
|
+
private resolveProvider;
|
|
76
|
+
private instantiate;
|
|
77
|
+
private findRequestScope;
|
|
78
|
+
private lineage;
|
|
79
|
+
private findEquivalentToken;
|
|
80
|
+
private getComparableStaticProps;
|
|
81
|
+
private staticPropsMatch;
|
|
22
82
|
}
|
|
23
83
|
declare function Bind(...tokens: BindToken[]): BindDecorator;
|
|
84
|
+
declare function getMetadataKey(): symbol;
|
|
85
|
+
declare function getStandardMetadata(metadata: object | undefined, propertyKey?: PropertyKey): BindingMetadata | undefined;
|
|
86
|
+
declare function setStandardMetadata(metadata: object | undefined, propertyKey: PropertyKey, value: BindingMetadata): void;
|
|
87
|
+
declare function getBindingMetadataFromTargets(targets: Array<{
|
|
88
|
+
target?: object;
|
|
89
|
+
propertyKey?: PropertyKey;
|
|
90
|
+
}>): BindingMetadata | undefined;
|
|
91
|
+
declare function getBindingMetadata(target: object, propertyKey?: PropertyKey): BindingMetadata | undefined;
|
|
92
|
+
declare function setBindingMetadata(target: object, propertyKey: PropertyKey, metadata: BindingMetadata): void;
|
|
93
|
+
declare function getDesignParamTypes(target: object, propertyKey?: PropertyKey): BindToken[];
|
|
94
|
+
declare function isClass(value: any): boolean;
|
|
24
95
|
//#endregion
|
|
25
|
-
export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
|
|
96
|
+
export { Bind, BindClass, BindDecorator, BindFactory, BindProvider, BindToken, BindValue, BindingOptions, BindingScope, ClassProvider, Container, ContainerResolutionError, ExistingProvider, FactoryProvider, InjectionToken, ValueProvider, getBindingMetadata, getBindingMetadataFromTargets, getDesignParamTypes, getMetadataKey, getStandardMetadata, isClass, setBindingMetadata, setStandardMetadata };
|
package/dist/core/bindings.d.mts
CHANGED
|
@@ -1,25 +1,96 @@
|
|
|
1
1
|
//#region src/core/bindings.d.ts
|
|
2
|
-
type
|
|
2
|
+
type BindClass<T = any> = abstract new (...args: any[]) => T;
|
|
3
|
+
type BindToken<T = any> = BindClass<T> | InjectionToken<T> | symbol;
|
|
3
4
|
type BindFactory<T = any> = (ctx: any) => T | Promise<T>;
|
|
4
|
-
type
|
|
5
|
+
type BindingScope = 'singleton' | 'request' | 'transient';
|
|
6
|
+
type BindingOptions = {
|
|
7
|
+
scope?: BindingScope;
|
|
8
|
+
};
|
|
9
|
+
type ValueProvider<T = any> = {
|
|
10
|
+
useValue: T;
|
|
11
|
+
};
|
|
12
|
+
type ClassProvider<T = any> = BindingOptions & {
|
|
13
|
+
useClass: BindClass<T>;
|
|
14
|
+
dependencies?: BindToken[];
|
|
15
|
+
};
|
|
16
|
+
type FactoryProvider<T = any> = BindingOptions & {
|
|
17
|
+
useFactory: BindFactory<T>;
|
|
18
|
+
};
|
|
19
|
+
type ExistingProvider<T = any> = {
|
|
20
|
+
useExisting: BindToken<T>;
|
|
21
|
+
};
|
|
22
|
+
type BindProvider<T = any> = ValueProvider<T> | ClassProvider<T> | FactoryProvider<T> | ExistingProvider<T>;
|
|
23
|
+
type BindValue<T = any> = T | BindFactory<T> | BindClass<T> | BindProvider<T>;
|
|
5
24
|
type BindDecorator = MethodDecorator & ClassDecorator & {
|
|
6
25
|
<This, Value extends (this: This, ...args: any[]) => any>(value: Value, context: ClassMethodDecoratorContext<This, Value>): void | Value;
|
|
7
26
|
<Value extends abstract new (...args: any[]) => any>(value: Value, context: ClassDecoratorContext<Value>): void | Value;
|
|
8
27
|
};
|
|
28
|
+
type BindingMetadata = {
|
|
29
|
+
tokens?: BindToken[];
|
|
30
|
+
method?: PropertyKey;
|
|
31
|
+
};
|
|
32
|
+
declare class InjectionToken<T = any> {
|
|
33
|
+
readonly description: string;
|
|
34
|
+
readonly __type?: T;
|
|
35
|
+
readonly key: symbol;
|
|
36
|
+
constructor(description: string);
|
|
37
|
+
toString(): string;
|
|
38
|
+
}
|
|
39
|
+
declare class ContainerResolutionError extends Error {
|
|
40
|
+
readonly path: BindToken[];
|
|
41
|
+
constructor(message: string, path: BindToken[]);
|
|
42
|
+
}
|
|
9
43
|
declare class Container {
|
|
10
|
-
|
|
11
|
-
|
|
44
|
+
readonly parent?: Container | undefined;
|
|
45
|
+
private readonly context?;
|
|
46
|
+
private readonly requestScope;
|
|
47
|
+
private static readonly activeScope;
|
|
48
|
+
private static readonly containers;
|
|
49
|
+
private static readonly globalContainer;
|
|
50
|
+
private readonly registry;
|
|
51
|
+
private readonly instances;
|
|
52
|
+
private readonly pending;
|
|
53
|
+
constructor(parent?: Container | undefined, context?: any | undefined, requestScope?: boolean);
|
|
54
|
+
static get global(): Container;
|
|
55
|
+
static current(): Container;
|
|
56
|
+
static run<T>(container: Container, callback: () => T): T;
|
|
57
|
+
static create(parent?: Container): Container;
|
|
58
|
+
createRequestScope(ctx: any): Container;
|
|
59
|
+
static bind<T>(token: BindToken<T>, value: BindValue<T>, options?: BindingOptions): void;
|
|
12
60
|
static unbind<T>(token: BindToken<T>): void;
|
|
13
61
|
static clear(): void;
|
|
14
62
|
static has<T>(token: BindToken<T>): boolean;
|
|
15
63
|
static bindings<V = any>(): Record<string, BindValue<V>>;
|
|
16
|
-
static resolve<T>(token: BindToken<T>, ctx
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
64
|
+
static resolve<T>(token: BindToken<T>, ctx?: any, autoDiscover?: boolean): Promise<T | undefined>;
|
|
65
|
+
bind<T>(token: BindToken<T>, value: BindValue<T>, options?: BindingOptions): void;
|
|
66
|
+
unbind<T>(token: BindToken<T>): void;
|
|
67
|
+
clear(): void;
|
|
68
|
+
has<T>(token: BindToken<T>): boolean;
|
|
69
|
+
entries(): ReadonlyMap<BindToken, BindProvider>;
|
|
70
|
+
bindings<V = any>(): Record<string, BindValue<V>>;
|
|
71
|
+
resolve<T>(token: BindToken<T>, ctx?: any, autoDiscover?: boolean): Promise<T | undefined>;
|
|
72
|
+
resolveOrFail<T>(token: BindToken<T>, ctx?: any, autoDiscover?: boolean): Promise<T>;
|
|
73
|
+
private resolveToken;
|
|
74
|
+
private findProvider;
|
|
75
|
+
private resolveProvider;
|
|
76
|
+
private instantiate;
|
|
77
|
+
private findRequestScope;
|
|
78
|
+
private lineage;
|
|
79
|
+
private findEquivalentToken;
|
|
80
|
+
private getComparableStaticProps;
|
|
81
|
+
private staticPropsMatch;
|
|
22
82
|
}
|
|
23
83
|
declare function Bind(...tokens: BindToken[]): BindDecorator;
|
|
84
|
+
declare function getMetadataKey(): symbol;
|
|
85
|
+
declare function getStandardMetadata(metadata: object | undefined, propertyKey?: PropertyKey): BindingMetadata | undefined;
|
|
86
|
+
declare function setStandardMetadata(metadata: object | undefined, propertyKey: PropertyKey, value: BindingMetadata): void;
|
|
87
|
+
declare function getBindingMetadataFromTargets(targets: Array<{
|
|
88
|
+
target?: object;
|
|
89
|
+
propertyKey?: PropertyKey;
|
|
90
|
+
}>): BindingMetadata | undefined;
|
|
91
|
+
declare function getBindingMetadata(target: object, propertyKey?: PropertyKey): BindingMetadata | undefined;
|
|
92
|
+
declare function setBindingMetadata(target: object, propertyKey: PropertyKey, metadata: BindingMetadata): void;
|
|
93
|
+
declare function getDesignParamTypes(target: object, propertyKey?: PropertyKey): BindToken[];
|
|
94
|
+
declare function isClass(value: any): boolean;
|
|
24
95
|
//#endregion
|
|
25
|
-
export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
|
|
96
|
+
export { Bind, BindClass, BindDecorator, BindFactory, BindProvider, BindToken, BindValue, BindingOptions, BindingScope, ClassProvider, Container, ContainerResolutionError, ExistingProvider, FactoryProvider, InjectionToken, ValueProvider, getBindingMetadata, getBindingMetadataFromTargets, getDesignParamTypes, getMetadataKey, getStandardMetadata, isClass, setBindingMetadata, setStandardMetadata };
|