Skip to content
Open
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
58 changes: 58 additions & 0 deletions external_plugins/api-to-sdk/.zcode-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "api-to-sdk",
"version": "0.1.0",
"description": "Turn an OpenAPI 3.0 / 3.1 spec (YAML or JSON) into a typed TypeScript SDK package: types for every schema, a dependency-free fetch client with one method per operation (path, query and header parameters, JSON / multipart / form bodies, bearer / API key / basic auth, timeouts, ApiError with the parsed error body), a README and an example that calls every operation, plus a mock server built from the spec's examples. Verified automatically: strict TypeScript compile, then every operation is called against the mock and checked against the spec.",
"description_i18n": {
"en": "Turn an OpenAPI 3.0 / 3.1 spec (YAML or JSON) into a typed TypeScript SDK package: types for every schema, a dependency-free fetch client with one method per operation (path, query and header parameters, JSON / multipart / form bodies, bearer / API key / basic auth, timeouts, ApiError with the parsed error body), a README and an example that calls every operation, plus a mock server built from the spec's examples. Verified automatically: strict TypeScript compile, then every operation is called against the mock and checked against the spec.",
"zh-CN": "把 OpenAPI 3.0 / 3.1 规范(YAML 或 JSON)生成带完整类型的 TypeScript SDK 包:每个数据结构都有类型;基于 fetch、没有依赖的客户端,每个接口一个方法(路径/查询/请求头参数,JSON / multipart / 表单请求体,Bearer / API Key / Basic 认证,超时,带解析后错误内容的 ApiError);附 README、调用全部接口的示例,以及用规范示例搭建的模拟服务器。自动验证:TypeScript 严格模式编译,再逐个调用每个接口并按规范检查。"
},
"author": {
"name": "Penova",
"url": "https://github.com/ppppp2005-p"
},
"license": "MIT",
"keywords": [
"openapi",
"swagger",
"sdk",
"typescript",
"api client",
"codegen",
"mock server",
"fetch",
"接口",
"SDK",
"代码生成",
"模拟服务器"
],
"skills": "skills",
"mcpServers": {
"api-to-sdk": {
"type": "stdio",
"command": "node",
"args": [
"${ZCODE_PLUGIN_ROOT}/dist/server.mjs"
],
"cwd": "${ZCODE_PROJECT_DIR}",
"timeoutMs": 900000,
"env": {
"ZCODE_WORKSPACE_ROOT": "${ZCODE_PROJECT_DIR}"
}
}
},
"ui": {
"surfaces": [
{
"id": "editor",
"title": {
"zh-CN": "接口转 SDK",
"en-US": "API To SDK"
},
"icon": "code",
"server": "api-to-sdk",
"resourceUri": "ui://api-to-sdk/panel.html",
"availability": "session"
}
]
}
}
21 changes: 21 additions & 0 deletions external_plugins/api-to-sdk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Penova

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions external_plugins/api-to-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# API To SDK

Turn an **OpenAPI 3.0 / 3.1** spec (YAML or JSON) into a **typed TypeScript SDK package**, then prove it works: it is compiled with TypeScript in strict mode, and every operation is called against a mock server built from the spec.

## What it does

- **Reads the spec**: local `$ref`s (schemas, parameters, request bodies, responses), path-level parameters, `allOf` / `oneOf` / `anyOf`, `const`, enums, 3.0 `nullable` and 3.1 type arrays, `additionalProperties`, security schemes. It warns about external or broken `$ref`s, undeclared path parameters, missing or duplicate operationIds and OAuth flows.
- **Generates** `sdk/<api>/`:
- `src/types.ts`: one type per schema, with descriptions as JSDoc.
- `src/client.ts`: a client class with one method per operation (`api.getBike({ bikeId: 7 })`). It has typed arguments and responses, and also provides:
- Path, query (arrays repeated) and header parameters; JSON, multipart (`FormData`, `Blob`), form and raw bodies.
- Bearer token (string or function), API key (header, query or cookie) and basic auth, chosen per operation from `security`.
- `timeoutMs`, `AbortSignal`, custom `fetch` and per-request headers.
- `ApiError` with status and parsed body; typed error bodies are exported as `<Op>ErrorBody`.
- No runtime dependencies (works in Node 20+, Deno, Bun and browsers). Pre-built `dist/` (JS + `.d.ts`), `package.json` and `tsconfig.json`.
- `README.md` with an operation table, and `examples/all-operations.ts`, which calls every operation with the spec's example values (spec examples that don't match their schema are reported).
- `mock/server.mjs`: a dependency-free mock answering from the spec's examples (or values synthesised from schemas). It checks requests against the spec (400 with reasons), supports `Prefer: code=404` to return documented errors, and allows CORS.
- **Verifies**:
- Strict TypeScript compile.
- Calling every operation against the mock. Each call checks method, path parameters, query, headers, credentials and JSON body against what was passed; that the response was parsed; and that a documented error status throws `ApiError` with the parsed body.

## Usage

Ask the agent in ZCode Desktop:

