@ahoo-wang/wow-generator 9.9.0 → 10.0.0-alpha.4

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.
Files changed (81) hide show
  1. package/README.md +99 -62
  2. package/README.zh-CN.md +85 -48
  3. package/dist/analysis/analyze.d.cts +5 -8
  4. package/dist/analysis/analyze.d.ts +5 -8
  5. package/dist/analysis/apiClients.d.cts +3 -4
  6. package/dist/analysis/apiClients.d.ts +3 -4
  7. package/dist/analysis/clientNames.d.cts +0 -20
  8. package/dist/analysis/clientNames.d.ts +0 -20
  9. package/dist/analysis/model.d.cts +2 -102
  10. package/dist/analysis/model.d.ts +2 -102
  11. package/dist/analysis/modelInfo.d.cts +12 -3
  12. package/dist/analysis/modelInfo.d.ts +12 -3
  13. package/dist/analysis/models.d.cts +3 -11
  14. package/dist/analysis/models.d.ts +3 -11
  15. package/dist/api/configuration.d.cts +1 -5
  16. package/dist/api/configuration.d.ts +1 -5
  17. package/dist/cli/program.d.cts +0 -5
  18. package/dist/cli/program.d.ts +0 -5
  19. package/dist/cli.cjs +9 -1
  20. package/dist/cli.cjs.map +1 -1
  21. package/dist/cli.js +102 -3
  22. package/dist/cli.js.map +1 -1
  23. package/dist/codeGenerator-CU1TGcUI.js +2407 -0
  24. package/dist/codeGenerator-CU1TGcUI.js.map +1 -0
  25. package/dist/codeGenerator-DzI0zCgM.cjs +27 -0
  26. package/dist/codeGenerator-DzI0zCgM.cjs.map +1 -0
  27. package/dist/emit/imports.d.cts +0 -9
  28. package/dist/emit/imports.d.ts +0 -9
  29. package/dist/emitters/decorators.d.cts +3 -18
  30. package/dist/emitters/decorators.d.ts +3 -18
  31. package/dist/emitters/emit.d.cts +2 -3
  32. package/dist/emitters/emit.d.ts +2 -3
  33. package/dist/emitters/manifestClients.d.cts +15 -0
  34. package/dist/emitters/manifestClients.d.ts +15 -0
  35. package/dist/emitters/models.d.cts +1 -1
  36. package/dist/emitters/models.d.ts +1 -1
  37. package/dist/index.cjs +1 -1
  38. package/dist/index.js +1 -1
  39. package/dist/input/configuration.d.cts +2 -4
  40. package/dist/input/configuration.d.ts +2 -4
  41. package/dist/input/parsers.d.cts +13 -0
  42. package/dist/input/parsers.d.ts +13 -0
  43. package/dist/manifest/analyze.d.cts +148 -0
  44. package/dist/manifest/analyze.d.ts +148 -0
  45. package/dist/manifest/document.d.cts +89 -0
  46. package/dist/manifest/document.d.ts +89 -0
  47. package/dist/naming/modelInfo.d.cts +1 -2
  48. package/dist/naming/modelInfo.d.ts +1 -2
  49. package/dist/naming/paths.d.cts +0 -5
  50. package/dist/naming/paths.d.ts +0 -5
  51. package/dist/output/outputStore.d.cts +2 -6
  52. package/dist/output/outputStore.d.ts +2 -6
  53. package/dist/pipeline/codeGenerator.d.cts +19 -11
  54. package/dist/pipeline/codeGenerator.d.ts +19 -11
  55. package/package.json +8 -8
  56. package/dist/analysis/aggregates.d.cts +0 -21
  57. package/dist/analysis/aggregates.d.ts +0 -21
  58. package/dist/codeGenerator-BMyiC0tW.cjs +0 -23
  59. package/dist/codeGenerator-BMyiC0tW.cjs.map +0 -1
  60. package/dist/codeGenerator-CsWf0pZy.js +0 -2572
  61. package/dist/codeGenerator-CsWf0pZy.js.map +0 -1
  62. package/dist/emitters/commandClients.d.cts +0 -14
  63. package/dist/emitters/commandClients.d.ts +0 -14
  64. package/dist/emitters/queryClients.d.cts +0 -11
  65. package/dist/emitters/queryClients.d.ts +0 -11
  66. package/dist/fetcherGeneratorCli.cjs +0 -3
  67. package/dist/fetcherGeneratorCli.cjs.map +0 -1
  68. package/dist/fetcherGeneratorCli.d.cts +0 -8
  69. package/dist/fetcherGeneratorCli.d.ts +0 -8
  70. package/dist/fetcherGeneratorCli.js +0 -6
  71. package/dist/fetcherGeneratorCli.js.map +0 -1
  72. package/dist/program-64WQvLq1.js +0 -103
  73. package/dist/program-64WQvLq1.js.map +0 -1
  74. package/dist/program-DWjbTSVJ.cjs +0 -10
  75. package/dist/program-DWjbTSVJ.cjs.map +0 -1
  76. package/dist/wow/conventions.d.cts +0 -154
  77. package/dist/wow/conventions.d.ts +0 -154
  78. package/dist/wow/model.d.cts +0 -116
  79. package/dist/wow/model.d.ts +0 -116
  80. package/dist/wow/resolveWowModel.d.cts +0 -21
  81. package/dist/wow/resolveWowModel.d.ts +0 -21
package/README.md CHANGED
@@ -1,13 +1,69 @@
1
1
  # `@ahoo-wang/wow-generator`
2
2
 
