getbox 1.4.0 → 2.1.0
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/CONTEXT.md +5 -5
- package/README.md +52 -35
- package/dist/context.cjs +8 -37
- package/dist/context.d.cts +13 -30
- package/dist/context.d.mts +13 -30
- package/dist/context.mjs +8 -33
- package/dist/index.cjs +99 -150
- package/dist/index.d.cts +70 -113
- package/dist/index.d.mts +70 -113
- package/dist/index.mjs +95 -146
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
|
|
2
2
|
//#region src/index.ts
|
|
3
3
|
/**
|
|
4
|
+
* Symbol key used to opt a constructor out of caching.
|
|
5
|
+
* When set to `false`, {@link Box.get} will not cache the resolved value.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* class MyService {
|
|
10
|
+
* static [boxCache] = false;
|
|
11
|
+
* static init = Box.init(MyService).get(Database);
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
const boxCache = Symbol("Box.cache");
|
|
16
|
+
/**
|
|
4
17
|
* Creates a {@link Constructor} from a factory function.
|
|
5
18
|
* The factory receives the box as an argument, allowing it to resolve other dependencies.
|
|
6
19
|
*
|
|
@@ -14,9 +27,8 @@
|
|
|
14
27
|
* ```
|
|
15
28
|
*/
|
|
16
29
|
function factory(init) {
|
|
17
|
-
return { init };
|
|
30
|
+
return { init: new Init(init) };
|
|
18
31
|
}
|
|
19
|
-
const noCacheSymbol = Symbol("Box.noCache");
|
|
20
32
|
/**
|
|
21
33
|
* Creates a {@link Constructor} that computes a value from the box without caching the result.
|
|
22
34
|
*
|
|
@@ -26,7 +38,7 @@ const noCacheSymbol = Symbol("Box.noCache");
|
|
|
26
38
|
* baseUrl = "https://example.com";
|
|
27
39
|
* }
|
|
28
40
|
*
|
|
29
|
-
* const RequestContext =
|
|
41
|
+
* const RequestContext = computed((box) => ({
|
|
30
42
|
* baseUrl: box.get(Config).baseUrl,
|
|
31
43
|
* timestamp: Date.now(),
|
|
32
44
|
* }));
|
|
@@ -36,42 +48,37 @@ const noCacheSymbol = Symbol("Box.noCache");
|
|
|
36
48
|
* console.log(ctx1 === ctx2); // false
|
|
37
49
|
* ```
|
|
38
50
|
*/
|
|
39
|
-
function
|
|
51
|
+
function computed(init) {
|
|
40
52
|
return {
|
|
41
|
-
init,
|
|
42
|
-
[
|
|
53
|
+
init: new Init(init),
|
|
54
|
+
[boxCache]: false
|
|
43
55
|
};
|
|
44
56
|
}
|
|
45
57
|
/**
|
|
46
|
-
* @deprecated Use {@link derive} instead.
|
|
47
|
-
*/
|
|
48
|
-
function transient(init) {
|
|
49
|
-
return derive(init);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
58
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
53
|
-
* Constant values are
|
|
59
|
+
* Constant values are already fixed and do not need caching.
|
|
54
60
|
*
|
|
55
61
|
* @example
|
|
56
62
|
* ```ts
|
|
57
63
|
* const ApiUrl = constant("https://api.example.com");
|
|
58
|
-
* const
|
|
64
|
+
* const url = box.get(ApiUrl); // "https://api.example.com"
|
|
59
65
|
* ```
|
|
60
66
|
*/
|
|
61
67
|
function constant(value) {
|
|
62
68
|
return {
|
|
63
|
-
init: () => value,
|
|
64
|
-
[
|
|
69
|
+
init: new Init(() => value),
|
|
70
|
+
[boxCache]: false
|
|
65
71
|
};
|
|
66
72
|
}
|
|
67
73
|
/**
|
|
68
74
|
* Dependency injection container that resolves and caches instances from
|
|
69
75
|
* a {@link Constructor}.
|
|
70
76
|
*
|
|
71
|
-
* A constructor is
|
|
72
|
-
* a class with a
|
|
73
|
-
*
|
|
74
|
-
*
|
|
77
|
+
* A constructor is a class with a `static init` property set to an initializer,
|
|
78
|
+
* a class with a no-argument constructor, or a function-based constructor created by
|
|
79
|
+
* {@link factory}, {@link computed}, or {@link constant}.
|
|
80
|
+
*
|
|
81
|
+
* `Box` can be resolved as a dependency — `box.get(Box)` returns the current instance.
|
|
75
82
|
*
|
|
76
83
|
* @example
|
|
77
84
|
* ```ts
|
|
@@ -89,53 +96,65 @@ function constant(value) {
|
|
|
89
96
|
* const db = box.get(Database);
|
|
90
97
|
*
|
|
91
98
|
* console.log(service.db === db); // true (cached)
|
|
99
|
+
* console.log(box.get(Box) === box); // true
|
|
92
100
|
* ```
|
|
93
101
|
*/
|
|
94
|
-
var Box = class {
|
|
102
|
+
var Box = class Box {
|
|
95
103
|
cache = /* @__PURE__ */ new Map();
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
new(arg) {
|
|
105
|
+
if (Array.isArray(arg)) return arg.map((c) => this.new(c));
|
|
106
|
+
if (typeof arg === "function") return arg.init instanceof Init ? arg.init.fn(this) : new arg();
|
|
107
|
+
if (arg.init instanceof Init) return arg.init.fn(this);
|
|
108
|
+
const result = {};
|
|
109
|
+
for (const [key, constructor] of Object.entries(arg)) result[key] = this.new(constructor);
|
|
110
|
+
return result;
|
|
101
111
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
112
|
+
get(arg) {
|
|
113
|
+
if (Array.isArray(arg)) return arg.map((c) => this.get(c));
|
|
114
|
+
if (typeof arg === "function") {
|
|
115
|
+
if (this.cache.has(arg)) return this.cache.get(arg);
|
|
116
|
+
if (arg === Box) return this;
|
|
117
|
+
const value = arg.init instanceof Init ? arg.init.fn(this) : new arg();
|
|
118
|
+
if (arg[boxCache] !== false) this.cache.set(arg, value);
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
if (arg.init instanceof Init) {
|
|
122
|
+
if (this.cache.has(arg)) return this.cache.get(arg);
|
|
123
|
+
const value = arg.init.fn(this);
|
|
124
|
+
if (arg[boxCache] !== false) this.cache.set(arg, value);
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
const result = {};
|
|
128
|
+
for (const [key, constructor] of Object.entries(arg)) result[key] = this.get(constructor);
|
|
129
|
+
return result;
|
|
111
130
|
}
|
|
112
|
-
/** Resolves multiple constructors at once. */
|
|
113
|
-
all = new BoxAll(this);
|
|
114
131
|
/**
|
|
115
|
-
* Returns
|
|
116
|
-
* resolving constructors for each constructor parameter.
|
|
132
|
+
* Returns an initializer builder for a class constructor.
|
|
117
133
|
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* class UserService {
|
|
137
|
+
* constructor(private db: Database, private logger: Logger) {}
|
|
138
|
+
* static init = Box.init(UserService).get(Database, LoggerFactory);
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
121
141
|
*/
|
|
122
|
-
|
|
123
|
-
return new
|
|
142
|
+
static init(ctor) {
|
|
143
|
+
return new StaticInit(ctor);
|
|
124
144
|
}
|
|
125
145
|
/**
|
|
126
|
-
* Returns
|
|
127
|
-
* The result can be assigned directly to `static init`.
|
|
146
|
+
* Returns an initializer from a factory function.
|
|
128
147
|
*
|
|
129
148
|
* @example
|
|
130
149
|
* ```ts
|
|
131
150
|
* class UserService {
|
|
132
|
-
* constructor(private db: Database
|
|
133
|
-
* static init = Box.
|
|
151
|
+
* constructor(private db: Database) {}
|
|
152
|
+
* static init = Box.fn((box) => new UserService(box.get(Database)));
|
|
134
153
|
* }
|
|
135
154
|
* ```
|
|
136
155
|
*/
|
|
137
|
-
static
|
|
138
|
-
return new
|
|
156
|
+
static fn(fn) {
|
|
157
|
+
return new Init(fn);
|
|
139
158
|
}
|
|
140
159
|
/**
|
|
141
160
|
* Registers a mock value in the box's cache for a given constructor.
|
|
@@ -147,116 +166,49 @@ var Box = class {
|
|
|
147
166
|
/**
|
|
148
167
|
* Removes the instance from the box's cache for a given constructor.
|
|
149
168
|
* Removes all instances if no constructor is provided.
|
|
169
|
+
*
|
|
170
|
+
* Returns true if an instance existed and has been removed from the box's cache.
|
|
150
171
|
*/
|
|
151
172
|
static clear(box, constructor) {
|
|
152
|
-
if (!constructor)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
constructor(box) {
|
|
159
|
-
this.box = box;
|
|
160
|
-
}
|
|
161
|
-
new(constructors) {
|
|
162
|
-
if (Array.isArray(constructors)) return constructors.map((c) => this.box.new(c));
|
|
163
|
-
const result = {};
|
|
164
|
-
for (const [key, constructor] of Object.entries(constructors)) result[key] = this.box.new(constructor);
|
|
165
|
-
return result;
|
|
166
|
-
}
|
|
167
|
-
get(constructors) {
|
|
168
|
-
if (Array.isArray(constructors)) return constructors.map((c) => this.box.get(c));
|
|
169
|
-
const result = {};
|
|
170
|
-
for (const [key, constructor] of Object.entries(constructors)) result[key] = this.box.get(constructor);
|
|
171
|
-
return result;
|
|
173
|
+
if (!constructor) {
|
|
174
|
+
const size = box.cache.size;
|
|
175
|
+
box.cache.clear();
|
|
176
|
+
return size > 0;
|
|
177
|
+
}
|
|
178
|
+
return box.cache.delete(constructor);
|
|
172
179
|
}
|
|
173
180
|
};
|
|
174
|
-
/**
|
|
175
|
-
var
|
|
176
|
-
constructor(
|
|
177
|
-
this.
|
|
178
|
-
this.construct = construct;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Resolves each dependency as a new instance via {@link Box.new},
|
|
182
|
-
* meaning dependencies are not cached or shared.
|
|
183
|
-
*
|
|
184
|
-
* The returned instance is cached or new depending on whether the
|
|
185
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
* ```ts
|
|
189
|
-
* class UserService {
|
|
190
|
-
* constructor(private db: Database, private logger: Logger) {}
|
|
191
|
-
* static init(box: Box) {
|
|
192
|
-
* return box.for(UserService).new(Database, LoggerFactory);
|
|
193
|
-
* }
|
|
194
|
-
* }
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
new(...args) {
|
|
198
|
-
const instances = args.map((arg) => this.box.new(arg));
|
|
199
|
-
return new this.construct(...instances);
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Resolves each dependency as a cached instance via {@link Box.get},
|
|
203
|
-
* meaning dependencies are shared across the box.
|
|
204
|
-
*
|
|
205
|
-
* The returned instance is cached or new depending on whether the
|
|
206
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```ts
|
|
210
|
-
* class UserService {
|
|
211
|
-
* constructor(private db: Database, private logger: Logger) {}
|
|
212
|
-
* static init(box: Box) {
|
|
213
|
-
* return box.for(UserService).get(Database, LoggerFactory);
|
|
214
|
-
* }
|
|
215
|
-
* }
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
|
-
get(...args) {
|
|
219
|
-
const instances = args.map((arg) => this.box.get(arg));
|
|
220
|
-
return new this.construct(...instances);
|
|
181
|
+
/** An initializer wrapping a factory function for use as a {@link Constructor}. */
|
|
182
|
+
var Init = class {
|
|
183
|
+
constructor(fn) {
|
|
184
|
+
this.fn = fn;
|
|
221
185
|
}
|
|
222
186
|
};
|
|
223
|
-
/**
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
var StaticConstruct = class {
|
|
228
|
-
constructor(construct) {
|
|
229
|
-
this.construct = construct;
|
|
187
|
+
/** Builder that creates an initializer with dependencies resolved from a {@link Box}. */
|
|
188
|
+
var StaticInit = class {
|
|
189
|
+
constructor(ctor) {
|
|
190
|
+
this.ctor = ctor;
|
|
230
191
|
}
|
|
231
192
|
/**
|
|
232
|
-
* Resolves each dependency as a new instance via {@link Box.new}
|
|
233
|
-
*
|
|
234
|
-
* Returns
|
|
235
|
-
*
|
|
236
|
-
* The returned instance is cached or new depending on whether the
|
|
237
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
193
|
+
* Resolves each dependency as a new instance via {@link Box.new}.
|
|
194
|
+
* Dependencies are not cached or shared.
|
|
195
|
+
* Returns an initializer.
|
|
238
196
|
*
|
|
239
197
|
* @example
|
|
240
198
|
* ```ts
|
|
241
199
|
* class UserService {
|
|
242
200
|
* constructor(private db: Database, private logger: Logger) {}
|
|
243
|
-
* static init = Box.init(UserService).
|
|
201
|
+
* static init = Box.init(UserService).new(Database, LoggerFactory);
|
|
244
202
|
* }
|
|
245
203
|
* ```
|
|
246
204
|
*/
|
|
247
205
|
new(...args) {
|
|
248
|
-
return (box) =>
|
|
249
|
-
const instances = args.map((arg) => box.new(arg));
|
|
250
|
-
return new this.construct(...instances);
|
|
251
|
-
};
|
|
206
|
+
return new Init((box) => new this.ctor(...box.new(args)));
|
|
252
207
|
}
|
|
253
208
|
/**
|
|
254
|
-
* Resolves each dependency as a cached instance via {@link Box.get}
|
|
255
|
-
*
|
|
256
|
-
* Returns
|
|
257
|
-
*
|
|
258
|
-
* The returned instance is cached or new depending on whether the
|
|
259
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
209
|
+
* Resolves each dependency as a cached instance via {@link Box.get}.
|
|
210
|
+
* Dependencies are cached and shared.
|
|
211
|
+
* Returns an initializer.
|
|
260
212
|
*
|
|
261
213
|
* @example
|
|
262
214
|
* ```ts
|
|
@@ -267,18 +219,15 @@ var StaticConstruct = class {
|
|
|
267
219
|
* ```
|
|
268
220
|
*/
|
|
269
221
|
get(...args) {
|
|
270
|
-
return (box) =>
|
|
271
|
-
const instances = args.map((arg) => box.get(arg));
|
|
272
|
-
return new this.construct(...instances);
|
|
273
|
-
};
|
|
222
|
+
return new Init((box) => new this.ctor(...box.get(args)));
|
|
274
223
|
}
|
|
275
224
|
};
|
|
276
225
|
|
|
277
226
|
//#endregion
|
|
278
227
|
exports.Box = Box;
|
|
279
|
-
exports.
|
|
280
|
-
exports.
|
|
228
|
+
exports.Init = Init;
|
|
229
|
+
exports.StaticInit = StaticInit;
|
|
230
|
+
exports.boxCache = boxCache;
|
|
231
|
+
exports.computed = computed;
|
|
281
232
|
exports.constant = constant;
|
|
282
|
-
exports.
|
|
283
|
-
exports.factory = factory;
|
|
284
|
-
exports.transient = transient;
|
|
233
|
+
exports.factory = factory;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Symbol key used to opt a constructor out of caching.
|
|
4
|
+
* When set to `false`, {@link Box.get} will not cache the resolved value.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* class MyService {
|
|
9
|
+
* static [boxCache] = false;
|
|
10
|
+
* static init = Box.init(MyService).get(Database);
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare const boxCache: unique symbol;
|
|
15
|
+
/**
|
|
16
|
+
* A type that can be resolved by a {@link Box}. Either a class with a
|
|
17
|
+
* `static init` property set to an initializer, a class with a no-argument
|
|
18
|
+
* constructor, or a function-based constructor created by {@link factory},
|
|
19
|
+
* {@link computed}, or {@link constant}.
|
|
6
20
|
*/
|
|
7
21
|
type Constructor<T> = {
|
|
8
|
-
init
|
|
22
|
+
init: Init<T>;
|
|
9
23
|
} | {
|
|
10
24
|
new (): T;
|
|
11
25
|
};
|
|
12
26
|
/** Extracts the instance type from a {@link Constructor}. */
|
|
13
27
|
type ConstructorInstanceType<T> = T extends {
|
|
14
|
-
init
|
|
28
|
+
init: Init<infer U>;
|
|
15
29
|
} ? U : T extends {
|
|
16
30
|
new (): infer U;
|
|
17
31
|
} ? U : never;
|
|
@@ -38,7 +52,7 @@ declare function factory<T>(init: (box: Box) => T): Constructor<T>;
|
|
|
38
52
|
* baseUrl = "https://example.com";
|
|
39
53
|
* }
|
|
40
54
|
*
|
|
41
|
-
* const RequestContext =
|
|
55
|
+
* const RequestContext = computed((box) => ({
|
|
42
56
|
* baseUrl: box.get(Config).baseUrl,
|
|
43
57
|
* timestamp: Date.now(),
|
|
44
58
|
* }));
|
|
@@ -48,19 +62,15 @@ declare function factory<T>(init: (box: Box) => T): Constructor<T>;
|
|
|
48
62
|
* console.log(ctx1 === ctx2); // false
|
|
49
63
|
* ```
|
|
50
64
|
*/
|
|
51
|
-
declare function
|
|
52
|
-
/**
|
|
53
|
-
* @deprecated Use {@link derive} instead.
|
|
54
|
-
*/
|
|
55
|
-
declare function transient<T>(init: (box: Box) => T): Constructor<T>;
|
|
65
|
+
declare function computed<T>(init: (box: Box) => T): Constructor<T>;
|
|
56
66
|
/**
|
|
57
67
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
58
|
-
* Constant values are
|
|
68
|
+
* Constant values are already fixed and do not need caching.
|
|
59
69
|
*
|
|
60
70
|
* @example
|
|
61
71
|
* ```ts
|
|
62
72
|
* const ApiUrl = constant("https://api.example.com");
|
|
63
|
-
* const
|
|
73
|
+
* const url = box.get(ApiUrl); // "https://api.example.com"
|
|
64
74
|
* ```
|
|
65
75
|
*/
|
|
66
76
|
declare function constant<const T>(value: T): Constructor<T>;
|
|
@@ -68,10 +78,11 @@ declare function constant<const T>(value: T): Constructor<T>;
|
|
|
68
78
|
* Dependency injection container that resolves and caches instances from
|
|
69
79
|
* a {@link Constructor}.
|
|
70
80
|
*
|
|
71
|
-
* A constructor is
|
|
72
|
-
* a class with a
|
|
73
|
-
*
|
|
74
|
-
*
|
|
81
|
+
* A constructor is a class with a `static init` property set to an initializer,
|
|
82
|
+
* a class with a no-argument constructor, or a function-based constructor created by
|
|
83
|
+
* {@link factory}, {@link computed}, or {@link constant}.
|
|
84
|
+
*
|
|
85
|
+
* `Box` can be resolved as a dependency — `box.get(Box)` returns the current instance.
|
|
75
86
|
*
|
|
76
87
|
* @example
|
|
77
88
|
* ```ts
|
|
@@ -89,33 +100,28 @@ declare function constant<const T>(value: T): Constructor<T>;
|
|
|
89
100
|
* const db = box.get(Database);
|
|
90
101
|
*
|
|
91
102
|
* console.log(service.db === db); // true (cached)
|
|
103
|
+
* console.log(box.get(Box) === box); // true
|
|
92
104
|
* ```
|
|
93
105
|
*/
|
|
94
106
|
declare class Box {
|
|
95
107
|
protected cache: Map<Constructor<any>, any>;
|
|
96
108
|
/**
|
|
97
109
|
* Creates a new instance without caching.
|
|
110
|
+
* Accepts a single constructor, an array of constructors, or an object map of constructors.
|
|
98
111
|
*/
|
|
99
112
|
new<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
|
|
113
|
+
new<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
114
|
+
new<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
100
115
|
/**
|
|
101
116
|
* Resolves an instance from the cache, or creates and caches a new one.
|
|
102
117
|
* Subsequent calls with the same constructor return the cached instance.
|
|
118
|
+
* Accepts a single constructor, an array of constructors, or an object map of constructors.
|
|
103
119
|
*/
|
|
104
120
|
get<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Returns a {@link Construct} builder for creating class instances by
|
|
109
|
-
* resolving constructors for each constructor parameter.
|
|
110
|
-
*
|
|
111
|
-
* Intended to be used inside a class's `static init` method. The instance
|
|
112
|
-
* returned by the builder is never cached itself, but can be cached when
|
|
113
|
-
* the class is retrieved via {@link Box.get} or new via {@link Box.new}.
|
|
114
|
-
*/
|
|
115
|
-
for<T extends ClassConstructor<any>>(constructor: T): Construct<T>;
|
|
121
|
+
get<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
122
|
+
get<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
116
123
|
/**
|
|
117
|
-
* Returns
|
|
118
|
-
* The result can be assigned directly to `static init`.
|
|
124
|
+
* Returns an initializer builder for a class constructor.
|
|
119
125
|
*
|
|
120
126
|
* @example
|
|
121
127
|
* ```ts
|
|
@@ -125,108 +131,59 @@ declare class Box {
|
|
|
125
131
|
* }
|
|
126
132
|
* ```
|
|
127
133
|
*/
|
|
128
|
-
static init<T extends ClassConstructor<any>>(
|
|
129
|
-
/**
|
|
130
|
-
* Registers a mock value in the box's cache for a given constructor.
|
|
131
|
-
* Useful for replacing dependencies in tests.
|
|
132
|
-
*/
|
|
133
|
-
static mock<T, V extends T = T>(box: Box, constructor: Constructor<T>, value: V): void;
|
|
134
|
-
/**
|
|
135
|
-
* Removes the instance from the box's cache for a given constructor.
|
|
136
|
-
* Removes all instances if no constructor is provided.
|
|
137
|
-
*/
|
|
138
|
-
static clear<T>(box: Box, constructor?: Constructor<T>): void;
|
|
139
|
-
}
|
|
140
|
-
/** Resolves multiple constructors at once from a {@link Box}. */
|
|
141
|
-
declare class BoxAll {
|
|
142
|
-
private box;
|
|
143
|
-
constructor(box: Box);
|
|
144
|
-
/**
|
|
145
|
-
* Resolves each constructor as a new instance via {@link Box.new}.
|
|
146
|
-
* Accepts an array or object of constructors and returns instances in the same shape.
|
|
147
|
-
*/
|
|
148
|
-
new<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
149
|
-
new<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
150
|
-
/**
|
|
151
|
-
* Resolves each constructor as a cached instance via {@link Box.get}.
|
|
152
|
-
* Accepts an array or object of constructors and returns instances in the same shape.
|
|
153
|
-
*/
|
|
154
|
-
get<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
155
|
-
get<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
156
|
-
}
|
|
157
|
-
/** Builder for creating class instances with constructor dependencies resolved from a {@link Box}. */
|
|
158
|
-
declare class Construct<T extends ClassConstructor<any>> {
|
|
159
|
-
private box;
|
|
160
|
-
private construct;
|
|
161
|
-
constructor(box: Box, construct: T);
|
|
134
|
+
static init<T extends ClassConstructor<any>>(ctor: T): StaticInit<T>;
|
|
162
135
|
/**
|
|
163
|
-
*
|
|
164
|
-
* meaning dependencies are not cached or shared.
|
|
165
|
-
*
|
|
166
|
-
* The returned instance is cached or new depending on whether the
|
|
167
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
136
|
+
* Returns an initializer from a factory function.
|
|
168
137
|
*
|
|
169
138
|
* @example
|
|
170
139
|
* ```ts
|
|
171
140
|
* class UserService {
|
|
172
|
-
* constructor(private db: Database
|
|
173
|
-
* static init(box
|
|
174
|
-
* return box.for(UserService).new(Database, LoggerFactory);
|
|
175
|
-
* }
|
|
141
|
+
* constructor(private db: Database) {}
|
|
142
|
+
* static init = Box.fn((box) => new UserService(box.get(Database)));
|
|
176
143
|
* }
|
|
177
144
|
* ```
|
|
178
145
|
*/
|
|
179
|
-
|
|
146
|
+
static fn<T>(fn: (box: Box) => T): Init<T>;
|
|
180
147
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
148
|
+
* Registers a mock value in the box's cache for a given constructor.
|
|
149
|
+
* Useful for replacing dependencies in tests.
|
|
150
|
+
*/
|
|
151
|
+
static mock<T, V extends T = T>(box: Box, constructor: Constructor<T>, value: V): void;
|
|
152
|
+
/**
|
|
153
|
+
* Removes the instance from the box's cache for a given constructor.
|
|
154
|
+
* Removes all instances if no constructor is provided.
|
|
186
155
|
*
|
|
187
|
-
*
|
|
188
|
-
* ```ts
|
|
189
|
-
* class UserService {
|
|
190
|
-
* constructor(private db: Database, private logger: Logger) {}
|
|
191
|
-
* static init(box: Box) {
|
|
192
|
-
* return box.for(UserService).get(Database, LoggerFactory);
|
|
193
|
-
* }
|
|
194
|
-
* }
|
|
195
|
-
* ```
|
|
156
|
+
* Returns true if an instance existed and has been removed from the box's cache.
|
|
196
157
|
*/
|
|
197
|
-
|
|
158
|
+
static clear<T>(box: Box, constructor?: Constructor<T>): boolean;
|
|
198
159
|
}
|
|
199
|
-
/**
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
160
|
+
/** An initializer wrapping a factory function for use as a {@link Constructor}. */
|
|
161
|
+
declare class Init<T> {
|
|
162
|
+
readonly fn: (box: Box) => T;
|
|
163
|
+
constructor(fn: (box: Box) => T);
|
|
164
|
+
}
|
|
165
|
+
/** Builder that creates an initializer with dependencies resolved from a {@link Box}. */
|
|
166
|
+
declare class StaticInit<C extends ClassConstructor<any>> {
|
|
167
|
+
private ctor;
|
|
168
|
+
constructor(ctor: C);
|
|
206
169
|
/**
|
|
207
|
-
* Resolves each dependency as a new instance via {@link Box.new}
|
|
208
|
-
*
|
|
209
|
-
* Returns
|
|
210
|
-
*
|
|
211
|
-
* The returned instance is cached or new depending on whether the
|
|
212
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
170
|
+
* Resolves each dependency as a new instance via {@link Box.new}.
|
|
171
|
+
* Dependencies are not cached or shared.
|
|
172
|
+
* Returns an initializer.
|
|
213
173
|
*
|
|
214
174
|
* @example
|
|
215
175
|
* ```ts
|
|
216
176
|
* class UserService {
|
|
217
177
|
* constructor(private db: Database, private logger: Logger) {}
|
|
218
|
-
* static init = Box.init(UserService).
|
|
178
|
+
* static init = Box.init(UserService).new(Database, LoggerFactory);
|
|
219
179
|
* }
|
|
220
180
|
* ```
|
|
221
181
|
*/
|
|
222
|
-
new(...args: ClassConstructorArgs<
|
|
182
|
+
new(...args: ClassConstructorArgs<C>): Init<InstanceType<C>>;
|
|
223
183
|
/**
|
|
224
|
-
* Resolves each dependency as a cached instance via {@link Box.get}
|
|
225
|
-
*
|
|
226
|
-
* Returns
|
|
227
|
-
*
|
|
228
|
-
* The returned instance is cached or new depending on whether the
|
|
229
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
184
|
+
* Resolves each dependency as a cached instance via {@link Box.get}.
|
|
185
|
+
* Dependencies are cached and shared.
|
|
186
|
+
* Returns an initializer.
|
|
230
187
|
*
|
|
231
188
|
* @example
|
|
232
189
|
* ```ts
|
|
@@ -236,13 +193,13 @@ declare class StaticConstruct<T extends ClassConstructor<any>> {
|
|
|
236
193
|
* }
|
|
237
194
|
* ```
|
|
238
195
|
*/
|
|
239
|
-
get(...args: ClassConstructorArgs<
|
|
196
|
+
get(...args: ClassConstructorArgs<C>): Init<InstanceType<C>>;
|
|
240
197
|
}
|
|
241
198
|
/** A class with any constructor signature. */
|
|
242
199
|
type ClassConstructor<T> = {
|
|
243
200
|
new (...args: any): T;
|
|
244
201
|
};
|
|
245
202
|
/** Maps each constructor parameter to its corresponding {@link Constructor} type. */
|
|
246
|
-
type ClassConstructorArgs<
|
|
203
|
+
type ClassConstructorArgs<C extends ClassConstructor<any>, Args = ConstructorParameters<C>> = { [K in keyof Args]: Constructor<Args[K]> };
|
|
247
204
|
//#endregion
|
|
248
|
-
export { Box,
|
|
205
|
+
export { Box, Constructor, ConstructorInstanceType, Init, StaticInit, boxCache, computed, constant, factory };
|