生成客户端
wow-generator 读取服务的 Manifest,为每个聚合写出一个类型化客户端:每条命令一个方法,以及它的快照和事件查询、状态读取、错误码和等待的各跳。应用只通过这些客户端调用服务,所以服务改变了某个路由或字段,表现为编译错误,而不是一次失败的请求。
Manifest 从哪里来
服务在 GET /wow/manifest 上提供它的 Manifest(在公开端口上,只有设置 wow.http.manifest.enabled: true 时才提供),它的构建也把同一份文档提交为 manifest/baseline.json,由 check 保证它与服务提供的一致(Manifest 与兼容)。从基线生成:不需要运行中的服务,它是经过评审的版本,它的改变就是客户端的改变。
pnpm add @ahoo-wang/fetcher @ahoo-wang/fetcher-eventstream @ahoo-wang/wow-client
pnpm add -D @ahoo-wang/wow-generator typescript
pnpm exec wow-generator generate \
--input ../ordering-server/manifest/baseline.json \
--output src/generated/ordering \
--strict--input 也接受 http(s) URL,并用 -H 'Name: value' 带上需要的请求头。--strict 让记录了警告的运行失败。一次运行读取一份 Manifest;面向多个服务的前端,为每个服务运行一次生成器,各自输出到一个目录。eShop 的 Web 界面为它的五个服务这样做:
// Generates the clients of the eShop's five services from their Manifest
// baselines (`examples/eshop/<context>/<context>-server/manifest/baseline.json`,
// which each server's `check` holds to what it serves) into
// src/generated/<context>/, one wow-generator run per service:
//
// pnpm --filter @ahoo-wang/wow-generator... build
// pnpm --filter wow-eshop-web generate
//
// The output is committed byte for byte. With `--check` (`node
// scripts/generate.mjs --check`), wow-generator writes nothing and fails,
// listing the files, when src/generated/<context>/ differs from what it would
// write; src/generated.test.ts runs it for each service.
import { execFileSync } from 'node:child_process';
import { rmSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
export const CONTEXTS = ['catalog', 'basket', 'ordering', 'inventory', 'payment'];
const WEB = fileURLToPath(new URL('..', import.meta.url));
export const ESHOP = join(WEB, '..');
export const CLI = join(WEB, '../../../typescript/wow-generator/dist/cli.js');
/** The Manifest baseline of the service of `context`. */
export function baselineOf(context) {
return join(ESHOP, context, `${context}-server`, 'manifest', 'baseline.json');
}
/** Writes the clients of `context` into `output`, or with `--check` compares them. */
export function generate(context, output, ...options) {
execFileSync(
process.execPath,
[CLI, 'generate', '-i', baselineOf(context), '-o', output, '--strict', '--quiet', ...options],
{ stdio: 'inherit' },
);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const check = process.argv.includes('--check');
for (const context of CONTEXTS) {
const output = join(WEB, 'src', 'generated', context);
if (check) {
generate(context, output, '--check');
} else {
rmSync(output, { recursive: true, force: true });
generate(context, output);
}
}
}所有选项和退出码见 CLI 参考。
它写出什么
每个限界上下文一个以它命名的目录:
| 文件 | 包含 |
|---|---|
contract.ts | <Context>Contract:上下文的名字、上下文级的错误码和它的各个聚合(各带自己的错误码);<Context>ErrorCode 联合类型,以及在其中等待的各跳 |
manifest.ts | <Context>Manifest:上下文列出的每条命令的路由(按名字与曾用名,包括系统命令与作业的命令,内部命令除外)与它的服务路由,以及命令体指明了聚合的命令里承载 ID 的属性(idProperty),与 Manifest 列出的一致,不含 schema。命令客户端按它发送,因此不必获取 Manifest |
client.ts | <Context>Client:上下文的 CommandClient(commands),它像聚合的命令客户端一样把每条命令的精确数字不加引号写出,以及每个聚合的命令、快照、事件和状态客户端;有服务路由时还有它们(routes) |
routes.ts | <Context>Routes:上下文每个服务路由一个方法,基于 @ahoo-wang/wow-client/routes 的 ServiceRoutes,返回路由的应答类型;Manifest 标为 responseOptional 的路由(null 以 204 No Content 应答)再加上 undefined,没有应答的路由返回 void;只在 Manifest 列出服务路由时写出 |
types.ts | Manifest 的 schema 中的命令、事件和状态,各以其类的简单名命名。两个类共用的名字,或生成的模块已经占用的名字(它们用到的 TypeScript 全局类型,如 Promise、Record;它们从 @ahoo-wang/wow-client 导入的名字,如 CommandClient;生成器自己声明的名字,如 <Aggregate>Event),会以类的包名限定:shop.Record 生成为 ShopRecord。参数化类型在使用处原地写出,但引用自身的参数化类型(子节点也是 Node<String> 的 Node<String>)只声明一次,名字是类名、Of 与各类型参数的简单名,多个参数以 And 连接,嵌套的参数在其位置展开:a.Node(java.lang.String) 是 NodeOfString,java.util.Map(java.lang.String,java.util.List(a.Item)) 是 MapOfStringAndListOfItem;重名时以类的包名限定 |
<aggregate>/aggregate.ts | 聚合的描述符、查询能指明的 state.* 字段,以及它的事件的联合类型;以 @Alias 改名的事件也以每个旧名字列出,即尚未升级的服务端回答它的名字 |
<aggregate>/commandClient.ts | <Aggregate>CommandClient:每条命令一个方法,每个方法有一个 …Stream 孪生方法,以及聚合列出的生命周期命令。内部命令("internal": true,只在内部受理的命令)没有方法,manifest.ts 里也没有它的条目,只有它用到的类型也不生成:公开端口不提供它 |
<aggregate>/queryClient.ts | <Aggregate>SnapshotQueryClient、<Aggregate>EventQueryClient 和 <Aggregate>StateClient |
index.ts | 以上全部,再导出一次;在根目录,两个上下文都导出的名字(两者都有的聚合名)不进入根索引,也不产生警告:从它所在上下文的 index.ts 导入 |
.wow-generator.json | 这次运行写出的文件及其哈希,以便之后的运行删除它不再写出的文件,以及因此变空的目录 |
错误码的常量按 Kotlin 契约的命名:取聚合(context.aggregate.code)或上下文之后的部分,以点分隔的每一段写成大写蛇形,用 __ 连接,以数字开头时前加 _。invalid-item 是 INVALID_ITEM,invalidURLValue 是 INVALID_URL_VALUE,上下文 analytics-catalog 的 analytics.catalog.incomplete 是 ANALYTICS__CATALOG__INCOMPLETE。常量相同的两个错误码会被拒绝,并点名两者(见下文)。名字与生成器在旁边声明的成员相同时,它得到自己的名字:方法或 …Stream 孪生方法已被占用的命令(Start 旁的 StartStream、Commands、Then,或生命周期命令的名字)加 Command,再占用就加 Command2(所以新增一条命令可能让原有的方法改名:单有 StartStream 时它是 startStream,加上 Start 之后 startStream 成了 Start 的孪生方法,StartStream 的方法变成 startStreamCommand;tsc 会指出每一处移动了的调用);名为 commands 或 routes 的聚合在 <Context>Client 中是 commandsAggregate,名为 errors 或 wait 的聚合在契约中是 ErrorsAggregate 或 WaitAggregate,与 Kotlin 契约的对象同名;服务路由的方法与命令的方法同样命名(export-orders 是 exportOrders),名为 routes 的服务路由是 routesRoute。生成的类都不取成员 then:有它的对象会成为 thenable,await 会把它当作 promise。
生成器按 Wow 编译器的规则核对 Manifest 里的名字,两者对同一个名字不会有不同的看法:聚合、命令或事件的名字是一个字母后接字母、数字、_ 和 -([A-Za-z][A-Za-z0-9_-]*),或者是框架自己的名字(wow.lifecycle.DeleteAggregate),命令只有作为系统命令时才能使用这类名字,与 Manifest 合并器的规则一致;订阅的名字是处理器的名字与处理函数的名字以点相连;错误码是以点连接的若干段,每段是一个字母或数字后接字母、数字、_ 和 -。编译器写出的 Manifest 总是合乎这些规则。由别的东西写出、不合规则的 Manifest 会作为文档的问题被拒绝,并点名那一项和规则:什么都不生成,不为取不出标识符的名字猜一个,像 a/b 这样的名字也不会被当作路径。同一上下文里名字得出同一个生成名的两个聚合,order-item 与 order_item(都是 OrderItem),或 errors 与 errors-aggregate(都是契约的 ErrorsAggregate),同样被拒绝,并点名两者:Kotlin 契约拒绝同样的聚合对。名字得出某个 Errors 中同一个常量的两个错误码,invalid-item 与 invalid_item(都是 INVALID_ITEM),以同样的方式被拒绝;Manifest 里列了两次的同一个错误码只是一个常量。同一聚合的两个事件以名字或旧名字(@Alias)认领同一个名字时被拒绝,并写明这个名字和两个事件,与 Wow 编译器的 Manifest 合并器拒绝它们一样:以这个名字到来的记录可能是其中任何一个。聚合的模块放在以它命名的目录里,位于其上下文的目录之下,所以同一上下文里名字只有大小写不同的两个聚合(orderItem 与 orderitem),或名字只有大小写不同的两个上下文,也被拒绝:不区分大小写的文件系统(macOS 和 Windows 默认如此)会把两者写进同一个目录。
eShop ordering 的契约。它的错误码只是领域的拒绝:违反约束的命令回答框架的 wow.command.invalid(FrameworkErrorCodes.COMMAND_INVALID)及其 violations(发送命令)。
// Code generated by wow-generator. DO NOT EDIT.
import { Hop } from '@ahoo-wang/wow-client';
import { OrderAggregate } from './order/aggregate.js';
/**
* The bounded context `eshop-ordering`: its name, its error codes, its wait hops and its aggregates, as the Kotlin `EshopOrderingContract` names them. A problem's `code` is one of its `Errors`, or of an aggregate's.
* The eShop ordering: orders from placement to shipment
*/
export const EshopOrderingContract = {
NAME: 'eshop-ordering',
/** The hops of a wait chain (`Wow-Wait`) in `eshop-ordering`, as the Kotlin contract's `Wait` holds them. `WaitPlan.of(...)` chains them, those of other contexts too. */
Wait: {
/** Until a command of `eshop-ordering` passed admission. */
accepted: (): Hop => Hop.accepted(EshopOrderingContract.NAME),
/** Until a command's events are appended to the log of `eshop-ordering`. */
committed: (): Hop => Hop.committed(EshopOrderingContract.NAME),
/** Until the snapshot of `eshop-ordering` holds the commit. */
snapshot: (): Hop => Hop.snapshot(EshopOrderingContract.NAME),
/** Until the store the snapshot queries of `eshop-ordering` read holds the commit. */
queryable: (): Hop => Hop.queryable(EshopOrderingContract.NAME),
/** Until a subscription of `eshop-ordering` handled the commit. */
handled: (subscription: EshopOrderingSubscription): Hop => Hop.handled(subscription, EshopOrderingContract.NAME),
},
Order: OrderAggregate,
} as const;
/** An error code of `eshop-ordering`, of the context or of one of its aggregates. */
export type EshopOrderingErrorCode = 'eshop-ordering.order.wrong-amount' | 'eshop-ordering.order.wrong-status';
/** The subscriptions of `eshop-ordering` a `handled` hop names. */
export type EshopOrderingSubscription = 'OrderProcessSaga.onOrderAwaitingValidation' | 'OrderProcessSaga.onOrderSubmitted' | 'OrderProcessSaga.onReserveCommitted' | 'OrderSummaryProjection.onEvent' | 'ShipmentNotifier.onOrderShipped';每个文件都以 // Code generated by wow-generator. DO NOT EDIT. 开头,下一次运行会覆盖它。把输出连同 .wow-generator.json 一起提交。生成器写出 \n 行尾,它的哈希和 --check 把 \r\n 当作 \n 读,所以转换行尾的检出(Git 的 core.autocrlf)持有的是同样的文件;要保持原样,在 .gitattributes 中加上 src/generated/** text eol=lf。
输出是普通的 TypeScript,不带装饰器,它的相对导入以 .js 结尾,所以在 "moduleResolution": "bundler" 和 "nodenext" 下都能编译。打包工具项目保留它自己的设置;运行编译后输出、并在 package.json 中带 "type": "module" 的 Node 项目,可以使用这个 tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src/**/*.ts"]
}CI 正是用这个 tsconfig.json 编译本页的代码。
精确数字
Kotlin 的 Long、UInt、BigInteger、ULong、BigDecimal 或 Number 字段(schema 格式为 int64、bigint 或 decimal),在生成的类型中是 string,按服务写出的样子精确地保存数字;JavaScript 的 number 会对它们取整。线上仍然传输 JSON 数字:聚合的描述符列出这些字段,客户端从响应的文本中读取它们,并在命令体中不加引号地写回去;查询的过滤条件也一样,根上的过滤条件以及聚合查询中元素和指标的过滤条件,每个字段都按服务读取它的位置读取(在元素之下时相对于该元素):这类字段上的比较操作数(value、values 中的每一个、BETWEEN 的两个边界,经由 and、or、nor 与 elementMatch)是这样的文本时,按它表示的数字写出,所以从记录读到的键按服务读取该字段的方式比较。不是 JSON 数字的操作数仍是字符串。在事件查询中,事件体路径(events.body.<path>,或 element('events') 之下的 body.<path>)只要有一个声明它的事件标记了它,就是精确的,与服务在每个声明该路径的事件中读取它一致:另一个事件把它作为 double 保存时,它的值也按文本读取;另一个事件把它作为字符串保存时,表示数字的操作数按数字写出。
这在任何深度、Manifest 描述了其值的每条路径上都成立:状态、事件(以旧名字 @Alias 到来时也是如此)、命令体、查询的记录,以及服务路由的请求体和响应(它没有描述的两类值见下文)。递归类型(children 仍是树的树)只在上下文的 textNumbers.ts 中标记一次,值到达哪里就在哪里读取。联合类型的各个分支保留各自的数字:一个值是 decimal 或由它们组成的对象时,按它实际是哪一种读取;由某个取常量值的成员区分的对象(Jackson 的 @JsonTypeInfo 写出子类型的方式)各自按自己的标记读取。如果分支对同一个数字的读法不同(一个精确、一个按数字),又没有这样的成员可以区分,生成就会失败(退出码 4),并指明 schema、路径和两种格式:给这些分支加上判别字段,或者使用同一种数字格式。
// Code generated by wow-generator. DO NOT EDIT.
import type { CommittedEvent, NoAccess, SystemEvent } from '@ahoo-wang/wow-client';
import type { OrderAwaitingValidation, OrderCancelled, OrderPaid, OrderShipped, OrderStockConfirmed, OrderStockItemConfirmed, OrderSubmitted } from '../types.js';
/**
* The aggregate `eshop-ordering.order`.
* A buyer's order, from its submission to its shipment
*/
export const OrderAggregate = {
context: 'eshop-ordering',
name: 'order',
ownership: 'owner',
idSource: 'assigned',
snapshot: true,
textNumbers: {
state: { items: { '*': { unitPrice: true } }, totalAmount: true },
events: {
'eshop-ordering.order.OrderPaid': { amount: true },
'eshop-ordering.order.OrderSubmitted': { items: { '*': { unitPrice: true } } },
},
commands: {
'eshop-ordering.order.PayOrder': { amount: true },
'eshop-ordering.order.SubmitOrder': { items: { '*': { unitPrice: true } } },
},
},
Errors: {
/** 422: A payment pays the order's total amount. */
WRONG_AMOUNT: 'eshop-ordering.order.wrong-amount',
/** 409: The order's status does not allow the command. */
WRONG_STATUS: 'eshop-ordering.order.wrong-status',
},
} as const;
/** The `state.*` paths of `order`'s snapshots a query filters, sorts and projects on. */
export type OrderStateField = 'state.cancelReason' | 'state.confirmedProducts' | 'state.items' | 'state.items.productId' | 'state.items.quantity' | 'state.items.unitPrice' | 'state.paymentId' | 'state.status' | 'state.totalAmount';
/** The access attributes of `eshop-ordering.order`: it declares none. */
export type OrderAccess = NoAccess;
/** The events of `order`, named as the server sends them, `eshop-ordering.order.<Event>` at its current revision (the `events` query upgrades older records), and the system events every aggregate has. */
export type OrderEvent = CommittedEvent<'eshop-ordering.order.OrderAwaitingValidation', OrderAwaitingValidation> | CommittedEvent<'eshop-ordering.order.OrderCancelled', OrderCancelled> | CommittedEvent<'eshop-ordering.order.OrderPaid', OrderPaid> | CommittedEvent<'eshop-ordering.order.OrderShipped', OrderShipped> | CommittedEvent<'eshop-ordering.order.OrderStockConfirmed', OrderStockConfirmed> | CommittedEvent<'eshop-ordering.order.OrderStockItemConfirmed', OrderStockItemConfirmed> | CommittedEvent<'eshop-ordering.order.OrderSubmitted', OrderSubmitted> | SystemEvent;@ahoo-wang/wow-client 的 parseJson 和 stringifyJson 为你自己的请求做同样的事;手写的客户端接收同样的标记(TextNumbers):ServiceRoutes 通过 textNumbers: { 'context.route': { request, response } },数据集的行通过 DatasetClient.dataset(context, dataset, textNumbers)。标记有这些形式:
| 形式 | 标记的是 | 示例 |
|---|---|---|
true | 这个值是精确数字 | { price: true } |
成员、'*' | 对象按名字的成员;'*' 是数组的每一项和映射的每个值 | { lines: { '*': { amount: true } } } |
() => marks | 只在值到达那里时才读取的标记:递归类型的标记 | const tree: TextNumbers = { amount: true, children: { '*': () => tree } } |
textNumbersOr(marks) | 精确数字,或以 marks 标记的对象或数组,取决于值是哪一种 | decimal | { amount: decimal } 用 textNumbersOr({ amount: true }) |
textNumbersBy(member, cases) | 按对象成员 member 的字符串值选择该对象的标记 | textNumbersBy('kind', { decimal: { amount: true } }) |
有两类值读取时不带标记,其中的大数字仍会被取整(聚合查询中以带标记的字段分组的 terms 键,以及对这类字段取 any、first 或 last 的指标值,即该字段的一个值,与该字段一样按文本读取,在元素之下也是如此,所以两个分组不会读成同一个键):
- 命令结果的
results是服务自己的值,Manifest 没有给出它们的 schema:命令在其中返回的Long或BigDecimal会以 JavaScriptnumber到达。请把这样的值以字符串返回,或者从聚合的状态中读取。 - 聚合查询中计算得出的指标值:对精确字段求和或取最大值,结果超出
number能表示的范围时会被取整。
Kotlin 的 Double 或 Float 是 number。持有非数或无穷的值以字符串 "NaN"、"Infinity" 或 "-Infinity" 到达,与 Wow 的 JSON 写出的一样(JSON 值),尽管它的类型写着 number:可能持有这类值的字段要检查 typeof value === 'string'。为一个状态很少持有的值放宽每个 double,会让每个调用方都多一层负担。
让客户端保持同步
基线改变时就重新生成,并让 CI 在提交的客户端与生成器现在写出的不同时失败:
pnpm exec wow-generator generate --input ../ordering-server/manifest/baseline.json --output src/generated/ordering --strict
git diff --exit-code -- src/generated然后编译:命令体改变了、字段被删除了或聚合改了名,都会让使用它的代码在 tsc 中失败。eShop 在一个测试 src/generated.test.ts 中让它的客户端与基线保持一致,这个测试运行 generate --check:它什么也不写,已提交的客户端与它要写的不同时失败,并列出这些文件。
让生成器与生成代码所编译依赖的 wow-client 保持同一个版本,也就是它所调用的服务的版本。
基于任何 OpenAPI 文档
生成器也能从不是来自 Wow 的 OpenAPI 3 文档(例如某个第三方 API 的)写出 TypeScript 模型和 Fetcher 装饰器客户端;做法见从任意 OpenAPI 文档生成客户端。Wow 服务自己的 OpenAPI 文档(OpenAPI 与 springdoc)供其他工具使用:它的 TypeScript 客户端要从 Manifest 生成。