- "Generate a TypeScript SDK from openapi.yaml"
- "Make a client for this API spec and test it"
- "Re-verify the SDK after I changed the spec"

Or use the panel: choose the spec, **Read spec**, optionally set the folder, package name and class name, then **Generate & verify**.

## Tools

| Tool | Purpose |
|---|---|
| `sdk_inspect` | List operations, schemas, auth and spec problems (writes nothing) |
| `sdk_generate` | Write the SDK package, compile it and call every operation against the mock |
| `sdk_test` | Re-run the compile and mock calls (for example after editing generated code) |

## Scope (v0.1)

- TypeScript only; publishing to npm is up to you (`npm publish` in the folder).
- OpenAPI 2 (Swagger) must be converted to 3 first. External `$ref` files are not followed.
- OAuth2 / OpenID flows are not generated; pass an access token with `token`. Cookie parameters, `deepObject` query style, XML bodies and callbacks/webhooks are not supported.
- The mock is for development and tests, not a full contract tester. Its checks cover types, required fields, enums, `const`, formats, ranges and `oneOf` / `anyOf` / `allOf`.

## Privacy, network and files

- Runs locally with no network access and no API keys. Verification talks only to a mock on 127.0.0.1; the real API is never called.
- Reads the spec. Writes the SDK folder and `.api-to-sdk/sdks.json`.

## License

