@teqfw/di 2.7.0 → 2.8.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/ai/usage.md DELETED
@@ -1,201 +0,0 @@
1
- # usage.md
2
-
3
- Version: 20260710
4
-
5
- ## Purpose
6
-
7
- This document shows canonical usage patterns for the container. Examples are intentionally short and prioritize supported, recommended forms over convenience shorthand.
8
-
9
- ## Canonical Module Descriptor
10
-
11
- The canonical form of `__deps__` is hierarchical and keyed by export name.
12
-
13
- ```js
14
- // @ts-check
15
-
16
- export const __deps__ = {
17
- default: {
18
- cast: "App_Helper_Cast$",
19
- },
20
- };
21
-
22
- export default class App_Root {
23
- /**
24
- * @param {{cast: (value: unknown) => string}} deps
25
- */
26
- constructor({ cast }) {
27
- return {
28
- configure(params = {}) {
29
- return {
30
- name: cast(params.name ?? "app"),
31
- };
32
- },
33
- };
34
- }
35
- }
36
- ```
37
-
38
- Dependency module:
39
-
40
- ```js
41
- // @ts-check
42
-
43
- export default function App_Helper_Cast() {
44
- return function cast(value) {
45
- return String(value);
46
- };
47
- }
48
- ```
49
-
50
- Rules:
51
-
52
- - the hierarchical export-scoped form is canonical;
53
- - each export entry maps constructor dependency names to CDC identifiers;
54
- - if `__deps__` is omitted, the module has no declared dependencies;
55
- - dependencies are resolved recursively before instantiation.
56
-
57
- ## Canonical Container Setup
58
-
59
- The container is configured in the composition root before the first resolution.
60
-
61
- ```js
62
- import path from "node:path";
63
- import { fileURLToPath } from "node:url";
64
- import Container from "@teqfw/di";
65
-
66
- const __filename = fileURLToPath(import.meta.url);
67
- const __dirname = path.dirname(__filename);
68
-
69
- const container = new Container();
70
- container.addNamespaceRoot("App_", path.resolve(__dirname, "./src/App"), ".mjs");
71
- ```
72
-
73
- Namespace roots may also use URL-backed module-specifier bases:
74
-
75
- ```js
76
- container.addNamespaceRoot("App_", "https://cdn.example.com/app", ".mjs");
77
- ```
78
-
79
- ## Resolve Root Dependency
80
-
81
- Applications typically resolve one root dependency and let the container build the full object graph.
82
-
83
- ```js
84
- const root = await container.get("App_Root$");
85
- console.log(root.configure({name: 123}).name);
86
- console.log(Object.isFrozen(root));
87
- ```
88
-
89
- ## Named Export
90
-
91
- Named exports use the `__ExportName` segment in the CDC and the same hierarchical `__deps__` structure.
92
-
93
- ```js
94
- export const __deps__ = {
95
- default: {
96
- cast: "App_Helper_Cast$",
97
- },
98
- Factory: {
99
- cast: "App_Helper_Cast$",
100
- },
101
- };
102
-
103
- export default class RuntimeWrapper {
104
- constructor() {
105
- return {mode: "runtime"};
106
- }
107
- }
108
-
109
- export class Factory {
110
- constructor({ cast }) {
111
- this.configure = function (params = {}) {
112
- return {
113
- mode: "factory",
114
- name: cast(params.name ?? "app"),
115
- };
116
- };
117
- }
118
- }
119
- ```
120
-
121
- Resolution examples:
122
-
123
- ```js
124
- const runtime = await container.get("App_Module$");
125
- const factory = await container.get("App_Module__Factory$");
126
- ```
127
-
128
- Without a lifecycle marker, the export is resolved as-is — the class or function itself, not an instance:
129
-
130
- ```js
131
- const FactoryClass = await container.get("App_Module__Factory");
132
- const factory = new FactoryClass({cast: resolvedCast});
133
- ```
134
-
135
- ## Singleton And Transient
136
-
137
- Common lifecycle-based compositions:
138
-
139
- ```txt
140
- App_Service$
141
- App_Task$$
142
- App_Task$$$
143
- ```
144
-
145
- - `$` creates and reuses a singleton instance;
146
- - `$$` creates a new instance for each request;
147
- - `$$$` is a transient alias in the current implementation.
148
-
149
- ## Wrappers
150
-
151
- Wrapper exports are selected by CDC suffixes and are applied after postprocess hooks.
152
-
153
- ```txt
154
- App_Service$$_wrapLog_wrapTrace
155
- ```
156
-
157
- This pattern is useful when runtime behavior should be decorated without changing the service module contract.
158
-
159
- ## Platform Modules
160
-
161
- CDC may refer to platform modules directly.
162
-
163
- ```txt
164
- node:fs
165
- npm:@humanfs/core
166
- node:worker_threads
167
- ```
168
-
169
- These forms resolve the selected platform module export as-is unless a lifecycle marker is explicitly added.
170
-
171
- ## Non-Canonical Shorthand
172
-
173
- A flat `__deps__` object is a supported shorthand for default-export-only modules, but it is not the canonical model.
174
-
175
- ```js
176
- export const __deps__ = {
177
- cast: "App_Helper_Cast$",
178
- };
179
-
180
- export default class App_Short {
181
- constructor({ cast }) {
182
- this.cast = cast;
183
- }
184
- }
185
- ```
186
-
187
- Prefer the hierarchical form for new integrations and for any module that exposes named exports.
188
-
189
- ## Empty Descriptor
190
-
191
- Modules with no declared dependencies omit `__deps__` entirely.
192
-
193
- ```js
194
- export default class App_Empty {
195
- constructor() {
196
- this.ready = function () {
197
- return true;
198
- };
199
- }
200
- }
201
- ```