3
- Generate TypeScript models, Fetcher decorator clients, and Wow clients from a
4
- local or remote OpenAPI document.
3
+ Generate the typed TypeScript clients of a Wow service from its Manifest, and
4
+ TypeScript models and Fetcher decorator clients from any OpenAPI 3 document.
5
5
 
6
- ## Install and run
6
+ ## From a Wow Manifest
7
+
8
+ A Wow service describes its bounded context, aggregates, commands, events and
9
+ state in its Manifest (`GET /wow/manifest`, or the `manifest/baseline.json` a
10
+ service commits). The generator turns it into one client per aggregate on
11
+ `@ahoo-wang/wow-client`:
12
+
13
+ ```bash
14
+ pnpm add @ahoo-wang/fetcher @ahoo-wang/fetcher-eventstream @ahoo-wang/wow-client
15
+ pnpm add -D @ahoo-wang/wow-generator typescript
16
+ curl -s http://localhost:8080/wow/manifest > manifest.json
17
+ pnpm exec wow-generator generate \
18
+ --input ./manifest.json \
19
+ --output ./src/generated \
20
+ --ts-config-file-path ./tsconfig.json
21
+ ```
22
+
23
+ For each bounded context it writes `types.ts` (the command, event and state
24
+ types of the Manifest's schemas) and `boundedContext.ts` (its name, error codes
25
+ and wait hops), and for each aggregate:
26
+
27
+ - `aggregate.ts`: the aggregate's descriptor, its state fields and its event
28
+ union;
29
+ - `commandClient.ts`: `<Aggregate>CommandClient`, a method per command and a
30
+ `…Stream` twin that answers one result per hop of the wait;
31
+ - `queryClient.ts`: `<Aggregate>SnapshotQueryClient`,
32
+ `<Aggregate>EventQueryClient` and `<Aggregate>StateClient`.
33
+
34
+ <!-- typecheck-generated: example/eshop/web/src/generated/basket -->
35
+
36
+ ```ts
37
+ import { Fetcher } from '@ahoo-wang/fetcher';
38
+ import { filter } from '@ahoo-wang/wow-client/dsl';
39
+ import {
40
+ BasketCommandClient,
41
+ BasketSnapshotQueryClient,
42
+ } from './generated/index.js';
43
+
44
+ const fetcher = new Fetcher({ baseURL: 'http://localhost:8080' });
45
+ const basket = new BasketCommandClient({ fetcher });
46
+ const baskets = new BasketSnapshotQueryClient({ fetcher });
47
+
48
+ await basket.addBasketItem(
49
+ { productId: 'p-1', quantity: 2, unitPrice: '10.00' },
50
+ { id: 'buyer-1' },
51
+ );
52
+ const page = await baskets.paged({ filter: filter.matchAll() });
53
+ console.log(page.total);
54
+ ```
55
+
56
+ `--strict` fails a run that logged a warning, which a CI step that regenerates
57
+ the clients from a service's committed Manifest wants.
58
+
59
+ ## From an OpenAPI document
60
+
61
+ Any OpenAPI 3 document, a service's own routes or a third-party API, generates
62
+ a model per component schema and a Fetcher decorator client per tag:
7
63
 
8
64
  ```bash
9
65
  pnpm add @ahoo-wang/fetcher @ahoo-wang/fetcher-decorator \
10
- @ahoo-wang/fetcher-eventstream @ahoo-wang/wow-client
66
+ @ahoo-wang/fetcher-eventstream
11
67
  pnpm add -D @ahoo-wang/wow-generator typescript
12
68
  pnpm exec wow-generator generate \
13
69
  --input ./openapi.yaml \
@@ -15,20 +71,14 @@ pnpm exec wow-generator generate \
15
71
  --ts-config-file-path ./tsconfig.json
16
72
  ```
17
73
 
18
- The version follows Wow, so a minor release may contain breaking changes: keep the Wow packages on one minor with `save-prefix=~` or `--save-exact`, as [version ranges](https://wow.ahoo.me/guide/typescript/compatibility#version-ranges) explains.
74
+ Generated API clients are decorator classes, so the project that compiles them
75
+ needs `"experimentalDecorators": true` in its `tsconfig.json`. The document is
76
+ read as OpenAPI alone: schema keys, tags and path parameters mean what they say
77
+ in it, with no Wow conventions on top.
19
78
 
20
- The first line installs what the generated code imports at run time, the
21
- second the generator and TypeScript. The generator's peers (`fetcher`,
22
- `fetcher-decorator`, `fetcher-eventstream`, `wow-client`) are the runtime
23
- packages of the first line; `wow-client` has to be on the generator's minor
24
- version. Node 22.12 or later is required, and TypeScript 6 or later: CI tests
25
- 6.0 through the latest 7.x. The command
26
- used to be `fetcher-generator`; that name stays as an alias until v10 and
27
- prints a deprecation notice to stderr before it runs.
79
+ ## Versions
28
80
 
