getbox 1.3.1 → 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/CONTEXT.md CHANGED
@@ -11,8 +11,8 @@ import { withBox } from "getbox/context";
11
11
 
12
12
  withBox(() => {
13
13
  // All code in this scope can resolve dependencies
14
- const app = resolve(App);
15
- app.start();
14
+ const service = inject(UserService);
15
+ service.createUser("Alice");
16
16
  });
17
17
  ```
18
18
 
@@ -23,24 +23,24 @@ import { Box } from "getbox";
23
23
  import { withBox } from "getbox/context";
24
24
 
25
25
  const box = new Box();
26
- Box.mock(box, LoggerFactory, new TestLogger());
26
+ Box.mock(box, LoggerFactory, new MockLogger());
27
27
 
28
28
  withBox(box, () => {
29
- const app = resolve(App);
30
- app.start();
29
+ const service = inject(UserService);
30
+ service.createUser("Alice");
31
31
  });
32
32
  ```
33
33
 
34
34
  ## Resolving dependencies
35
35
 
36
- With the context pattern, classes can use `resolve` directly as field initializers. No `static init` method is needed.
36
+ With the context pattern, classes can use `inject` directly as field initializers. No `static init` property is needed.
37
37
 
38
38
  ```ts
39
- import { resolve } from "getbox/context";
39
+ import { inject } from "getbox/context";
40
40
 
41
41
  class UserService {
42
- public db = resolve(Database);
43
- public logger = resolve(LoggerFactory);
42
+ public db = inject(Database);
43
+ public logger = inject(LoggerFactory);
44
44
 
45
45
  createUser(name: string) {
46
46
  this.logger.log(`Creating user: ${name}`);
@@ -63,43 +63,28 @@ class UserService {
63
63
 
64
64
  ## Resolving multiple dependencies
65
65
 
66
- Use `resolveAll` to resolve multiple constructors at once. Accepts an object or array of constructors.
66
+ Pass an array or object of constructors to `inject` to resolve multiple at once.
67
67
 
68
68
  ```ts
69
69
  withBox(() => {
70
70
  // Object form
71
- const { db, logger } = resolveAll({ db: Database, logger: LoggerFactory });
71
+ const { db, logger } = inject({ db: Database, logger: LoggerFactory });
72
72
 
73
73
  // Array form
74
- const [db2, logger2] = resolveAll([Database, LoggerFactory]);
74
+ const [db2, logger2] = inject([Database, LoggerFactory]);
75
75
 
76
76
  console.log(db === db2); // true (cached)
77
77
  console.log(logger === logger2); // true (cached)
78
78
  });
79
79
  ```
80
80
 
81
- ## Resolving constructor parameters
82
-
83
- Use `construct` to create a class instance with resolved constructor parameters.
84
-
85
- ```ts
86
- class UserService {
87
- constructor(private db: Database, private logger: Logger) {}
88
- }
89
-
90
- withBox(() => {
91
- const service = construct(UserService).get(Database, LoggerFactory);
92
- service.createUser("Alice");
93
- });
94
- ```
95
-
96
81
  ## Accessing the box
97
82
 
98
- Use `useBox()` to get the current box from the scope. This is useful when you need full access to the box API.
83
+ Use `getBox()` to get the current box from the scope. This is useful when you need full access to the box API.
99
84
 
100
85
  ```ts
101
86
  withBox(() => {
102
- const box = useBox();
87
+ const box = getBox();
103
88
  const db = box.new(Database);
104
89
  });
105
90
  ```
@@ -110,11 +95,11 @@ Each `withBox` call creates an independent scope. Nested scopes do not share the
110
95
 
111
96
  ```ts
112
97
  withBox(() => {
113
- const db = resolve(Database);
98
+ const db = inject(Database);
114
99
 
115
- // Inner scope gets a fresh box
100
+ // Inner scope gets a new box
116
101
  withBox(() => {
117
- const db2 = resolve(Database);
102
+ const db2 = inject(Database);
118
103
  console.log(db === db2); // false (different scope)
119
104
  });
120
105
  });
package/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  ### Lightweight dependency injection for TypeScript.
4
4
 