MIT. Bundles [yaml](https://github.com/eemeli/yaml) (ISC) and [TypeScript](https://github.com/microsoft/TypeScript) (Apache-2.0) for compiling.
54 changes: 54 additions & 0 deletions external_plugins/api-to-sdk/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 接口转 SDK

把 **OpenAPI 3.0 / 3.1** 规范(YAML 或 JSON)生成**带完整类型的 TypeScript SDK 包**,并证明它能用:用 TypeScript 严格模式编译,再对照用规范搭建的模拟服务器,逐个调用每个接口。

## 功能

- **读取规范**:本地 `$ref`(数据结构、参数、请求体、响应)、路径级参数、`allOf` / `oneOf` / `anyOf`、`const`、枚举、3.0 的 `nullable` 和 3.1 的类型数组、`additionalProperties`、认证方案。会提示外部或失效的 `$ref`、未声明的路径参数、缺失或重复的 operationId,以及 OAuth 流程。
- **生成** `sdk/<api>/`:
- `src/types.ts`:每个数据结构一个类型,说明文字转成 JSDoc。
- `src/client.ts`:客户端类,每个接口一个方法(如 `api.getBike({ bikeId: 7 })`),参数和返回值都有类型。还支持:
- 路径、查询(数组会重复参数)和请求头参数;JSON、multipart(`FormData`、`Blob`)、表单和原始请求体。
- Bearer 令牌(字符串或函数)、API Key(请求头、查询参数或 Cookie)和 Basic 认证,按每个接口的 `security` 自动选择。
- `timeoutMs`、`AbortSignal`、自定义 `fetch`、单次请求的请求头。
- 失败时抛出 `ApiError`,带状态码和解析后的错误内容;错误内容的类型导出为 `<接口>ErrorBody`。
- 没有运行时依赖,可用于 Node 20+、Deno、Bun 和浏览器。已预先构建好 `dist/`(JS + `.d.ts`),附 `package.json` 和 `tsconfig.json`。
- `README.md`(含接口表)和 `examples/all-operations.ts`(用规范里的示例值调用每个接口;示例与数据结构不符时会提示)。
- `mock/server.mjs`:没有依赖的模拟服务器。按规范里的示例(或根据数据结构生成的值)返回响应;对照规范检查请求,不符时返回 400 并说明原因;支持用 `Prefer: code=404` 返回文档里写的错误;允许跨域。
- **验证**:
- TypeScript 严格模式编译。
- 对模拟服务器逐个调用接口。检查方法、路径参数、查询参数、请求头、认证信息和 JSON 请求体是否和传入的一致,响应是否正确解析,文档里写的错误状态码是否抛出带解析内容的 `ApiError`。

## 使用

在 ZCode Desktop 里对助手说:

- “根据 openapi.yaml 生成 TypeScript SDK”
- “给这个接口文档生成客户端并测试”
- “规范改了,重新验证 SDK”

也可以用面板:选择规范文件,点 **读取接口**,按需填写输出目录、包名和类名,再点 **生成 SDK 并验证**。

## 工具

| 工具 | 作用 |
|---|---|
| `sdk_inspect` | 列出接口、数据结构、认证和规范问题(不写文件) |
| `sdk_generate` | 生成 SDK 包、编译,并对模拟服务器调用每个接口 |
| `sdk_test` | 重新编译并调用(例如手动改过生成的代码之后) |

## 范围(v0.1)

- 只生成 TypeScript;发布到 npm 由你自己完成(在目录里运行 `npm publish`)。
- OpenAPI 2(Swagger)需要先转换成 3。不跟随外部 `$ref` 文件。
- 不生成 OAuth2 / OpenID 登录流程,请用 `token` 传入访问令牌。不支持 Cookie 参数、`deepObject` 查询参数、XML 请求体和回调/Webhook。
- 模拟服务器用于开发和测试,不是完整的契约测试工具。它检查类型、必填、枚举、`const`、格式、范围和 `oneOf` / `anyOf` / `allOf`。

## 隐私、网络与文件

- 全部在本地运行,不联网,不需要 API Key。验证时只访问 127.0.0.1 上的模拟服务器,不会调用真实接口。
- 读取规范文件,写入 SDK 目录和 `.api-to-sdk/sdks.json`。

## 许可证

MIT。内置 [yaml](https://github.com/eemeli/yaml)(ISC)和用于编译的 [TypeScript](https://github.com/microsoft/TypeScript)(Apache-2.0)。
13 changes: 13 additions & 0 deletions external_plugins/api-to-sdk/skills/api-to-sdk/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: api-to-sdk
description: 把 OpenAPI 3 规范(YAML/JSON)生成带类型的 TypeScript SDK 包(fetch 客户端、类型、错误处理、认证、README、示例、模拟服务器),并自动验证:严格模式编译 + 对模拟服务器调用每个接口。用户提到“OpenAPI / Swagger / 接口文档生成 SDK / 生成客户端 / API client”时使用。
---

# 接口转 SDK

1. 找到规范文件(openapi.yaml / openapi.json)。如果是 Swagger 2.0,先告诉用户需要转换成 OpenAPI 3(可以用 swagger2openapi)。
2. 先用 `sdk_inspect({spec})` 查看:接口数量、认证方式、警告(外部 $ref、缺少 operationId 等)。把重要的警告告诉用户。
3. 用 `sdk_generate({spec, out?, packageName?, className?})` 生成 SDK。它会自动编译,并对模拟服务器调用每个接口。
4. 报告结果:编译是否通过、通过了几个接口;有失败的,说明是哪个接口、什么问题(通常是规范自身前后矛盾,例如示例与数据结构不符)。
5. 告诉用户怎么用:`import { <Class> } from "<包名>"`(或 `npm install ./sdk/<api>`),`new <Class>({ token })`;开发时可以用 `node sdk/<api>/mock/server.mjs` 启动模拟服务器。
6. 规范或生成的代码改了以后,用 `sdk_test({out})` 重新验证。
50 changes: 50 additions & 0 deletions marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,56 @@
"把上一个 Hi3D 任务生成的模型按角色拆分成部件"
]
}
},
{
"name": "api-to-sdk",
"componentTypes": [
"ui"
],
"source": "./external_plugins/api-to-sdk",
"displayName": "API To SDK",
"displayName_i18n": {
"en": "API To SDK",
"zh-CN": "接口转 SDK"
},
"description": "Turn an OpenAPI 3.0 / 3.1 spec (YAML or JSON) into a typed TypeScript SDK package: types for every schema, a dependency-free fetch client with one method per operation (path, query and header parameters, JSON / multipart / form bodies, bearer / API key / basic auth, timeouts, ApiError with the parsed error body), a README and an example that calls every operation, plus a mock server built from the spec's examples. Verified automatically: strict TypeScript compile, then every operation is called against the mock and checked against the spec.",
"description_i18n": {
"en": "Turn an OpenAPI 3.0 / 3.1 spec (YAML or JSON) into a typed TypeScript SDK package: types for every schema, a dependency-free fetch client with one method per operation (path, query and header parameters, JSON / multipart / form bodies, bearer / API key / basic auth, timeouts, ApiError with the parsed error body), a README and an example that calls every operation, plus a mock server built from the spec's examples. Verified automatically: strict TypeScript compile, then every operation is called against the mock and checked against the spec.",
"zh-CN": "把 OpenAPI 3.0 / 3.1 规范(YAML 或 JSON)生成带完整类型的 TypeScript SDK 包:每个数据结构都有类型;基于 fetch、没有依赖的客户端,每个接口一个方法(路径/查询/请求头参数,JSON / multipart / 表单请求体,Bearer / API Key / Basic 认证,超时,带解析后错误内容的 ApiError);附 README、调用全部接口的示例,以及用规范示例搭建的模拟服务器。自动验证:TypeScript 严格模式编译,再逐个调用每个接口并按规范检查。"
},
"version": "0.1.0",
"author": {
"name": "Penova",
"url": "https://github.com/ppppp2005-p"
},
"license": "MIT",
"category": "developer-tools",
"keywords": [
"openapi",
"swagger",
"sdk",
"typescript",
"api client",
"codegen",
"mock server",
"fetch",
"接口",
"SDK",
"代码生成",
"模拟服务器"
],
"examplePrompts": [
"Generate a TypeScript SDK from openapi.yaml",
"Make a client for this API spec and test it",
"Re-verify the SDK after I changed the spec"
],
"examplePrompts_i18n": {
"zh-CN": [
"根据 openapi.yaml 生成 TypeScript SDK",
"给这个接口文档生成客户端并测试",
"规范改了,重新验证 SDK"
]
}
}
]
}
Loading