29
- Generated clients are decorator classes, so the project that compiles them
30
- needs `"experimentalDecorators": true` in its `tsconfig.json`. Released with
31
- Wow 9.2.0, from the same tag and with the same version.
81
+ The version follows Wow, so a minor release may contain breaking changes: keep the Wow packages on one minor with `save-prefix=~` or `--save-exact`, as [version ranges](https://wow.ahoo.me/typescript/compatibility#version-ranges) explains. `wow-client` has to be on the generator's minor version. Node 22.12 or later is required, and TypeScript 6 or later: CI tests 6.0 through the latest 7.x.
32
82
 
33
83
  A successful run prints its warnings, if any, and one summary line:
34
84
 
@@ -38,18 +88,18 @@ Generated 12 files into src/generated with /work/app/wow-generator.config.json,
38
88
 
39
89
  ## Options and exit codes
40
90
 
41
- | Option | Meaning |
42
- | ---------------------------------- | ------------------------------------------------------------------------- |
43
- | `-i, --input <file>` | OpenAPI 3.x document: a file path or an http(s) URL (required) |
44
- | `-o, --output <path>` | Output directory, `src/generated` by default |
45
- | `-c, --config <file>` | Configuration file, `./wow-generator.config.json` by default |
46
- | `-t, --ts-config-file-path <file>` | The project's `tsconfig.json` |
47
- | `-H, --header <header>` | `Name: value` header for an http(s) input or configuration; repeatable |
48
- | `--timeout <ms>` | Abandon an http(s) fetch after this many milliseconds, 30000 by default |
49
- | `--schema-docs <mode>` | `summary` (default) or `full`, which also embeds each model's JSON schema |
50
- | `--strict` | Exit with code 4 when the run logged a warning |
51
- | `--verbose` | Log every step with timestamps, and the stack trace of a failure |
52
- | `--quiet` | Log only warnings and errors |
91
+ | Option | Meaning |
92
+ | ---------------------------------- | --------------------------------------------------------------------------- |
93
+ | `-i, --input <file>` | Wow Manifest or OpenAPI 3.x document: a file path or http(s) URL (required) |
94
+ | `-o, --output <path>` | Output directory, `src/generated` by default |
95
+ | `-c, --config <file>` | Configuration file, `./wow-generator.config.json` by default |
96
+ | `-t, --ts-config-file-path <file>` | The project's `tsconfig.json` |
97
+ | `-H, --header <header>` | `Name: value` header for an http(s) input or configuration; repeatable |
98
+ | `--timeout <ms>` | Abandon an http(s) fetch after this many milliseconds, 30000 by default |
99
+ | `--schema-docs <mode>` | `summary` (default) or `full`, which also embeds each model's JSON schema |
100
+ | `--strict` | Exit with code 4 when the run logged a warning |
101
+ | `--verbose` | Log every step with timestamps, and the stack trace of a failure |
102
+ | `--quiet` | Log only warnings and errors |
53
103
 
54
104
  An http(s) input fails on a response outside 2xx or when the timeout expires.
55
105
  Swagger 2.0 documents are refused; convert them to OpenAPI 3 first.
@@ -68,6 +118,8 @@ A failure prints one line naming what failed; `--verbose` adds the cause.
68
118
 
69
119
  ## Configuration
70
120
 
121
+ The configuration applies to the API clients of an OpenAPI document:
122
+
71
123
  ```json
72
124
  {
73
125
  "apiClients": {
@@ -81,10 +133,7 @@ A failure prints one line naming what failed; `--verbose` adds the cause.
81
133
 
82
134
  The default config path is `./wow-generator.config.json`, resolved against
83
135
  the working directory the CLI runs in. Only that default path is optional: when
84
- no file is there the CLI generates with defaults. A
85
- `./fetcher-generator.config.json`, the name the file had before, is still read
86
- when the new name is absent, with a deprecation warning; rename it, as v10 no
87
- longer reads it. A path passed to
136
+ no file is there the CLI generates with defaults. A path passed to
88
137
  `--config` has to exist, and a configuration that cannot be read, cannot be
89
138
  parsed, or declares an option with the wrong shape fails the run rather than
90
139
  degrading to defaults. Options the generator does not read - a misspelled
@@ -103,35 +152,27 @@ check which directory the CLI ran in.
103
152
 
104
153
  The generator records the files it wrote in `.wow-generator.json` at the root
105
154
  of the output directory, so a later run removes the files it no longer
106
- generates. Commit it with the output. A `.fetcher-generator.json` left by an
107
- older version is read once and replaced.
155
+ generates. Commit it with the output.
108
156
 
109
- `ignorePathParameters` defaults to `['tenantId', 'ownerId']` only in a Wow
110
- document (one with `x-wow-context-alias` or aggregate routes); other documents
111
- keep those path parameters. `methodNames` names the method of an operationId.
157
+ `ignorePathParameters` names the path parameters a tag's client leaves out,
158
+ because an interceptor fills them; none by default. `methodNames` names the
159
+ method of an operationId.
112
160
 
113
161
  ## Generated code
114
162
 
115
163
  - Every file starts with `// Code generated by wow-generator. DO NOT EDIT.`.
116
164
  Relative imports end in `.js`, so the output compiles under `NodeNext` as
117
165
  well as `bundler`, and it is the same wherever it is written.
118
- - A method is named by `methodNames`, else `x-fetcher-method`, else the last
119
- segment of the operationId camel-cased (`example.cart.add_cart_item` →
120
- `addCartItem`). Adding an operation never renames a method; two operations
121
- of one client with the same name fail with exit code 4.
122
- - Command clients, and API clients of a document with `x-wow-context-alias`,
123
- merge the constructor's `apiMetadata` over their defaults:
124
- `new CartCommandClient({ fetcher })` keeps the bounded-context base path.
125
- Pass `basePath: ''` (query factories: `contextAlias: ''`) to reach a service
126
- directly.
127
- - Query client factories type their fields as `` `${CartAggregatedFields}` ``;
128
- annotate a query as ``ListQuery<`${CartAggregatedFields}`>``.
166
+ - An API method is named by `methodNames`, else `x-fetcher-method`, else the
167
+ last segment of the operationId camel-cased (`catalog.get_item` →
168
+ `getItem`). Adding an operation never renames a method; two operations of
169
+ one client with the same name fail with exit code 4.
129
170
  - `--schema-docs full` embeds each model's JSON schema in its doc comment;
130
171
  the default is a summary.
131
172
 
132
173
  ## Property optionality
133
174
 
134
- Every property a schema declares is generated as required. A statically typed
175
+ Every property an OpenAPI schema declares is generated as required. A statically typed
135
176
  service has no absent `int` or `boolean` to hand back, so a model full of `?`
136
177
  describes its exporter rather than its wire format - exporters routinely drop
137
178
  properties that carry a default value from `required`, and the resulting nulls
@@ -141,11 +182,6 @@ Optionality that the document genuinely means is carried where it belongs:
141
182
 
142
183
  - **Null** stays in the type. A nullable property generates `T | null`, so a
143
184
  value that may be absent is still impossible to forget.
144
- - **Commands** keep their declared optionality at the command type. A command
145
- client wraps its body in `PartialBy<Command, 'field' | ...>` built from the
146
- document's `required`, so a caller may still omit what the API says is
147
- optional - `AddCartItemCommand = CommandBody<PartialBy<AddCartItem,
148
- 'quantity'>>`. Requiring the model never narrows what a client may send.
149
185
  - **API client bodies** are wrapped the same way: a JSON request body is
150
186
  `PartialBy<Model, 'field' | ...>` for the properties its schema leaves out of
151
187
  `required`.
@@ -157,10 +193,11 @@ constructs fine.
157
193
 
158
194
  ## Core capabilities
159
195
 
160
- - Local JSON/YAML and HTTP(S) OpenAPI input.
161
- - TypeScript models and decorator API clients grouped by tag, with typed path,
162
- query and header parameters and request bodies, required ones first.
163
- - Wow bounded-context, command, snapshot, event, and query discovery.
196
+ - Wow Manifest input: the typed command, query and state clients of every
197
+ aggregate on `@ahoo-wang/wow-client`.
198
+ - Local JSON/YAML and HTTP(S) OpenAPI input: TypeScript models and decorator
199
+ API clients grouped by tag, with typed path, query and header parameters and
200
+ request bodies, required ones first.
164
201
  - Recursive `index.ts` generation and ts-morph formatting.
165
202
  - Programmatic `CodeGenerator` API with injectable logging, returning the files
166
203
  written and the warning count.
@@ -169,10 +206,10 @@ Regenerate after every contract change and compile the result before publishing.
169
206
 
170
207
  ## Documentation
171
208
 
172
- - [Quick start: generate a client from a Wow service and call it](https://wow.ahoo.me/guide/typescript/quick-start)
173
- - [Regenerate in CI](https://wow.ahoo.me/guide/typescript/regenerate-in-ci)
174
- - [Generate a client from any OpenAPI document](https://wow.ahoo.me/guide/typescript/generated-client)
209
+ - [Generate a client from a Wow service](https://wow.ahoo.me/typescript/generate)
210
+ - [Regenerate in CI](https://wow.ahoo.me/typescript/generate#keep-the-clients-in-step)
211
+ - [Generate a client from any OpenAPI document](https://wow.ahoo.me/typescript/openapi-client)
175
212
  - [wow-generator reference](https://wow.ahoo.me/reference/typescript/wow-generator/)
176
- - [Compatibility and versions](https://wow.ahoo.me/guide/typescript/compatibility)
213
+ - [Compatibility and versions](https://wow.ahoo.me/typescript/compatibility)
177
214
 
178
215
  [中文](./README.zh-CN.md) · [License](https://github.com/Ahoo-Wang/Wow/blob/main/LICENSE)
package/README.zh-CN.md CHANGED
@@ -1,13 +1,62 @@
1
1
  # `@ahoo-wang/wow-generator`
2
2
 
3
- 从本地或远程 OpenAPI 文档生成 TypeScript 模型、Fetcher Decorator 客户端与 Wow
4
- 客户端。
3
+ 从 Wow 服务的 Manifest 生成带类型的 TypeScript 客户端;也可从任意 OpenAPI 3 文档生成
4
+ TypeScript 模型与 Fetcher Decorator 客户端。
5
5
 
6
- ## 安装与运行
6
+ ## 从 Wow Manifest 生成
7
+
8
+ Wow 服务在 Manifest(`GET /wow/manifest`,或服务提交的 `manifest/baseline.json`)里描述它的
9
+ 限界上下文、聚合、命令、事件与状态。生成器据此为每个聚合生成基于 `@ahoo-wang/wow-client` 的客户端:
10
+
11
+ ```bash
12
+ pnpm add @ahoo-wang/fetcher @ahoo-wang/fetcher-eventstream @ahoo-wang/wow-client
13
+ pnpm add -D @ahoo-wang/wow-generator typescript
14
+ curl -s http://localhost:8080/wow/manifest > manifest.json
15
+ pnpm exec wow-generator generate \
16
+ --input ./manifest.json \
17
+ --output ./src/generated \
18
+ --ts-config-file-path ./tsconfig.json
19
+ ```
20
+
21
+ 每个限界上下文写出 `types.ts`(Manifest 中 schema 对应的命令、事件与状态类型)和
22
+ `boundedContext.ts`(上下文名、错误码与等待节点);每个聚合写出:
23
+
24
+ - `aggregate.ts`:聚合的描述、状态字段与事件联合类型;
25
+ - `commandClient.ts`:`<Aggregate>CommandClient`,每条命令一个方法,另有按等待的每个节点逐个返回结果的 `…Stream` 方法;
26
+ - `queryClient.ts`:`<Aggregate>SnapshotQueryClient`、`<Aggregate>EventQueryClient` 与 `<Aggregate>StateClient`。
27
+
28
+ <!-- typecheck-generated: example/eshop/web/src/generated/basket -->
29
+
30
+ ```ts
31
+ import { Fetcher } from '@ahoo-wang/fetcher';
32
+ import { filter } from '@ahoo-wang/wow-client/dsl';
33
+ import {
34
+ BasketCommandClient,
35
+ BasketSnapshotQueryClient,
36
+ } from './generated/index.js';
37
+
38
+ const fetcher = new Fetcher({ baseURL: 'http://localhost:8080' });
39
+ const basket = new BasketCommandClient({ fetcher });
40
+ const baskets = new BasketSnapshotQueryClient({ fetcher });
41
+
42
+ await basket.addBasketItem(
43
+ { productId: 'p-1', quantity: 2, unitPrice: '10.00' },
44
+ { id: 'buyer-1' },
45
+ );
46
+ const page = await baskets.paged({ filter: filter.matchAll() });
47
+ console.log(page.total);
48
+ ```
49
+
50
+ `--strict` 让有警告的运行失败;在 CI 里从服务提交的 Manifest 重新生成客户端时正需要它。
51
+
52
+ ## 从 OpenAPI 文档生成
53
+
54
+ 任意 OpenAPI 3 文档——服务自己的路由或第三方接口——为每个组件 schema 生成一个模型,为每个
55
+ tag 生成一个 Fetcher Decorator 客户端:
7
56
 
8
57
  ```bash
9
58
  pnpm add @ahoo-wang/fetcher @ahoo-wang/fetcher-decorator \
10
- @ahoo-wang/fetcher-eventstream @ahoo-wang/wow-client
59
+ @ahoo-wang/fetcher-eventstream
11
60
  pnpm add -D @ahoo-wang/wow-generator typescript
12
61
  pnpm exec wow-generator generate \
13
62
  --input ./openapi.yaml \
@@ -15,16 +64,13 @@ pnpm exec wow-generator generate \
15
64
  --ts-config-file-path ./tsconfig.json
16
65
  ```
17
66
 
18
- 版本号跟随 Wow,次版本可能带有破坏性改动:用 `save-prefix=~` 或 `--save-exact` 让 Wow 包停在同一个次版本上,见[版本范围](https://wow.ahoo.me/zh/guide/typescript/compatibility#版本范围)。
67
+ 生成的 API 客户端是装饰器类,编译它们的项目需要在 `tsconfig.json` 中设置
68
+ `"experimentalDecorators": true`。文档只按 OpenAPI 读取:schema 键、tag 与路径参数就是它们在文档里的
69
+ 含义,不叠加 Wow 的约定。
19
70
 
20
- 第一行安装生成代码运行时导入的包,第二行安装生成器和 TypeScript。生成器的 peer(`fetcher`、
21
- `fetcher-decorator`、`fetcher-eventstream`、`wow-client`)就是第一行的运行时包;`wow-client`
22
- 须与生成器的次版本一致。需要 Node 22.12 或更高版本,以及 TypeScript 6 或更高版本:CI 测试
23
- 6.0 到最新的 7.x。命令原名
24
- `fetcher-generator`,这个名字作为别名保留到 v10,运行前会向 stderr 打印一行弃用提示。
71
+ ## 版本
25
72
 
26
- 生成的客户端是装饰器类,编译它们的项目需要在 `tsconfig.json` 中设置
27
- `"experimentalDecorators": true`。随 Wow 9.2.0 发布,与 Wow 同一个 tag、同一个版本号。
73
+ 版本号跟随 Wow,次版本可能带有破坏性改动:用 `save-prefix=~` 或 `--save-exact` 让 Wow 包停在同一个次版本上,见[版本范围](https://wow.ahoo.me/zh/typescript/compatibility#版本范围)。`wow-client` 须与生成器的次版本一致。需要 Node 22.12 或更高版本,以及 TypeScript 6 或更高版本:CI 测试 6.0 到最新的 7.x。
28
74
 
29
75
  生成成功时先输出警告(如有),最后输出一行摘要:
30
76
 
@@ -34,18 +80,18 @@ Generated 12 files into src/generated with /work/app/wow-generator.config.json,
34
80
 
35
81
  ## 选项与退出码
36
82
 
37
- | 选项 | 含义 |
38
- | ---------------------------------- | ------------------------------------------------------------ |
39
- | `-i, --input <file>` | OpenAPI 3.x 文档:文件路径或 http(s) URL(必填) |
40
- | `-o, --output <path>` | 输出目录,默认 `src/generated` |
41
- | `-c, --config <file>` | 配置文件,默认 `./wow-generator.config.json` |
42
- | `-t, --ts-config-file-path <file>` | 项目的 `tsconfig.json` |
43
- | `-H, --header <header>` | 读取 http(s) 输入或配置时发送的 `Name: value` 请求头,可重复 |
44
- | `--timeout <ms>` | http(s) 请求超过这个毫秒数即放弃,默认 30000 |
45
- | `--schema-docs <mode>` | `summary`(默认)或 `full`,后者还嵌入每个模型的 JSON schema |
46
- | `--strict` | 本次运行有警告时以退出码 4 结束 |
47
- | `--verbose` | 输出每一步(带时间戳),失败时输出堆栈 |
48
- | `--quiet` | 只输出警告和错误 |
83
+ | 选项 | 含义 |
84
+ | ---------------------------------- | ---------------------------------------------------------------- |
85
+ | `-i, --input <file>` | Wow Manifest 或 OpenAPI 3.x 文档:文件路径或 http(s) URL(必填) |
86
+ | `-o, --output <path>` | 输出目录,默认 `src/generated` |
87
+ | `-c, --config <file>` | 配置文件,默认 `./wow-generator.config.json` |
88
+ | `-t, --ts-config-file-path <file>` | 项目的 `tsconfig.json` |
89
+ | `-H, --header <header>` | 读取 http(s) 输入或配置时发送的 `Name: value` 请求头,可重复 |
90
+ | `--timeout <ms>` | http(s) 请求超过这个毫秒数即放弃,默认 30000 |
91
+ | `--schema-docs <mode>` | `summary`(默认)或 `full`,后者还嵌入每个模型的 JSON schema |
92
+ | `--strict` | 本次运行有警告时以退出码 4 结束 |
93
+ | `--verbose` | 输出每一步(带时间戳),失败时输出堆栈 |
94
+ | `--quiet` | 只输出警告和错误 |
49
95
 
50
96
  http(s) 输入的响应不是 2xx 或超时都会失败。Swagger 2.0 文档会被拒绝,请先转换为 OpenAPI 3。
51
97
 
@@ -63,6 +109,8 @@ http(s) 输入的响应不是 2xx 或超时都会失败。Swagger 2.0 文档会
63
109
 
64
110
  ## 配置
65
111
 
112
+ 配置作用于 OpenAPI 文档的 API 客户端:
113
+
66
114
  ```json
67
115
  {
68
116
  "apiClients": {
@@ -75,8 +123,7 @@ http(s) 输入的响应不是 2xx 或超时都会失败。Swagger 2.0 文档会
75
123
  ```
76
124
 
77
125
  默认配置路径为 `./wow-generator.config.json`,相对 CLI 的运行目录解析。只有这个默认路径是
78
- 可选的:该位置没有文件时,CLI 按默认值生成。新文件名不存在时,仍会读取旧名
79
- `./fetcher-generator.config.json` 并给出弃用警告;请改名,v10 起不再读取旧名。`--config` 指定的路径必须
126
+ 可选的:该位置没有文件时,CLI 按默认值生成。`--config` 指定的路径必须
80
127
  存在;配置读不到、解析不了,或某个选项的形态不对,都会让本次生成失败,而不是退回默认值。
81
128
  生成器不认识的选项——例如拼错的 `apiClient`——会被逐个点名警告并忽略。
82
129
 
@@ -90,38 +137,29 @@ http(s) 输入的响应不是 2xx 或超时都会失败。Swagger 2.0 文档会
90
137
  如果摘要里没有配置路径,说明配置文件根本没送到生成器——请检查 CLI 的运行目录。
91
138
 
92
139
  生成器把写出的文件记录在输出目录根下的 `.wow-generator.json` 中,之后的运行据此删除不再生成的
93
- 文件。请把它和输出一起提交。旧版本留下的 `.fetcher-generator.json` 会被读取一次并替换。
140
+ 文件。请把它和输出一起提交。
94
141
 
95
- `ignorePathParameters` 只在 Wow 文档(带 `x-wow-context-alias` 或聚合路由)中默认为
96
- `['tenantId', 'ownerId']`;其他文档保留这两个路径参数。`methodNames` 为 operationId 指定方法名。
142
+ `ignorePathParameters` 指定某个 tag 的客户端省略的路径参数(由拦截器填充),默认不省略。
143
+ `methodNames` 为 operationId 指定方法名。
97
144
 
98
145
  ## 生成的代码
99
146
 
100
147
  - 每个文件以 `// Code generated by wow-generator. DO NOT EDIT.` 开头。相对导入以 `.js` 结尾,
101
148
  所以输出在 `NodeNext` 和 `bundler` 下都能编译,且写到哪里都一样。
102
- - 方法名依次取 `methodNames`、`x-fetcher-method`,否则取 operationId 最后一段的 camelCase
103
- (`example.cart.add_cart_item` → `addCartItem`)。新增操作不会让已有方法改名;同一客户端的两个
149
+ - API 方法名依次取 `methodNames`、`x-fetcher-method`,否则取 operationId 最后一段的 camelCase
150
+ (`catalog.get_item` → `getItem`)。新增操作不会让已有方法改名;同一客户端的两个
104
151
  操作同名时以退出码 4 失败。
105
- - 命令客户端,以及文档带 `x-wow-context-alias` 时的 API 客户端,把构造器的 `apiMetadata` 合并到
106
- 默认值之上:`new CartCommandClient({ fetcher })` 保留限界上下文的基础路径。直接访问服务时传
107
- `basePath: ''`(查询客户端工厂传 `contextAlias: ''`)。
108
- - 查询客户端工厂的字段类型为 `` `${CartAggregatedFields}` ``;标注查询时写
109
- ``ListQuery<`${CartAggregatedFields}`>``。
110
152
  - `--schema-docs full` 会在每个模型的文档注释中嵌入其 JSON schema;默认只含摘要。
111
153
 
112
154
  ## 属性可选性
113
155
 
114
- schema 声明的每个属性都生成为必填。强类型后端根本没有"缺席的 `int`/`boolean`"可以返回,
156
+ OpenAPI schema 声明的每个属性都生成为必填。强类型后端根本没有"缺席的 `int`/`boolean`"可以返回,
115
157
  满屏 `?` 的模型描述的是导出器而不是线上格式——导出器常把带默认值的属性排除在 `required`
116
158
  之外,由此产生的空值判断纯属噪音。
117
159
 
118
160
  文档真正想表达的可选性,则由各自合适的位置承载:
119
161
 
120
162
  - **可空**留在类型里。可空属性生成 `T | null`,可能缺席的值依然不可能被忘记。
121
- - **命令**在命令类型上保留声明的可选性。命令客户端会依据文档的 `required` 把请求体包成
122
- `PartialBy<Command, 'field' | ...>`,调用方仍可省略 API 允许省略的字段——
123
- `AddCartItemCommand = CommandBody<PartialBy<AddCartItem, 'quantity'>>`。把模型改成必填
124
- 绝不会收紧客户端能发的内容。
125
163
  - **API 客户端请求体**也同样处理:JSON 请求体生成为 `PartialBy<Model, 'field' | ...>`,覆盖 schema
126
164
  未列入 `required` 的属性。
127
165
 
@@ -130,9 +168,8 @@ schema 声明的每个属性都生成为必填。强类型后端根本没有"缺
130
168
 
131
169
  ## 核心能力
132
170
 
133
- - 本地 JSON/YAML 与 HTTP(S) OpenAPI 输入。
134
- - 按 tag 分组的 TypeScript 模型与 Decorator API 客户端,path、query、header 参数和请求体都带类型,必填的在前。
135
- - Wow bounded-context、命令、快照、事件与查询发现。
171
+ - Wow Manifest 输入:每个聚合基于 `@ahoo-wang/wow-client` 的命令、查询与状态客户端。
172
+ - 本地 JSON/YAML 与 HTTP(S) OpenAPI 输入:按 tag 分组的 TypeScript 模型与 Decorator API 客户端,path、query、header 参数和请求体都带类型,必填的在前。
136
173
  - 递归生成 `index.ts`,并通过 ts-morph 格式化。
137
174
  - 支持注入日志的编程式 `CodeGenerator` API,返回写出的文件与警告数。
138
175
 
@@ -140,10 +177,10 @@ schema 声明的每个属性都生成为必填。强类型后端根本没有"缺
140
177
 
141
178
  ## 文档
142
179
 
143
- - [快速开始:从 Wow 服务生成客户端并调用](https://wow.ahoo.me/zh/guide/typescript/quick-start)
144
- - [在 CI 中重新生成](https://wow.ahoo.me/zh/guide/typescript/regenerate-in-ci)
145
- - [从任意 OpenAPI 文档生成客户端](https://wow.ahoo.me/zh/guide/typescript/generated-client)
180
+ - [从 Wow 服务生成客户端](https://wow.ahoo.me/zh/typescript/generate)
181
+ - [在 CI 中重新生成](https://wow.ahoo.me/zh/typescript/generate)
182
+ - [从任意 OpenAPI 文档生成客户端](https://wow.ahoo.me/zh/typescript/openapi-client)
146
183
  - [wow-generator 参考](https://wow.ahoo.me/zh/reference/typescript/wow-generator/)
147
- - [兼容性与版本](https://wow.ahoo.me/zh/guide/typescript/compatibility)
184
+ - [兼容性与版本](https://wow.ahoo.me/zh/typescript/compatibility)
148
185
 
149
186
  [English](./README.md) · [许可证](https://github.com/Ahoo-Wang/Wow/blob/main/LICENSE)
@@ -1,18 +1,15 @@
1
1
  import { GeneratorConfiguration } from '../api/configuration.cjs';
2
2
  import { OpenApiDocument } from '../openapi/document.cjs';
3
- import { WowModel } from '../wow/model.cjs';
4
3
  import { Analysis } from './model.cjs';
5
4
  /**
6
- * Decides what a document generates: its bounded contexts, models, command
7
- * and query clients and API clients. A pure function of the document, its
8
- * Wow model and the configuration; nothing is written and nothing logged.
5
+ * Decides what an OpenAPI document generates: its models and API clients. A
6
+ * pure function of the document and the configuration; nothing is written
7
+ * and nothing logged.
9
8
  *
10
9
  * @param document - The document, left unchanged
11
- * @param wow - Its Wow model
12
10
  * @param config - The generator configuration
13
11
  * @returns The generation model, and the warnings for what it leaves out
14
12
  * @throws GeneratorError when the document asks for code that cannot
15
- * compile: two schemas of one model, a command type named like a model,
16
- * two operations of one method
13
+ * compile: two schemas of one model, two operations of one method
17
14
  */
18
- export declare function analyze(document: OpenApiDocument, wow: WowModel, config: GeneratorConfiguration): Analysis;
15
+ export declare function analyze(document: OpenApiDocument, config: GeneratorConfiguration): Analysis;
@@ -1,18 +1,15 @@
1
1
  import { GeneratorConfiguration } from '../api/configuration.js';
2
2
  import { OpenApiDocument } from '../openapi/document.js';
3
- import { WowModel } from '../wow/model.js';
4
3
  import { Analysis } from './model.js';
5
4
  /**
6
- * Decides what a document generates: its bounded contexts, models, command
7
- * and query clients and API clients. A pure function of the document, its
8
- * Wow model and the configuration; nothing is written and nothing logged.
5
+ * Decides what an OpenAPI document generates: its models and API clients. A
6
+ * pure function of the document and the configuration; nothing is written
7
+ * and nothing logged.
9
8
  *
10
9
  * @param document - The document, left unchanged
11
- * @param wow - Its Wow model
12
10
  * @param config - The generator configuration
13
11
  * @returns The generation model, and the warnings for what it leaves out
14
12
  * @throws GeneratorError when the document asks for code that cannot
15
- * compile: two schemas of one model, a command type named like a model,
16
- * two operations of one method
13
+ * compile: two schemas of one model, two operations of one method
17
14
  */
18
- export declare function analyze(document: OpenApiDocument, wow: WowModel, config: GeneratorConfiguration): Analysis;
15
+ export declare function analyze(document: OpenApiDocument, config: GeneratorConfiguration): Analysis;
@@ -1,14 +1,13 @@
1
1
  import { GeneratorConfiguration } from '../api/configuration.cjs';
2
2
  import { OpenApiDocument } from '../openapi/document.cjs';
3
- import { WowModel } from '../wow/model.cjs';
4
3
  import { ApiClientModel } from './model.cjs';
5
4
  /**
6
- * The API clients of a document: one per tag that is neither Wow's own, the
7
- * actuator's, nor an aggregate's, holding a method per operation.
5
+ * The API clients of a document: one per tag, holding a method per
6
+ * operation.
8
7
  *
9
8
  * @param warnings - Receives a line for every operation left out and every
10
9
  * client renamed
11
10
  * @throws GeneratorError when two operations of a client name the same
12
11
  * method, or a configured or extension method name is not one
13
12
  */
14
- export declare function analyzeApiClients(document: OpenApiDocument, wow: WowModel, config: GeneratorConfiguration, warnings: string[]): ApiClientModel[];
13
+ export declare function analyzeApiClients(document: OpenApiDocument, config: GeneratorConfiguration, warnings: string[]): ApiClientModel[];
@@ -1,14 +1,13 @@
1
1
  import { GeneratorConfiguration } from '../api/configuration.js';
2
2
  import { OpenApiDocument } from '../openapi/document.js';
3
- import { WowModel } from '../wow/model.js';
4
3
  import { ApiClientModel } from './model.js';
5
4
  /**
6
- * The API clients of a document: one per tag that is neither Wow's own, the
7
- * actuator's, nor an aggregate's, holding a method per operation.
5
+ * The API clients of a document: one per tag, holding a method per
6
+ * operation.
8
7
  *
9
8
  * @param warnings - Receives a line for every operation left out and every
10
9
  * client renamed
11
10
  * @throws GeneratorError when two operations of a client name the same
12
11
  * method, or a configured or extension method name is not one
13
12
  */
14
- export declare function analyzeApiClients(document: OpenApiDocument, wow: WowModel, config: GeneratorConfiguration, warnings: string[]): ApiClientModel[];
13
+ export declare function analyzeApiClients(document: OpenApiDocument, config: GeneratorConfiguration, warnings: string[]): ApiClientModel[];
@@ -1,24 +1,4 @@
1
- import { TagAliasAggregate } from '../wow/model.cjs';
2
1
  import { Operation } from '@ahoo-wang/fetcher-openapi';
3
- /**
4
- * The path of a client module of an aggregate, relative to the output
5
- * directory: `<contextAlias>/<aggregateName>/<fileName>.ts`.
6
- *
7
- * @param aggregate - The aggregate metadata containing context alias and aggregate name
8
- * @param fileName - The name of the file, without extension
9
- *
10
- * @example
11
- * ```typescript
12
- * clientModulePath({ contextAlias: 'user', aggregateName: 'profile' }, 'queryClient');
13
- * // 'user/profile/queryClient.ts'
14
- * ```
15
- */
16
- export declare function clientModulePath(aggregate: Pick<TagAliasAggregate, 'contextAlias' | 'aggregateName'>, fileName: string): string;
17
- /**
18
- * The name of a class or type of an aggregate: its name as a type, then the
19
- * suffix (`order`, `CommandClient` → `OrderCommandClient`).
20
- */
21
- export declare function resolveClassName(aggregate: Pick<TagAliasAggregate, 'aggregateName'>, suffix: string): string;
22
2
  /** Operation extension naming the method an operation generates. */
23
3
  export declare const OPERATION_METHOD_NAME_KEY = "x-fetcher-method";
24
4
  /**
@@ -1,24 +1,4 @@
1
- import { TagAliasAggregate } from '../wow/model.js';
2
1
  import { Operation } from '@ahoo-wang/fetcher-openapi';
3
- /**
4
- * The path of a client module of an aggregate, relative to the output
5
- * directory: `<contextAlias>/<aggregateName>/<fileName>.ts`.
6
- *
7
- * @param aggregate - The aggregate metadata containing context alias and aggregate name
8
- * @param fileName - The name of the file, without extension
9
- *
10
- * @example
11
- * ```typescript
12
- * clientModulePath({ contextAlias: 'user', aggregateName: 'profile' }, 'queryClient');
13
- * // 'user/profile/queryClient.ts'
14
- * ```
15
- */
16
- export declare function clientModulePath(aggregate: Pick<TagAliasAggregate, 'contextAlias' | 'aggregateName'>, fileName: string): string;
17
- /**
18
- * The name of a class or type of an aggregate: its name as a type, then the
19
- * suffix (`order`, `CommandClient` → `OrderCommandClient`).
20
- */
21
- export declare function resolveClassName(aggregate: Pick<TagAliasAggregate, 'aggregateName'>, suffix: string): string;
22
2
  /** Operation extension naming the method an operation generates. */
23
3
  export declare const OPERATION_METHOD_NAME_KEY = "x-fetcher-method";
24
4
  /**