@archastro/sdk-generator

中文文档
0.11.17发布于 2026/11/04MITTypeScriptES Module构建来源已验证近 30 天下载 7.9万

Generate typed TypeScript, Python, Swift, Go, Elixir, and Rust SDKs (plus contract tests) from an OpenAPI spec.

原始使用文档
文档目录8 节
  1. Install
  2. CLI
  3. Go target configuration
  4. Generated SDK documentation
  5. --config
  6. TypeScript client extensions
  7. Programmatic use
  8. Development

@archastro/sdk-generator

Generate typed TypeScript, Python, Swift, Go, Elixir, and Rust SDKs — plus cross-language contract tests — from an OpenAPI spec produced by the ArchAstro API DSL.

Install

# ad-hoc
npx @archastro/sdk-generator --spec ./openapi.json --lang typescript --out ./sdk

# global
npm install -g @archastro/sdk-generator
sdk-generator --spec ./openapi.json --lang python --out ./sdk

CLI

sdk-generator --spec <openapi.json> \
                  [--lang typescript|python|swift|go|elixir|rust|contract-tests-ts|contract-tests-py|contract-tests-swift|contract-tests-go|contract-tests-elixir|contract-tests-rust] \
                  [--out <dir>] \
                  [--config <config.json>] \
                  [--ast-only]

Targets:

--langEmits
typescriptTS SDK: resources, channel classes, auth, client, zod schemas
pythonPython SDK: Pydantic models, resources, channels
swiftSwift SDK: Codable models, resources, channels, async client
goGo SDK: JSON-tagged structs, context-taking resources, channels, client
elixirElixir SDK: structs, resource modules, auth, and Phoenix Channel facades
rustRust SDK: serde models, async/blocking resources, typed SSE, auth, and channel facades
contract-tests-tsTS contract tests that drive @archastro/channel-harness
contract-tests-pyPython contract tests (pytest + Prism mock server)
contract-tests-swiftSwift contract tests (swift-testing + Prism + harness)
contract-tests-goGo contract tests (go test + Prism + harness)
contract-tests-elixirElixir contract tests (ExUnit + Prism + harness)
contract-tests-rustRust integration contracts (cargo test + Prism + harness)

Go target configuration

Go resolves types and functions from one package-level namespace, and it compiles one package per directory, so the Go backend needs the package name and the import path callers reach it by. Both live in the config file under go:

{
  "go": {
    "packageName": "platform",
    "importPath": "github.com/ArchAstro/archastro-go/platform"
  }
}

The SDK lands flat in <out>/<packageName>/ (types_*.go, v1_*.go, channels_*.go, client.go, auth.go) and the contract tests in <out>/contracttests/, which imports the SDK by that path. The emitter writes structurally correct Go, not column-aligned Go — run gofmt -w over the output as part of regeneration.

Generated SDK documentation

The generator preserves OpenAPI documentation as idiomatic source docs in each language. Use standard OpenAPI fields as the source of truth:

  • Operation summary and description become TypeScript JSDoc, Python method docstrings, and Swift/Go // documentation comments.
  • Parameter and request-body field description values become parameter docs and input object field docs.
  • Success response descriptions become return-value docs.
  • Schema description values become TypeScript interface/schema docs and Python model docstrings.
  • Schema property description values become TypeScript field docs, Zod .describe(...) metadata, and Pydantic Field(description=...) metadata.

For consuming SDK repos, keep docs generation next to the generated package:

  • TypeScript: add TypeDoc, point entryPoints at the public generated entry points, and publish the generated docs/ directory with GitHub Pages.
  • Python: add pdoc, point it at the generated package, and publish the rendered HTML with GitHub Pages.
  • Run the docs build in CI after SDK regeneration so broken doc comments fail before release.

--config

Shared codegen metadata consumed by both backends:

{
  "name": "@archastro/platform-sdk",
  "version": "0.1.0",
  "baseUrl": "https://platform.archastro.ai",
  "apiBase": "/api",
  "defaultVersion": "v1",
  "description": "ArchAstro Platform API SDK"
}

TypeScript client extensions

Generated PlatformClient classes expose a typed class-expression mixin seam:

const RealtimePlatformClient = PlatformClient.extend(
  withCustomObjectSubscriptions,
);
const client = RealtimePlatformClient.withToken("pk_live_...", accessToken);

extend returns a subclass rather than modifying PlatformClient. Extensions therefore retain generated resources, can be chained, and are preserved by the generated static factories. A hand-maintained extension uses the standard TypeScript mixin shape:

const withDiagnostics = <TBase extends PlatformClientConstructor>(Base: TBase) =>
  class extends Base {
    diagnosticLabel() {
      return "ready";
    }
  };

Consuming SDKs can keep those modules public across regeneration without teaching the generator their methods:

{
  "typescript": {
    "clientExtensionModules": ["./custom-object-subscriptions.js"]
  }
}

Each configured module is emitted as an export * entry in the generated package barrel. The module itself remains hand-maintained.

Programmatic use

import {
  parseOpenApiSpec,
  generateTypeScript,
  generatePython,
  generateContractTests,
} from "@archastro/sdk-generator";

const spec = JSON.parse(readFileSync("openapi.json", "utf-8"));
const ast = parseOpenApiSpec(spec, { name: "@archastro/platform-sdk" });
const files = generateTypeScript(ast, { outDir: "./sdk" });

Development

This package lives inside the archastro-openapi workspace. See the root README for build / test / release instructions.

版本与发布记录

@archastro/sdk-generator 共发布 42 个版本,首次发布于 2026/04/18;过去 12 个月发布了 42 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.11.17latest2026/11/044 天前
0.11.162026/11/016 天前
0.11.152026/11/016 天前
0.11.142026/10/2216 天前
0.11.132026/10/2216 天前
0.11.122026/10/2216 天前

关于 @archastro/sdk-generator 的常见问题

@archastro/sdk-generator 是什么?

Generate typed TypeScript, Python, Swift, Go, Elixir, and Rust SDKs (plus contract tests) from an OpenAPI spec.

如何安装 @archastro/sdk-generator?

在项目目录运行 npm install @archastro/sdk-generator,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 sdk-generator,可以用 npx sdk-generator 直接运行。

@archastro/sdk-generator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@archastro/sdk-generator 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@archastro/sdk-generator 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@archastro/sdk-generator 可以商用吗?

@archastro/sdk-generator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@archastro/sdk-generator 的下载量有多少?

根据 npm 官方下载统计,@archastro/sdk-generator 在 2026/10/07 – 2026/11/05 这 30 天内被下载 78,984 次,日均约 2,633 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@archastro/sdk-generator 还在维护吗?

最新版本 0.11.17 发布于 2026/11/04(4 天前);至今共发布 42 个版本,其中过去 12 个月发布了 42 个;首次发布于 2026/04/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。