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