5
- `getbox` provides a simple way of managing dependencies in TypeScript applications. Dependencies are defined as constructors that act as interfaces for the values they resolve.
5
+ `getbox` is a lightweight inversion of control container for TypeScript. Constructors declare their own dependencies, keeping instantiation logic colocated with the type that owns it.
6
6
 
7
- Callers know the type of the value they need, but not how it will be derived. The box resolves constructors lazily and caches instances automatically.
7
+ Callers depend on types, not implementations. The box resolves and caches instances automatically on first use.
8
8
 
9
9
  ## Installation
10
10
 
@@ -16,121 +16,127 @@ npm install getbox
16
16
 
17
17
  `getbox` has a very small API surface. You typically only need `box.get()` and optionally `static init` or the `factory` helper.
18
18
 
19
- For an alternative pattern using AsyncLocalStorage where classes can resolve dependencies directly in their constructors, see [getbox/context](./CONTEXT.md).
19
+ For an alternative pattern using AsyncLocalStorage where classes can resolve dependencies directly, see [getbox/context](./CONTEXT.md).
20
20
 
21
- ### Create a class
21
+ ### Quick start
22
22
 
23
- Classes are instantiated once and cached. Subsequent calls return the cached instance.
23
+ Create a `Box` instance and call `box.get()` to resolve instances. The box automatically resolves dependencies and caches every instance, so shared dependencies always point to the same reference.
24
24
 
25
25
  ```ts
26
- // printer.ts
27
- export class Printer {
28
- print(text: string): string {
26
+ import { Box } from "getbox";
27
+
28
+ class Printer {
29
+ print(text: string) {
29
30
  return text.toUpperCase();
30
31
  }
31
32
  }
32
- ```
33
-
34
- ### Use in another class
35
-
36
- The `static init` property tells the box what dependencies to pass when instantiating the class.
37
-
38
- ```ts
39
- // office.ts
40
- import { Box } from "getbox";
41
- import { Printer } from "./printer";
42
33
 
43
- export class Office {
34
+ class Office {
44
35
  constructor(public printer: Printer) {}
45
36
 
46
37
  static init = Box.init(Office).get(Printer);
47
38
  }
48
- ```
49
-
50
- ### Use in application
51
-
52
- Create a Box instance to hold cached instances.
53
-
54
- When initializing a class, any dependencies it has will also be cached, ensuring that shared dependencies use the same instance.
55
-
56
- ```ts
57
- // main.ts
58
- import { Box } from "getbox";
59
- import { Office } from "./office";
60
- import { Printer } from "./printer";
61
39
 
62
40
  const box = new Box();
63
41
 
64
42
  const office = box.get(Office);
65
43
  office.printer.print("hello world");
66
44
 
67
- // Instances are cached and shared
45
+ // Dependencies are cached and shared
68
46
  const printer = box.get(Printer);
69
47
  console.log(office.printer === printer); // true
70
48
  ```
71
49
 
72
- ## Transient instances
50
+ ## Constructors
73
51
 
74
- Use `box.new()` to create a new instance each time without caching. This is useful for instances that should not be shared.
52
+ Constructors define what the box resolves. `getbox` supports classes, factories, computed values, and constants. Because constructors act as interfaces, the underlying implementation can change without affecting any consumer.
53
+
54
+ ### Classes
55
+
56
+ Define a `static init` property to allow the box to resolve classes that have constructor parameters. Classes with no parameters do not require it.
75
57
 
76
58
  ```ts
77
- // main.ts
78
59
  import { Box } from "getbox";
79
60
 
80
- class Database {
81
- connect() {
82
- /* ... */
61
+ class UserService {
62
+ constructor(private db: Database, private logger: Logger) {}
63
+
64
+ static init = Box.init(UserService).get(Database, LoggerFactory);
65
+
66
+ createUser(name: string) {
67
+ this.logger.log(`Creating user: ${name}`);
83
68
  }
84
69
  }
85
70
 
86
71
  const box = new Box();
72
+ const service = box.get(UserService);
73
+ service.createUser("Alice");
74
+ ```
87
75
 
88
- const db1 = box.new(Database);
89
- const db2 = box.new(Database);
76
+ `Box.init` is shorthand for writing the `static init` function yourself.
90
77
 
