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