@docspack/openapi

中文文档
0.2.1发布于 2026/10/03MITTypeScriptES Module近 30 天下载 2.4万

What docspack does with an OpenAPI document: a dense per-operation digest for an agent, code samples and a request builder for a page, and a try-it console.

原始使用文档
文档目录4 节
  1. Why a digest rather than the document
  2. LAPIS, and where the digest differs from it
  3. The try-it console
  4. Licence

@docspack/openapi

What docspack does with an OpenAPI document.

Reading the document and rendering it as LAPIS is @docspack/lapis, which is publishable on its own because that conversion is useful to people who will never touch docspack. This package is everything built on top of it — and it re-exports the model, so you need one import rather than two:

  • for an agent — a dense per-operation digest: the base URL, the credential, the inputs with their types, the body shape, the response, the failures and a runnable curl call, in LAPIS notation;
  • for a person — a request builder and code samples in cURL, JavaScript, Python and Go, plus the behaviour for a try-it console.

Both come from the same parse and the same request builder, so a sample on a page, the request the console sends and the example an agent is answered with cannot disagree.

Its only dependency is @docspack/lapis, whose main entry has none of its own, so this runs in the CLI and in a browser alike.

import { parseOpenApi, operationDigest, codeSamples, toGroups } from "@docspack/openapi";

// `parseOpenApi` and `toGroups` come from @docspack/lapis, re-exported here.
const document = parseOpenApi(JSON.parse(await readFile("openapi.json", "utf8")));
const [operation] = document.operations;

operationDigest(document, operation); // the dense text an agent gets
codeSamples(document, operation); // cURL, JavaScript, Python, Go
toGroups(document); // operations grouped by tag, for a sidebar

Why a digest rather than the document

An agent handed an OpenAPI document is handed every operation in the API to answer a question about one. Measured against Stripe's published spec — 594 operations, 1,454 schemas, 8.0 MB of JSON:

estimated tokens
The whole document, as published2,007,014
The whole document, as LAPIS193,593
One operation's digest (median)830

Against the smallest correct slice of OpenAPI JSON for a single operation — the operation, the schemas it references, its security scheme and its server — a digest is about 72% smaller (tests/digest.test.ts holds that number to a floor).

Three limits make that possible, and each of them exists because its absence was measured:

  • Named types are carried by name. The full transitive closure from one Stripe operation reaches most of the API, because every object has an expandable field pointing at another: digests came out at 66,500 tokens each. The walk is breadth-first from the operation's own schemas and stops at a token budget, naming what did not fit.
  • Inline objects expand two levels and list twelve fields. A document that names nothing pays for it here.
  • Enumerations list eight members. Stripe's merchant-category enum has 297 and appears four times in one operation — 25,637 characters on one line.

Anything elided is counted (+32 more, # not expanded here: …), because "there is more to this shape" and "this is the whole shape" are different claims.

LAPIS, and where the digest differs from it

LAPIS (v0.1.0, CC BY 4.0) is an open format for describing an API to a language model. It is adopted rather than replaced by a notation of our own: a format a model has already seen is one it parses without being taught, and the saving is structural either way. The renderer, and its one documented extension, are in @docspack/lapis.

Two further deviations belong to the digest specifically, because it describes one operation rather than a whole API:

  1. Errors are listed per digest, as <status> <Type> # <description>, rather than in a document-wide [errors] section with @ops: bindings. toLapis — the whole-document rendering — does emit the centralized form, which is where LAPIS's largest single saving comes from.
  2. The success status rides along as a # comment on the < line. LAPIS's output marker carries a type and nothing else, and 201 versus 204 changes what a caller does next.

The try-it console

@docspack/openapi/console is a DOM module, not a component:

import { mountApiConsoles } from "@docspack/openapi/console";

mountApiConsoles(); // claims every [data-api-console] on the page

The markup renders statically — a labelled, filled-in form showing exactly what the endpoint takes — and this makes the Send button work. The credential is never stored: it is read from the field at send time and kept in nothing that outlives the request. No localStorage, no sessionStorage, no cookie.

@docspack/sheaf-react renders markup this module understands.

Licence

MIT. The LAPIS specification is CC BY 4.0 and belongs to its authors; this package implements a renderer for it.

版本与发布记录

@docspack/openapi 共发布 3 个版本,首次发布于 2026/09/11;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.2.1latest2026/10/031 个月前
0.2.02026/09/161 个月前
0.1.02026/09/112 个月前

关于 @docspack/openapi 的常见问题

@docspack/openapi 是什么?

What docspack does with an OpenAPI document: a dense per-operation digest for an agent, code samples and a request builder for a page, and a try-it console.

如何安装 @docspack/openapi?

在项目目录运行 npm install @docspack/openapi,或使用 pnpm add / yarn add / bun add。

@docspack/openapi 支持 TypeScript 吗?

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

@docspack/openapi 支持 ESM 还是 CommonJS?

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

@docspack/openapi 需要什么版本的 Node.js?

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

@docspack/openapi 可以商用吗?

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

@docspack/openapi 的下载量有多少?

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

@docspack/openapi 还在维护吗?

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