91
- console.log(db1 === db2); // false
78
+ ```ts
79
+ class UserService {
80
+ constructor(private db: Database, private logger: Logger) {}
81
+
82
+ static init(box: Box) {
83
+ return new UserService(box.get(Database), box.get(LoggerFactory));
84
+ }
85
+ }
92
86
  ```
93
87
 
94
- ## Factory functions
88
+ If `static init` is defined, it takes priority over the class constructor.
89
+
90
+ ### Factory functions
95
91
 
96
92
  Use the `factory` helper to create function-based constructors instead of classes. Factories work well with interfaces for better abstraction.
97
93
 
98
94
  ```ts
99
- // logger.ts
100
95
  import { Box, factory } from "getbox";
101
96
 
102
- export interface Logger {
97
+ interface Logger {
103
98
  log(message: string): void;
104
99
  }
105
100
 
106
- const LoggerFactory = factory((box: Box): Logger => {
107
- return {
108
- log(message: string): void {
109
- console.log(`[LOG] ${message}`);
110
- },
111
- };
112
- });
101
+ const LoggerFactory = factory<Logger>(() => ({
102
+ log(message: string) {
103
+ console.log(`[LOG] ${message}`);
104
+ },
105
+ }));
106
+
107
+ const box = new Box();
108
+ const logger = box.get(LoggerFactory);
109
+
110
+ logger.log("hello world");
113
111
  ```
114
112
 
115
- ```ts
116
- // service.ts
117
- import { Box } from "getbox";
118
- import { Logger, LoggerFactory } from "./logger";
113
+ ### Computed values
119
114
 
120
- export class UserService {
121
- constructor(private logger: Logger) {}
115
+ Use the `computed` helper to compute a value from the box without caching the result.
122
116
 
123
- static init = Box.init(UserService).get(LoggerFactory);
117
+ ```ts
118
+ import { Box, computed } from "getbox";
124
119
 
125
- createUser(name: string) {
126
- this.logger.log(`Creating user: ${name}`);
127
- }
120
+ class Config {
121
+ baseUrl = "https://example.com";
128
122
  }
123
+
124
+ const RequestContext = computed((box) => ({
125
+ baseUrl: box.get(Config).baseUrl,
126
+ timestamp: Date.now(),
127
+ }));
128
+
129
+ const box = new Box();
130
+
131
+ const ctx1 = box.get(RequestContext);
132
+ const ctx2 = box.get(RequestContext);
133
+
134
+ console.log(ctx1 === ctx2); // false
129
135
  ```
130
136
 
131
- ## Constants
137
+ ### Constants
132
138
 
133
- Use the `constant` helper to register constant values without needing a factory or class. Constant values are never cached since they are already fixed.
139
+ Use the `constant` helper to wrap a fixed value as a constructor. Constant values are already fixed and do not need caching.
134
140
 
135
141
  ```ts
136
142
  import { Box, constant } from "getbox";
