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/CONTEXT.md CHANGED
@@ -63,15 +63,15 @@ class UserService {
63
63
 
64
64
  ## Resolving multiple dependencies
65
65
 
66
- Use `injectAll` 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 } = injectAll({ db: Database, logger: LoggerFactory });
71
+ const { db, logger } = inject({ db: Database, logger: LoggerFactory });
72
72
 
73
73
  // Array form
74
- const [db2, logger2] = injectAll([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)
@@ -80,11 +80,11 @@ withBox(() => {
80
80
 
81
81
  ## Accessing the box
82
82
 
83
- 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.
84
84
 
85
85
  ```ts
86
86
  withBox(() => {
87
- const box = useBox();
87
+ const box = getBox();
88
88
  const db = box.new(Database);
89
89
  });
90
90
  ```
package/README.md CHANGED
@@ -49,7 +49,7 @@ console.log(office.printer === printer); // true
49
49
 
50
50
  ## Constructors
51
51
 
52
- Constructors define what the box resolves. `getbox` supports classes, factories, derived values, and constants. Because constructors act as interfaces, the underlying implementation can change without affecting any consumer.
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
53
 
54
54
  ### Classes
55
55
 
@@ -73,7 +73,7 @@ const service = box.get(UserService);
73
73
  service.createUser("Alice");
74
74
  ```
75
75
 
76
- `Box.init` is shorthand for writing the `static init` property yourself.
76
+ `Box.init` is shorthand for writing the `static init` function yourself.
77
77
 
78
78
  ```ts
79
79
  class UserService {
@@ -85,20 +85,6 @@ class UserService {
85
85
  }
86
86
  ```
87
87
 
88
- Use `box.for()` when you need custom logic alongside dependency resolution.
89
-
90
- ```ts
91
- class UserService {
92
- constructor(private db: Database, private logger: Logger) {}
93
-
94
- static init(box: Box) {
95
- const logger = box.get(LoggerFactory);
96
- logger.log("Initializing UserService");
97
- return box.for(UserService).get(Database, LoggerFactory);
98
- }
99
- }
100
- ```
101
-
102
88
  If `static init` is defined, it takes priority over the class constructor.
103
89
 
104
90
  ### Factory functions
@@ -112,13 +98,11 @@ interface Logger {
112
98
  log(message: string): void;
113
99
  }
114
100
 
115
- const LoggerFactory = factory(
116
- (): Logger => ({
117
- log(message: string) {
118
- console.log(`[LOG] ${message}`);
119
- },
120
- }),
121
- );
101
+ const LoggerFactory = factory<Logger>(() => ({
102
+ log(message: string) {
103
+ console.log(`[LOG] ${message}`);
104
+ },
105
+ }));
122
106
 
123
107
  const box = new Box();
124
108
  const logger = box.get(LoggerFactory);
@@ -126,18 +110,18 @@ const logger = box.get(LoggerFactory);
126
110
  logger.log("hello world");
127
111
  ```
128
112
 
129
- ### Derived values
113
+ ### Computed values
130
114
 
131
- Use the `derive` helper to compute a value from the box without caching the result.
115
+ Use the `computed` helper to compute a value from the box without caching the result.
132
116
 
133
117
  ```ts
134
- import { Box, derive } from "getbox";
118
+ import { Box, computed } from "getbox";
135
119
 
136
120
  class Config {
137
121
  baseUrl = "https://example.com";
138
122
  }
139
123
 
140
- const RequestContext = derive((box) => ({
124
+ const RequestContext = computed((box) => ({
141
125
  baseUrl: box.get(Config).baseUrl,
142
126
  timestamp: Date.now(),
143
127
  }));
@@ -152,7 +136,7 @@ console.log(ctx1 === ctx2); // false
152
136
 
153
137
  ### Constants
154
138
 
155
- Use the `constant` helper to wrap a fixed value as a constructor. Constant values are never cached and always return the same stable reference.
139
+ Use the `constant` helper to wrap a fixed value as a constructor. Constant values are already fixed and do not need caching.
156
140
 
157
141
  ```ts
158
142
  import { Box, constant } from "getbox";
@@ -195,21 +179,21 @@ const db4 = box.new(Database);
195
179
  console.log(db3 === db4); // false (never cached)
196
180
  ```
197
181
 
198
- Use `box.all.get()` or `box.all.new()` to resolve multiple constructors at once. Pass an array to get an array of instances, or an object to get an object of instances.
182
+ Both `box.get()` and `box.new()` also accept an array or object of constructors to resolve multiple at once.
199
183
 
200
184
  ```ts
201
185
  const box = new Box();
202
186
 
203
187
  // Cached
204
- const { db, logger } = box.all.get({ db: Database, logger: LoggerFactory });
205
- const [db2, logger2] = box.all.get([Database, LoggerFactory]);
188
+ const { db, logger } = box.get({ db: Database, logger: LoggerFactory });
189
+ const [db2, logger2] = box.get([Database, LoggerFactory]);
206
190
 
207
191
  // New instances
208
- const { db3 } = box.all.new({ db3: Database });
209
- const [db4] = box.all.new([Database]);
192
+ const { db3 } = box.new({ db3: Database });
193
+ const [db4] = box.new([Database]);
210
194
  ```
211
195
 
212
- > `box.get()` does not cache `derive` or `constant` values.
196
+ > `box.get()` does not cache `computed` or `constant` values.
213
197
 
214
198
  ## Mocking
215
199
 
@@ -243,17 +227,21 @@ console.log(mockLogger.messages); // ["Creating user: Alice"]
243
227
 
244
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.
245
229
 
230
+ Returns `true` if an instance was removed, `false` otherwise.
231
+
246
232
  ```ts
247
233
  const box = new Box();
248
234
 
249
235
  const db = box.get(Database);
250
236
 
251
237
  // Clear a specific constructor
252
- Box.clear(box, Database);
238
+ const cleared = Box.clear(box, Database);
239
+ console.log(cleared);
253
240
  console.log(box.get(Database) === db); // false (new instance)
254
241
 
255
242
  // Clear all cached instances
256
- Box.clear(box);
243
+ const clearedAll = Box.clear(box);
244
+ console.log(clearedAll);
257
245
  ```
258
246
 
259
247
  ## Circular dependencies
package/dist/context.cjs CHANGED
@@ -8,48 +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 inject(constructor) {
24
- return useBox().get(constructor);
25
- }
26
- /**
27
- * @deprecated Use {@link inject} instead.
28
- */
29
- function resolve(constructor) {
30
- return inject(constructor);
31
- }
32
- function injectAll(constructors) {
33
- return useBox().all.get(constructors);
34
- }
35
- function resolveAll(constructors) {
36
- return injectAll(constructors);
37
- }
38
- /**
39
- * Returns a builder for creating a class instance with resolved constructor
40
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
41
- *
42
- * @deprecated
43
- */
44
- function construct(constructor) {
45
- return useBox().for(constructor);
19
+ function inject(arg) {
20
+ return getBox().get(arg);
46
21
  }
47
22
 
48
23
  //#endregion
49
- exports.construct = construct;
24
+ exports.getBox = getBox;
50
25
  exports.inject = inject;
51
- exports.injectAll = injectAll;
52
- exports.resolve = resolve;
53
- exports.resolveAll = resolveAll;
54
- exports.useBox = useBox;
55
26
  exports.withBox = withBox;
@@ -1,44 +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.
6
+ * Runs a function within a Box scope using AsyncLocalStorage.
7
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
23
  declare function inject<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
21
- /**
22
- * @deprecated Use {@link inject} instead.
23
- */
24
- declare function resolve<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
25
- /**
26
- * Resolves multiple cached instances from the current {@link withBox} scope.
27
- * Shorthand for `useBox().all.get(constructors)`.
28
- */
29
- declare function injectAll<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
30
- declare function injectAll<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
31
- /**
32
- * @deprecated Use {@link injectAll} instead.
33
- */
34
- declare function resolveAll<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
35
- declare function resolveAll<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
36
- /**
37
- * Returns a builder for creating a class instance with resolved constructor
38
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
39
- *
40
- * @deprecated
41
- */
42
- declare function construct<T extends new (...args: any) => any>(constructor: T): Construct<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]> };
43
26
  //#endregion
44
- export { construct, inject, injectAll, resolve, resolveAll, useBox, withBox };
27
+ export { getBox, inject, withBox };
@@ -1,44 +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.
6
+ * Runs a function within a Box scope using AsyncLocalStorage.
7
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
23
  declare function inject<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
21
- /**
22
- * @deprecated Use {@link inject} instead.
23
- */
24
- declare function resolve<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
25
- /**
26
- * Resolves multiple cached instances from the current {@link withBox} scope.
27
- * Shorthand for `useBox().all.get(constructors)`.
28
- */
29
- declare function injectAll<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
30
- declare function injectAll<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
31
- /**
32
- * @deprecated Use {@link injectAll} instead.
33
- */
34
- declare function resolveAll<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
35
- declare function resolveAll<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
36
- /**
37
- * Returns a builder for creating a class instance with resolved constructor
38
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
39
- *
40
- * @deprecated
41
- */
42
- declare function construct<T extends new (...args: any) => any>(constructor: T): Construct<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]> };
43
26
  //#endregion
44
- export { construct, inject, injectAll, resolve, resolveAll, useBox, withBox };
27
+ export { getBox, inject, withBox };
package/dist/context.mjs CHANGED
@@ -8,42 +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 inject(constructor) {
24
- return useBox().get(constructor);
25
- }
26
- /**
27
- * @deprecated Use {@link inject} instead.
28
- */
29
- function resolve(constructor) {
30
- return inject(constructor);
31
- }
32
- function injectAll(constructors) {
33
- return useBox().all.get(constructors);
34
- }
35
- function resolveAll(constructors) {
36
- return injectAll(constructors);
37
- }
38
- /**
39
- * Returns a builder for creating a class instance with resolved constructor
40
- * parameters from the current {@link withBox} scope. Shorthand for `useBox().for(constructor)`.
41
- *
42
- * @deprecated
43
- */
44
- function construct(constructor) {
45
- return useBox().for(constructor);
19
+ function inject(arg) {
20
+ return getBox().get(arg);
46
21
  }
47
22
 
48
23
  //#endregion
49
- export { construct, inject, injectAll, resolve, resolveAll, useBox, withBox };
24
+ export { getBox, inject, withBox };