Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
按版本倒序列出升级动作与可见行为变化。使用说明见 `guides/`,开发检查和发布操作分别见
[贡献指南](CONTRIBUTING.md)与[发布流程](RELEASING.md)。

## v3.3.0 — OpenAPI 3.0.3 与合约一致性
## v3.3.1 — SDK 位置参数兼容性与 3.3 系列首次发布

- 恢复已声明 params schema 的 SDK 位置参数顺序,避免字段声明顺序与路由顺序不同时,把既有调用的参数值对调。
- 保留已声明字段的全部原有形参位置;未声明的路径参数追加在后,仍与 body/query 共用避撞规则。
- 用完整生成的 ESM SDK 和真实 HTTP 请求覆盖逆序声明、部分声明和额外可选字段。
- 五个公开包统一发布为 3.3.1。3.3.0 未单独打 tag 或发布,其全部新增能力包含在本版本中。

## v3.3.0 — OpenAPI 3.0.3 与合约一致性(随 v3.3.1 首次发布)

新增独立的 OpenAPI 3 输出,现有 `docs.swagger` 继续生成 Swagger 2,无需修改运行时路由。

Expand Down
4 changes: 3 additions & 1 deletion guides/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ await sdk.putItemsId('42', { name: 'new name' }, { trace: 'request-1' });
```

上例中的方法分别对应 `/users`、`/items/:id` 路由,需先注册对应 schema 再生成 SDK。
命名规则为 HTTP 方法加路径驼峰形式,路径参数按路由中的顺序传入。
命名规则为 HTTP 方法加路径驼峰形式。路径参数沿用 params schema 的字段声明顺序;
未声明的路径参数随后按路由顺序补入。没有 params schema 时,全部按路由顺序传入。
为兼容已有 SDK 的位置参数,已声明但未出现在路径中的字段仍保留原有形参位置。
未声明或部分声明 params schema 时,缺少的路径参数按字符串补入 OpenAPI;只有路由中实际存在的参数
才会列为 path parameter,并始终标记 required。仍建议声明完整 params schema 以获得运行时校验。
SDK 的路径、body、query 形参共用避撞规则;同名字段使用内部别名,调用时的对象键保持原样。
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "erest",
"version": "3.3.0",
"version": "3.3.1",
"description": "Framework-agnostic REST API framework with Zod validation, auto docs & test scaffolding. Express/Koa/@leizm/web adapters as separate packages.",
"type": "module",
"main": "dist/lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/erest-express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@erest/express",
"version": "3.3.0",
"version": "3.3.1",
"repository": {
"type": "git",
"url": "git+https://github.com/yourtion/node-erest.git",
Expand Down
2 changes: 1 addition & 1 deletion packages/erest-gen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@erest/gen",
"version": "3.3.0",
"version": "3.3.1",
"repository": {
"type": "git",
"url": "git+https://github.com/yourtion/node-erest.git",
Expand Down
2 changes: 1 addition & 1 deletion packages/erest-koa/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@erest/koa",
"version": "3.3.0",
"version": "3.3.1",
"repository": {
"type": "git",
"url": "git+https://github.com/yourtion/node-erest.git",
Expand Down
2 changes: 1 addition & 1 deletion packages/erest-leizmweb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@erest/leizmweb",
"version": "3.3.0",
"version": "3.3.1",
"repository": {
"type": "git",
"url": "git+https://github.com/yourtion/node-erest.git",
Expand Down
4 changes: 3 additions & 1 deletion src/lib/plugin/generate_axios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as utils from "../../utils.js";
import { resolveZodShape } from "../zod-meta.js";

export default function generateAxios(data: IDocData, dir: string, options: IDocOptions, writter: IDocWritter) {

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

unicorn(consistent-function-scoping)

Function `uniqueBinding` does not capture any variables from its parent scope

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

unicorn(consistent-function-scoping)

Function `usesQueryInput` does not capture any variables from its parent scope

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

unicorn(consistent-function-scoping)

Function `schemaFieldNames` does not capture any variables from its parent scope

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

unicorn(consistent-function-scoping)

Function `rmPathParam` does not capture any variables from its parent scope

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

unicorn(consistent-function-scoping)

Function `slashToCamel` does not capture any variables from its parent scope

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

unicorn(consistent-function-scoping)

Function `rmPathParam` does not capture any variables from its parent scope

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

unicorn(consistent-function-scoping)

Function `slashToCamel` does not capture any variables from its parent scope

Check warning on line 9 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

unicorn(consistent-function-scoping)

Function `slashToCamel` does not capture any variables from its parent scope
debug("generateAxios: %s - %o", dir, options);

function slashToCamel(name: string) {
Expand All @@ -15,7 +15,7 @@
});
}

function rmPathParam(path: string) {

Check warning on line 18 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

eslint(no-shadow)

'path' is already declared in the upper scope.

Check warning on line 18 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

eslint(no-shadow)

'path' is already declared in the upper scope.

Check warning on line 18 in src/lib/plugin/generate_axios/index.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

eslint(no-shadow)

'path' is already declared in the upper scope.
return utils.mapPathParams(path, (name) => name);
}

Expand Down Expand Up @@ -49,7 +49,9 @@

function pathBindings(req: APIOption<unknown>) {
const used = new Set<string>();
return new Map(utils.getPathParamNames(req.realPath).map((key) => [key, uniqueBinding(key, used)]));
// 保持已生成 SDK 的位置参数契约:已声明字段顺序不变,遗漏的路径参数追加在后。
const keys = new Set([...schemaFieldNames(req.paramsSchema), ...utils.getPathParamNames(req.realPath)]);
return new Map([...keys].map((key) => [key, uniqueBinding(key, used)]));
}

function inputBindings(req: APIOption<unknown>) {
Expand Down
25 changes: 25 additions & 0 deletions src/test/test-sdk-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ import { z } from "zod";
import lib from "./lib";

const cases = [
{
method: "get",
route: "/:first/:second",
paramsSchema: z.object({ second: z.string(), first: z.string() }),
args: ["second", "first"],
url: "/first/second",
body: "",
},
{
method: "get",
route: "/:first/:second",
paramsSchema: z.object({ second: z.string() }),
args: ["second", "first"],
url: "/first/second",
body: "",
},
{
method: "get",
route: "/:id",
paramsSchema: z.object({ unused: z.string().optional(), id: z.string() }),
args: ["unused", "path"],
url: "/path",
body: "",
},
...["post", "put", "patch"].map((method) => ({
method,
route: "/:query",
Expand Down Expand Up @@ -72,6 +96,7 @@ describe("生成 SDK 的形参绑定与避撞", () => {
.registerTyped(
{
...("query" in entry ? { query: entry.query } : {}),
...("paramsSchema" in entry ? { params: entry.paramsSchema } : {}),
...("bodySchema" in entry ? { body: entry.bodySchema } : {}),
},
() => {}
Expand Down
Loading