@@ -144,124 +150,54 @@ const Config = constant({
144
150
 
145
151
  const box = new Box();
146
152
 
147
- const apiUrl = box.get(ApiUrl);
148
- const port = box.get(Port);
149
- const config = box.get(Config);
150
-
151
- console.log(apiUrl); // "https://api.example.com"
152
- console.log(port); // 3000
153
- console.log(config.timeout); // 5000
153
+ console.log(box.get(ApiUrl)); // "https://api.example.com"
154
+ console.log(box.get(Port)); // 3000
155
+ console.log(box.get(Config).timeout); // 5000
154
156
  ```
155
157
 
156
- Since constructors act as interfaces, a `constant` can later be replaced with a `factory` without changing any callers.
158
+ ## Resolving instances
157
159
 
158
- ```ts
159
- const ApiUrl = factory((box: Box) => {
160
- const config = box.get(Config);
161
- return `${config.baseUrl}/api`;
162
- });
163
-
164
- const apiUrl = box.get(ApiUrl); // "https://example.com/api"
165
- ```
166
-
167
- ## Transient factories
168
-
169
- Use the `transient` helper to create a factory whose result is never cached. The factory is called on every resolution, even when retrieved via `box.get()`.
170
-
171
- ```ts
172
- import { Box, transient } from "getbox";
173
-
174
- const RequestId = transient(() => crypto.randomUUID());
175
-
176
- const box = new Box();
177
-
178
- const id1 = box.get(RequestId);
179
- const id2 = box.get(RequestId);
180
-
181
- console.log(id1 === id2); // false
182
- ```
183
-
184
- ## Resolving multiple constructors
185
-
186
- Use `box.all.get()` to resolve multiple constructors at once. Pass an object to get an object of instances, or an array to get an array of instances.
187
-
188
- ```ts
189
- import { Box } from "getbox";
190
-
191
- const box = new Box();
192
-
193
- // Object form
194
- const { db, logger } = box.all.get({ db: Database, logger: LoggerFactory });
195
-
196
- // Array form
197
- const [db2, logger2] = box.all.get([Database, LoggerFactory]);
198
-
199
- console.log(db === db2); // true (cached)
200
- console.log(logger === logger2); // true (cached)
201
- ```
202
-
203
- Use `box.all.new()` to resolve multiple constructors as transient instances.
204
-
205
- ```ts
206
- const { db } = box.all.new({ db: Database });
207
- const [db2] = box.all.new([Database]);
208
-
209
- console.log(db === db2); // false (transient)
210
- ```
211
-
212
- ## Class constructors
213
-
214
- Use `Box.init` to allow resolving classes that have constructor parameters.
160
+ Use `box.get()` to resolve a cached instance. The box resolves the constructor on first call and returns the same instance on every subsequent call. Use `box.new()` to always get a new instance without caching.
215
161
 
216
162
  ```ts
217
163
  import { Box } from "getbox";
218
164
 
219
- class UserService {
220
- constructor(private db: Database, private logger: Logger) {}
221
-
222
- static init = Box.init(UserService).get(Database, LoggerFactory);
223
-
224
- createUser(name: string) {
225
- this.logger.log(`Creating user: ${name}`);
165
+ class Database {
166
+ connect() {
167
+ /* ... */
226
168
  }
227
169
  }
228
170
 
229
171
  const box = new Box();
230
- const service = box.get(UserService);
231
- service.createUser("Alice");
232
- ```
233
172
 
234
- `Box.init` is shorthand for writing the `static init` method yourself.
173
+ const db1 = box.get(Database);
174
+ const db2 = box.get(Database);
175
+ console.log(db1 === db2); // true (cached)
235
176
 
236
- ```ts
237
- class UserService {
238
- constructor(private db: Database, private logger: Logger) {}
239
-
240
- static init(box: Box) {
241
- return new UserService(box.get(Database), box.get(LoggerFactory));
242
- }
243
- }
177
+ const db3 = box.new(Database);
178
+ const db4 = box.new(Database);
179
+ console.log(db3 === db4); // false (never cached)
244
180
  ```
245
181
 
246
- Use `box.for()` when you need custom logic alongside dependency resolution.
182
+ Both `box.get()` and `box.new()` also accept an array or object of constructors to resolve multiple at once.
247
183
 
248
184
  ```ts
249
- class UserService {
250
- constructor(private db: Database, private logger: Logger) {}
185
+ const box = new Box();
251
186
 
252
- static init(box: Box) {
253
- const logger = box.get(LoggerFactory);
254
- logger.log("Initializing UserService");
255
- return box.for(UserService).get(Database, LoggerFactory);
256
- }
257
- }
187
+ // Cached
188
+ const { db, logger } = box.get({ db: Database, logger: LoggerFactory });
189
+ const [db2, logger2] = box.get([Database, LoggerFactory]);
190
+
191
+ // New instances
192
+ const { db3 } = box.new({ db3: Database });
193
+ const [db4] = box.new([Database]);
258
194
  ```
259
195
 
260
- Classes with no constructor parameters are resolved automatically without needing `static init`. If `static init` is defined, it takes priority over the default constructor.
196
+ > `box.get()` does not cache `computed` or `constant` values.
261
197
 
262
198
  ## Mocking
263
199
 
264
- You can mock dependencies for testing using `Box.mock`. This is particularly useful with factories and interfaces.
200
+ You can mock dependencies for testing using `Box.mock`.
265
201
 
266
202
  ```ts
267
203
  // service.test.ts
@@ -291,22 +227,26 @@ console.log(mockLogger.messages); // ["Creating user: Alice"]
291
227
 
292
228
  Use `Box.clear` to remove cached instances. Pass a specific constructor to clear a single entry, or omit it to clear all cached instances.
293
229
 
230
+ Returns `true` if an instance was removed, `false` otherwise.
231
+
294
232
  ```ts
295
233
  const box = new Box();
296
234
 
297
235
  const db = box.get(Database);
298
236
 
299
237
  // Clear a specific constructor
300
- Box.clear(box, Database);
238
+ const cleared = Box.clear(box, Database);
239
+ console.log(cleared);
301
240
  console.log(box.get(Database) === db); // false (new instance)
302
241
 
303
242
  // Clear all cached instances
304
- Box.clear(box);
243
+ const clearedAll = Box.clear(box);
244
+ console.log(clearedAll);
305
245
  ```
306
246
 
307
247
  ## Circular dependencies
308
248
 
309
- `getbox` does not prevent circular dependencies. You should structure your code to avoid circular imports between modules.
249
+ `getbox` does not prevent circular dependencies. You should structure your code to avoid them.
310
250
 
311
251
  ## License
312
252
 
package/dist/context.cjs CHANGED
@@ -8,35 +8,19 @@ function withBox(boxOrFn, fn) {
8
8
  return storage.run(boxOrFn, fn);
9
9
  }
10
10
  /**
11
- * Returns the current {@link Box} from the active {@link withBox} scope.
12
- * Throws if called outside a scope.
11
+ * Returns the current Box from the active Box scope.
12
+ * Throws if called outside a Box scope.
13
13
  */
14
- function useBox() {
14
+ function getBox() {
15
15
  const box = storage.getStore();
16
- if (!box) throw new Error("useBox() must be called within a withBox() scope");
17
- return box;
16
+ if (box) return box;
17
+ throw new Error("getBox() must be called within a withBox() scope");
18
18
  }
19
- /**
20
- * Resolves a cached instance from the current {@link withBox} scope.
21
- * Shorthand for `useBox().get(constructor)`.
22
- */
23
- function resolve(constructor) {
24
- return useBox().get(constructor);
25
- }
26
- function resolveAll(constructors) {
27
- return useBox().all.get(constructors);
28
- }
29
- /**
30
- * Returns a builder for creating a class instance with resolved constructor
31
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
32
- */
33
- function construct(constructor) {
34
- return useBox().for(constructor);
19
+ function inject(arg) {
20
+ return getBox().get(arg);
35
21
  }
36
22
 
37
23
  //#endregion
38
- exports.construct = construct;
39
- exports.resolve = resolve;
40
- exports.resolveAll = resolveAll;
41
- exports.useBox = useBox;
24
+ exports.getBox = getBox;
25
+ exports.inject = inject;
42
26
  exports.withBox = withBox;
@@ -1,33 +1,27 @@
1
- import { Box, Construct, Constructor, ConstructorInstanceType } from "./index.cjs";
1
+ import { Box, Constructor, ConstructorInstanceType } from "./index.cjs";
2
2
 
3
3
  //#region src/context.d.ts
4
4
 
5
5
  /**
6
- * Runs a function within a scoped {@link Box} context using AsyncLocalStorage.
7
- * Creates a fresh Box if none is provided.
6
+ * Runs a function within a Box scope using AsyncLocalStorage.
7
+ * Creates a new Box if none is provided.
8
+ *
9
+ * If the callback function throws an error, the error is thrown by withBox too.
8
10
  */
9
11
  declare function withBox<T>(fn: () => T): T;
10
12
  declare function withBox<T>(box: Box, fn: () => T): T;
11
13
  /**
12
- * Returns the current {@link Box} from the active {@link withBox} scope.
13
- * Throws if called outside a scope.
14
+ * Returns the current Box from the active Box scope.
15
+ * Throws if called outside a Box scope.
14
16
  */
15
- declare function useBox(): Box;
17
+ declare function getBox(): Box;
16
18
  /**
17
- * Resolves a cached instance from the current {@link withBox} scope.
18
- * Shorthand for `useBox().get(constructor)`.
19
+ * Resolves instances from the active Box scope.
20
+ * Accepts a single constructor, an array of constructors, or an object map of constructors.
21
+ * Throws if called outside a Box scope.
19
22
  */
20
- declare function resolve<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
21
- /**
22
- * Resolves multiple cached instances from the current {@link withBox} scope.
23
- * Shorthand for `useBox().all.get(constructors)`.
24
- */
25
- declare function resolveAll<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
26
- declare function resolveAll<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
27
- /**
28
- * Returns a builder for creating a class instance with resolved constructor
29
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
30
- */
31
- declare function construct<T extends new (...args: any) => any>(constructor: T): Construct<T>;
23
+ declare function inject<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
24
+ declare function inject<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
25
+ declare function inject<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
32
26
  //#endregion
33
- export { construct, resolve, resolveAll, useBox, withBox };
27
+ export { getBox, inject, withBox };
@@ -1,33 +1,27 @@
1
- import { Box, Construct, Constructor, ConstructorInstanceType } from "./index.mjs";
1
+ import { Box, Constructor, ConstructorInstanceType } from "./index.mjs";
2
2
 
3
3
  //#region src/context.d.ts
4
4
 
5
5
  /**
6
- * Runs a function within a scoped {@link Box} context using AsyncLocalStorage.
7
- * Creates a fresh Box if none is provided.
6
+ * Runs a function within a Box scope using AsyncLocalStorage.
7
+ * Creates a new Box if none is provided.
8
+ *
9
+ * If the callback function throws an error, the error is thrown by withBox too.
8
10
  */
9
11
  declare function withBox<T>(fn: () => T): T;
10
12
  declare function withBox<T>(box: Box, fn: () => T): T;
11
13
  /**
12
- * Returns the current {@link Box} from the active {@link withBox} scope.
13
- * Throws if called outside a scope.
14
+ * Returns the current Box from the active Box scope.
15
+ * Throws if called outside a Box scope.
14
16
  */
15
- declare function useBox(): Box;
17
+ declare function getBox(): Box;
16
18
  /**
17
- * Resolves a cached instance from the current {@link withBox} scope.
18
- * Shorthand for `useBox().get(constructor)`.
19
+ * Resolves instances from the active Box scope.
20
+ * Accepts a single constructor, an array of constructors, or an object map of constructors.
21
+ * Throws if called outside a Box scope.
19
22
  */
20
- declare function resolve<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
21
- /**
22
- * Resolves multiple cached instances from the current {@link withBox} scope.
23
- * Shorthand for `useBox().all.get(constructors)`.
24
- */
25
- declare function resolveAll<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
26
- declare function resolveAll<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
27
- /**
28
- * Returns a builder for creating a class instance with resolved constructor
29
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
30
- */
31
- declare function construct<T extends new (...args: any) => any>(constructor: T): Construct<T>;
23
+ declare function inject<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
24
+ declare function inject<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
25
+ declare function inject<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
32
26
  //#endregion
33
- export { construct, resolve, resolveAll, useBox, withBox };
27
+ export { getBox, inject, withBox };
package/dist/context.mjs CHANGED
@@ -8,31 +8,17 @@ function withBox(boxOrFn, fn) {
8
8
  return storage.run(boxOrFn, fn);
9
9
  }
10
10
  /**
11
- * Returns the current {@link Box} from the active {@link withBox} scope.
12
- * Throws if called outside a scope.
11
+ * Returns the current Box from the active Box scope.
12
+ * Throws if called outside a Box scope.
13
13
  */
14
- function useBox() {
14
+ function getBox() {
15
15
  const box = storage.getStore();
16
- if (!box) throw new Error("useBox() must be called within a withBox() scope");
17
- return box;
16
+ if (box) return box;
17
+ throw new Error("getBox() must be called within a withBox() scope");
18
18
  }
19
- /**
20
- * Resolves a cached instance from the current {@link withBox} scope.
21
- * Shorthand for `useBox().get(constructor)`.
22
- */
23
- function resolve(constructor) {
24
- return useBox().get(constructor);
25
- }
26
- function resolveAll(constructors) {
27
- return useBox().all.get(constructors);
28
- }
29
- /**
30
- * Returns a builder for creating a class instance with resolved constructor
31
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
32
- */
33
- function construct(constructor) {
34
- return useBox().for(constructor);
19
+ function inject(arg) {
20
+ return getBox().get(arg);
35
21
  }
36
22
 
37
23
  //#endregion
38
- export { construct, resolve, resolveAll, useBox, withBox };
24
+ export { getBox, inject, withBox };