@nwire/container 0.7.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/LICENSE +21 -0
- package/README.md +56 -0
- package/dist/container.d.ts +37 -0
- package/dist/container.d.ts.map +1 -0
- package/dist/container.js +40 -0
- package/dist/container.js.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Gefter / 200apps Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @nwire/container
|
|
2
|
+
|
|
3
|
+
> DI seam — the four-method `Container` interface every Nwire package composes through.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
A small `Container` interface (`resolve` / `has` / `register` / `createScope`) plus an in-memory default. Every other Nwire package depends only on this, so you can bring your own DI (plain object, Awilix, tsyringe) without rewriting the framework.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @nwire/container
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Standalone use
|
|
16
|
+
|
|
17
|
+
For developers who want a tiny DI seam in any TypeScript app — no Nwire dependency on the rest of the stack. Use `InMemoryContainer` as a typed registry and `createScope()` for per-request isolation.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { InMemoryContainer } from "@nwire/container";
|
|
21
|
+
|
|
22
|
+
const root = new InMemoryContainer();
|
|
23
|
+
root.register("db", () => new PrismaClient());
|
|
24
|
+
|
|
25
|
+
// per-request scope inherits the parent but isolates request-scoped values
|
|
26
|
+
const scope = root.createScope();
|
|
27
|
+
scope.register("user", () => req.user);
|
|
28
|
+
|
|
29
|
+
const db = scope.resolve<PrismaClient>("db"); // falls through to root
|
|
30
|
+
const user = scope.resolve<User>("user"); // local to this scope
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Within nwire-app
|
|
34
|
+
|
|
35
|
+
For developers using this package as part of the Nwire stack. Plugins register on the container during `boot`; handlers resolve via `ctx.resolve("key")`. Swap the implementation by passing a different `Container` to `createApp`.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { createApp } from "@nwire/forge";
|
|
39
|
+
import { AwilixNwireContainer } from "@nwire/container-awilix";
|
|
40
|
+
|
|
41
|
+
const container = new AwilixNwireContainer();
|
|
42
|
+
container.register("db", () => new PrismaClient());
|
|
43
|
+
|
|
44
|
+
const app = createApp("learnflow", { modules, container });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
- `Container` — interface every adapter implements.
|
|
50
|
+
- `InMemoryContainer` — `Map`-backed default; good for tests and small wires.
|
|
51
|
+
- `rootContainer` — a pre-built blank `InMemoryContainer` framework layers compose with.
|
|
52
|
+
|
|
53
|
+
## See also
|
|
54
|
+
|
|
55
|
+
- [Architecture sketch §05 — Foundation tier](../../architecture-sketch.html#packages)
|
|
56
|
+
- Sibling packages: [@nwire/container-awilix](../nwire-container-awilix), [@nwire/app](../nwire-app)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/container` — DI seam.
|
|
3
|
+
*
|
|
4
|
+
* Defines the four-method `Container` interface every Nwire layer composes
|
|
5
|
+
* through (`resolve` / `has` / `register` / `createScope`) and ships an
|
|
6
|
+
* in-memory default. Bring your own DI (plain object, Awilix, tsyringe)
|
|
7
|
+
* under any Nwire package without rewriting the framework.
|
|
8
|
+
*
|
|
9
|
+
* See: architecture-sketch.html §05 (Foundation tier).
|
|
10
|
+
*/
|
|
11
|
+
export interface Container {
|
|
12
|
+
/** Look up a registered value by name. Throws when unregistered. */
|
|
13
|
+
resolve<T = unknown>(name: string): T;
|
|
14
|
+
/** Register a value or factory under a name. Last-write wins. */
|
|
15
|
+
register<T = unknown>(name: string, factory: T | (() => T)): void;
|
|
16
|
+
/**
|
|
17
|
+
* Create a child scope that inherits parent registrations but lets the
|
|
18
|
+
* caller override or register locally. Used for per-request scopes —
|
|
19
|
+
* each HTTP request gets a scoped container so per-request values
|
|
20
|
+
* (envelope, user, tenant) don't leak across requests.
|
|
21
|
+
*/
|
|
22
|
+
createScope(): Container;
|
|
23
|
+
/** True when a name is registered (either locally or on a parent). */
|
|
24
|
+
has(name: string): boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare class InMemoryContainer implements Container {
|
|
27
|
+
private readonly parent?;
|
|
28
|
+
private readonly bindings;
|
|
29
|
+
constructor(parent?: InMemoryContainer | undefined);
|
|
30
|
+
resolve<T = unknown>(name: string): T;
|
|
31
|
+
register<T = unknown>(name: string, factory: T | (() => T)): void;
|
|
32
|
+
createScope(): Container;
|
|
33
|
+
has(name: string): boolean;
|
|
34
|
+
}
|
|
35
|
+
/** A blank root container, ready to be populated by the wire. */
|
|
36
|
+
export declare const rootContainer: Container;
|
|
37
|
+
//# sourceMappingURL=container.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container.d.ts","sourceRoot":"","sources":["../src/container.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,WAAW,SAAS;IACxB,oEAAoE;IACpE,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC;IAEtC,iEAAiE;IACjE,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC;IAElE;;;;;OAKG;IACH,WAAW,IAAI,SAAS,CAAC;IAEzB,sEAAsE;IACtE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAC5B;AAED,qBAAa,iBAAkB,YAAW,SAAS;IAGrC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IAFpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgD;gBAE5C,MAAM,CAAC,EAAE,iBAAiB,YAAA;IAEvD,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC;IASrC,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI;IAIjE,WAAW,IAAI,SAAS;IAIxB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;CAI3B;AAED,iEAAiE;AACjE,eAAO,MAAM,aAAa,EAAE,SAAmC,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/container` — DI seam.
|
|
3
|
+
*
|
|
4
|
+
* Defines the four-method `Container` interface every Nwire layer composes
|
|
5
|
+
* through (`resolve` / `has` / `register` / `createScope`) and ships an
|
|
6
|
+
* in-memory default. Bring your own DI (plain object, Awilix, tsyringe)
|
|
7
|
+
* under any Nwire package without rewriting the framework.
|
|
8
|
+
*
|
|
9
|
+
* See: architecture-sketch.html §05 (Foundation tier).
|
|
10
|
+
*/
|
|
11
|
+
export class InMemoryContainer {
|
|
12
|
+
parent;
|
|
13
|
+
bindings = new Map();
|
|
14
|
+
constructor(parent) {
|
|
15
|
+
this.parent = parent;
|
|
16
|
+
}
|
|
17
|
+
resolve(name) {
|
|
18
|
+
if (this.bindings.has(name)) {
|
|
19
|
+
const v = this.bindings.get(name);
|
|
20
|
+
return typeof v === "function" ? v() : v;
|
|
21
|
+
}
|
|
22
|
+
if (this.parent)
|
|
23
|
+
return this.parent.resolve(name);
|
|
24
|
+
throw new Error(`Container: no binding for "${name}"`);
|
|
25
|
+
}
|
|
26
|
+
register(name, factory) {
|
|
27
|
+
this.bindings.set(name, factory);
|
|
28
|
+
}
|
|
29
|
+
createScope() {
|
|
30
|
+
return new InMemoryContainer(this);
|
|
31
|
+
}
|
|
32
|
+
has(name) {
|
|
33
|
+
if (this.bindings.has(name))
|
|
34
|
+
return true;
|
|
35
|
+
return this.parent?.has(name) ?? false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** A blank root container, ready to be populated by the wire. */
|
|
39
|
+
export const rootContainer = new InMemoryContainer();
|
|
40
|
+
//# sourceMappingURL=container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container.js","sourceRoot":"","sources":["../src/container.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAqBH,MAAM,OAAO,iBAAiB;IAGC;IAFZ,QAAQ,GAAG,IAAI,GAAG,EAAqC,CAAC;IAEzE,YAA6B,MAA0B;QAA1B,WAAM,GAAN,MAAM,CAAoB;IAAG,CAAC;IAE3D,OAAO,CAAc,IAAY;QAC/B,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAClC,OAAO,OAAO,CAAC,KAAK,UAAU,CAAC,CAAC,CAAE,CAAa,EAAE,CAAC,CAAC,CAAE,CAAO,CAAC;QAC/D,CAAC;QACD,IAAI,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAI,IAAI,CAAC,CAAC;QACrD,MAAM,IAAI,KAAK,CAAC,8BAA8B,IAAI,GAAG,CAAC,CAAC;IACzD,CAAC;IAED,QAAQ,CAAc,IAAY,EAAE,OAAsB;QACxD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,OAAkB,CAAC,CAAC;IAC9C,CAAC;IAED,WAAW;QACT,OAAO,IAAI,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IAED,GAAG,CAAC,IAAY;QACd,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QACzC,OAAO,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC;IACzC,CAAC;CACF;AAED,iEAAiE;AACjE,MAAM,CAAC,MAAM,aAAa,GAAc,IAAI,iBAAiB,EAAE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nwire/container",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Nwire — DI container contract + InMemory default. Production adapters land as @nwire/container-awilix.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"container",
|
|
7
|
+
"di",
|
|
8
|
+
"ioc",
|
|
9
|
+
"nwire"
|
|
10
|
+
],
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/container.js",
|
|
17
|
+
"types": "./dist/container.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./dist/container.js",
|
|
21
|
+
"types": "./dist/container.d.ts"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^22.19.9",
|
|
29
|
+
"typescript": "^5.9.3",
|
|
30
|
+
"vitest": "^4.0.18"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc && node ../../scripts/fix-dist-extensions.mjs dist",
|
|
34
|
+
"dev": "tsc --watch",
|
|
35
|
+
"typecheck": "tsc --noEmit"
|
|
36
|
+
}
|
|
37
|
+
}
|