@theokit/di 0.2.1 → 0.3.1
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/CHANGELOG.md +12 -0
- package/README.md +24 -0
- package/dist/index.cjs +32 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -2
- package/dist/index.d.ts +20 -2
- package/dist/index.js +32 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 12e1f8a: The workspace declares `0.3.0`, the first release with `requestSeed`, so the `@theokit/di` peer range
|
|
8
|
+
of `@theokit/di-agent` (`>=0.3.0 <0.4`) is satisfied by what the workspace installs. The release
|
|
9
|
+
publishes 0.3.1.
|
|
10
|
+
- c03efc6: `Container.runInRequest(callback, { seed })` carries a per-request value, read with
|
|
11
|
+
`container.requestSeed<T>()`. It throws `ScopeViolationError` outside a request and the new
|
|
12
|
+
`MissingRequestSeedError` when the request was opened without a seed. Calls without options behave
|
|
13
|
+
as before (usetheokit/theokit-di#80).
|
|
14
|
+
|
|
3
15
|
## 0.2.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -56,6 +56,30 @@ const greeter = container.resolve(GreeterService);
|
|
|
56
56
|
console.log(greeter.greet("world")); // Hello, world!
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
+
## Request seed
|
|
60
|
+
|
|
61
|
+
`runInRequest` accepts a seed, the value the request serves (an inbound message, an HTTP request, a
|
|
62
|
+
session key). Read it with `container.requestSeed()` from any REQUEST-scoped factory or code running
|
|
63
|
+
inside the request. Nested requests each see their own seed.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { Container, Scope } from "@theokit/di";
|
|
67
|
+
|
|
68
|
+
const container = new Container();
|
|
69
|
+
container.register({
|
|
70
|
+
provide: "session",
|
|
71
|
+
useFactory: () => ({ key: container.requestSeed<string>() }),
|
|
72
|
+
scope: Scope.REQUEST,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const session = await container.runInRequest(() => container.resolveAsync("session"), {
|
|
76
|
+
seed: "user-42",
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`requestSeed()` throws `ScopeViolationError` outside a request, and `MissingRequestSeedError` in a
|
|
81
|
+
request opened without `{ seed }`.
|
|
82
|
+
|
|
59
83
|
## Polyglot strategy
|
|
60
84
|
|
|
61
85
|
`@theokit/di` is **intentionally TS-only**. DI containers are language-specific runtime constructs — Python uses `inspect`/`typing.get_type_hints`, Go uses `reflect`, etc. The cross-language story for the theokit ecosystem lives in the **contract layer** (OpenAPI from `@theokit/http`, SQL migrations + JSON schemas from `@theokit/orm`), not in the container.
|
package/dist/index.cjs
CHANGED
|
@@ -75,6 +75,15 @@ var ScopeViolationError = class extends Error {
|
|
|
75
75
|
super(`Scope violation for token ${describeToken(token)}: ${message}`), this.token = token;
|
|
76
76
|
}
|
|
77
77
|
};
|
|
78
|
+
var MissingRequestSeedError = class extends Error {
|
|
79
|
+
static {
|
|
80
|
+
__name(this, "MissingRequestSeedError");
|
|
81
|
+
}
|
|
82
|
+
name = "MissingRequestSeedError";
|
|
83
|
+
constructor() {
|
|
84
|
+
super("The active request scope has no seed. Pass { seed } to runInRequest: container.runInRequest(cb, { seed }).");
|
|
85
|
+
}
|
|
86
|
+
};
|
|
78
87
|
var MissingInjectableError = class extends Error {
|
|
79
88
|
static {
|
|
80
89
|
__name(this, "MissingInjectableError");
|
|
@@ -524,11 +533,14 @@ var Container = class {
|
|
|
524
533
|
* try/finally guarantees REQUEST-scoped instances are disposed
|
|
525
534
|
* even if the callback throws.
|
|
526
535
|
*/
|
|
527
|
-
async runInRequest(callback) {
|
|
536
|
+
async runInRequest(callback, options) {
|
|
528
537
|
this.assertNotDisposed();
|
|
538
|
+
const hasSeed = options !== void 0 && Object.hasOwn(options, "seed");
|
|
529
539
|
const store = {
|
|
530
540
|
cache: /* @__PURE__ */ new Map(),
|
|
531
|
-
instances: []
|
|
541
|
+
instances: [],
|
|
542
|
+
seed: hasSeed ? options.seed : void 0,
|
|
543
|
+
hasSeed
|
|
532
544
|
};
|
|
533
545
|
try {
|
|
534
546
|
return await this.requestStorage.run(store, async () => callback());
|
|
@@ -537,6 +549,23 @@ var Container = class {
|
|
|
537
549
|
}
|
|
538
550
|
}
|
|
539
551
|
/**
|
|
552
|
+
* The seed of the active request scope, as passed to
|
|
553
|
+
* `runInRequest(cb, { seed })`.
|
|
554
|
+
*
|
|
555
|
+
* @throws ScopeViolationError outside a request scope.
|
|
556
|
+
* @throws MissingRequestSeedError when the scope was opened without a seed.
|
|
557
|
+
*/
|
|
558
|
+
requestSeed() {
|
|
559
|
+
const store = this.requestStorage.getStore();
|
|
560
|
+
if (store === void 0) {
|
|
561
|
+
throw new ScopeViolationError("requestSeed", "requestSeed() requires container.runInRequest(...) to be active.");
|
|
562
|
+
}
|
|
563
|
+
if (!store.hasSeed) {
|
|
564
|
+
throw new MissingRequestSeedError();
|
|
565
|
+
}
|
|
566
|
+
return store.seed;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
540
569
|
* Debug helper — returns a snapshot of the dependency graph.
|
|
541
570
|
*
|
|
542
571
|
* Detects cycles in unused providers too (resolve-time
|
|
@@ -1157,6 +1186,7 @@ exports.InvalidExportError = InvalidExportError;
|
|
|
1157
1186
|
exports.InvalidModuleError = InvalidModuleError;
|
|
1158
1187
|
exports.METADATA_KEYS = METADATA_KEYS;
|
|
1159
1188
|
exports.MissingInjectableError = MissingInjectableError;
|
|
1189
|
+
exports.MissingRequestSeedError = MissingRequestSeedError;
|
|
1160
1190
|
exports.Module = Module;
|
|
1161
1191
|
exports.Optional = Optional;
|
|
1162
1192
|
exports.PostConstruct = PostConstruct;
|