@api-platform/api-doc-parser

中文文档
0.16.12发布于 2026/09/29MITTypeScriptES Module构建来源已验证近 30 天下载 3.4万

Transform an API documentation (Hydra, OpenAPI, GraphQL) in an intermediate representation that can be used for various tasks such as creating smart API clients, scaffolding code or building administration interfaces.

原始使用文档
文档目录17 节
  1. ✨ Key Features
  2. 📦 Installation
  3. 🚀 Usage
  4. Type definitions
  5. Api
  6. Resource
  7. Field
  8. Parameter
  9. FieldType
  10. Operation
  11. 📖 OpenAPI Support
  12. 🧩 Support for other formats (JSON:API, AsyncAPI...)
  13. 🤝 Contributing
  14. 👥 Contributors
  15. 🌟 Star History
  16. 🙌 Credits
  17. 🔒 License

API doc parser

API Doc Parser

Effortlessly turn Hydra, Swagger/OpenAPI, and GraphQL specs into actionable data for your tools and apps.

CI GitHub License npm bundle size npm version NPM Downloads


api-doc-parser is a standalone TypeScript library that parses Hydra, Swagger, OpenAPI, and GraphQL documentation into a unified, intermediate representation.
This normalized structure enables smart API clients, code generators, admin interfaces, and more.
It integrates seamlessly with the API Platform framework.

✨ Key Features

  • Unified output – one normalized Api object covering resources, fields, operations, parameters, and relations
  • TypeScript-first – strict typings for every parsed element
  • Embedded & referenced resources resolved automatically
  • Framework integration – easily integrates with the API Platform ecosystem
  • Supports all major API formats – Hydra, Swagger/OpenAPI v2, OpenAPI v3, and GraphQL

📦 Installation

Using NPM:

npm install @api-platform/api-doc-parser

Using Pnpm:

pnpm add @api-platform/api-doc-parser

With Yarn:

yarn add @api-platform/api-doc-parser

Using Bun:

bun add @api-platform/api-doc-parser

🚀 Usage

Hydra

import { parseHydraDocumentation } from "@api-platform/api-doc-parser";

const { api, response, status } = await parseHydraDocumentation(
  "https://demo.api-platform.com",
);

OpenAPI v2 (formerly known as Swagger)

import { parseSwaggerDocumentation } from "@api-platform/api-doc-parser";

const { api, response, status } = await parseSwaggerDocumentation(
  "https://demo.api-platform.com/docs.json",
);

OpenAPI v3

import { parseOpenApi3Documentation } from "@api-platform/api-doc-parser";

const { api, response, status } = await parseOpenApi3Documentation(
  "https://demo.api-platform.com/docs.jsonopenapi?spec_version=3.0.0",
);

GraphQL

import { parseGraphQl } from "@api-platform/api-doc-parser";

const { api, response } = await parseGraphQl(
  "https://demo.api-platform.com/graphql",
);

TypeScript Type definitions

Each parse function returns a Promise that resolves to an object containing the normalized API structure, the raw documentation, and the HTTP status code:

OpenAPI 3

function parseOpenApi3Documentation(
  entrypointUrl: string,
  options?: RequestInitExtended,
): Promise<{
  api: Api;
  response: OpenAPIV3.Document;
  status: number;
}>;

Swagger

function parseSwaggerDocumentation(entrypointUrl: string): Promise<{
  api: Api;
  response: OpenAPIV2.Document;
  status: number;
}>;

Hydra

function parseHydraDocumentation(
  entrypointUrl: string,
  options?: RequestInitExtended,
): Promise<{
  api: Api;
  response: Response;
  status: number;
}>;

GraphQL

function parseGraphQl(
  entrypointUrl: string,
  options?: RequestInit,
): Promise<{
  api: Api;
  response: Response;
}>;

Api

Represents the root of the parsed API, containing the entrypoint URL, an optional title, and a list of resources.

interface Api {
  entrypoint: string;
  title?: string;
  resources?: Resource[];
}

Resource

Describes an API resource (such as an entity or collection), including its fields, operations, and metadata.

interface Resource {
  name: string | null;
  url: string | null;
  id?: string | null;
  title?: string | null;
  description?: string | null;
  deprecated?: boolean | null;
  fields?: Field[] | null;
  readableFields?: Field[] | null;
  writableFields?: Field[] | null;
  parameters?: Parameter[] | null;
  getParameters?: () => Promise<Parameter[]> | null;
  operations?: Operation[] | null;
}

For Hydra resources, getParameters() loads parameters lazily and caches the result for the lifetime of the Resource instance. Run the documentation parser again to refresh parameters after the API schema or authorization context changes.

Field

Represents a property of a resource, including its type, constraints, and metadata.

interface Field {
  name: string | null;
  id?: string | null;
  range?: string | null;
  type?: FieldType | null;
  arrayType?: FieldType | null;
  enum?: { [key: string | number]: string | number } | null;
  reference?: string | Resource | null;
  embedded?: Resource | null;
  required?: boolean | null;
  nullable?: boolean | null;
  description?: string | null;
  maxCardinality?: number | null;
  deprecated?: boolean | null;
}

Parameter

Represents a query parameter for a collection/list operation, such as a filter or pagination variable.

interface Parameter {
  variable: string;
  range: string | null;
  required: boolean;
  description: string;
  deprecated?: boolean;
}

FieldType

Enumerates the possible types for a field, such as string, integer, date, etc.

type FieldType =
  | "string"
  | "integer"
  | "negativeInteger"
  | "nonNegativeInteger"
  | "positiveInteger"
  | "nonPositiveInteger"
  | "number"
  | "decimal"
  | "double"
  | "float"
  | "boolean"
  | "date"
  | "dateTime"
  | "duration"
  | "time"
  | "byte"
  | "binary"
  | "hexBinary"
  | "base64Binary"
  | "array"
  | "object"
  | "email"
  | "url"
  | "uuid"
  | "password"
  | string;

Operation

Represents an operation (such as GET, POST, PUT, PATCH, DELETE) that can be performed on a resource.

interface Operation {
  name: string | null;
  type: "show" | "edit" | "delete" | "list" | "create" | null;
  method?: string | null;
  expects?: any | null;
  returns?: string | null;
  types?: string[] | null;
  deprecated?: boolean | null;
}

📖 OpenAPI Support

api-doc-parser applies a predictable set of rules when interpreting an OpenAPI document.
If a rule is not met, the resource concerned is silently skipped.

RuleDetails
Single-item path patternA GET (read) or PUT/PATCH (update) endpoint must match:
/books/{id} (regex ^[^{}]+/{[^{}]+}/?$).
books may be singular (/book/{id}).
Schema discoveryGET → first searches responses → 200 → content → application/json; if missing, falls back to components (component name must be singular, e.g. Book).
PUT/PATCH → only requestBody → content → application/json is considered.
If both GET & PUT/PATCH schemas exist, their fields are merged.
Collection pathsA create (POST) or list (GET) endpoint must be plural:
/books.
Deletion pathDELETE must live under the single-item GET path (/books/{id}).
Relations & EmbeddedsLinks between resources are inferred from property names and their JSON schema:
• Plural object/array properties (e.g. reviews, authors) become embedded resources when their item schema matches an existing resource (Review, Author).
• ID-like properties (e.g. review_id, reviewId, review_ids, reviewIds, authorId) are treated as references to that resource.
• As a result, fields such as reviews (object/array) and review_ids (scalar/array of IDs) each point to the same Review resource, one flagged embedded, the other reference.
Parameter extractionParameters are read only from the list path (/books).

🧩 Support for other formats (JSON:API, AsyncAPI...)

API Doc Parser is designed to parse any API documentation format and convert it in the same intermediate representation. If you develop a parser for another format, please open a Pull Request to include it in the library.

🤝 Contributing

Contributions are welcome! To contribute:

  1. Read our Code of Conduct.

  2. Fork the repository and create a feature branch.

  3. Ensure you have the latest version of pnpm installed.

  4. Install dependencies

    pnpm install
    
  5. Adhere to the code style and lint rules

    pnpm lint:fix
    
    pnpm format
    
  6. Run tests

    pnpm test
    
  7. Ensure type correctness

    pnpm typecheck
    
  8. Submit a pull request with a clear description of your changes.

👥 Contributors

Contributors

🌟 Star History

Star History Chart

🙌 Credits

Created by Kévin Dunglas. Sponsored by Les-Tilleuls.coop.

🔒 License

This project is licensed under the MIT License - see the LICENSE file for details.

版本与发布记录

@api-platform/api-doc-parser 共发布 51 个版本,首次发布于 2017/10/06;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.16.12latest2026/09/291 个月前
0.16.102026/02/098 个月前
0.16.92026/02/098 个月前
0.16.82025/03/111.7 年前
0.16.72025/01/301.8 年前
0.16.62024/10/292 年前

关于 @api-platform/api-doc-parser 的常见问题

@api-platform/api-doc-parser 是什么?

Transform an API documentation (Hydra, OpenAPI, GraphQL) in an intermediate representation that can be used for various tasks such as creating smart API clients, scaffolding code or building administration interfaces.

如何安装 @api-platform/api-doc-parser?

在项目目录运行 npm install @api-platform/api-doc-parser,或使用 pnpm add / yarn add / bun add。

@api-platform/api-doc-parser 支持 TypeScript 吗?

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

@api-platform/api-doc-parser 支持 ESM 还是 CommonJS?

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

@api-platform/api-doc-parser 需要什么版本的 Node.js?

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

@api-platform/api-doc-parser 可以商用吗?

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

@api-platform/api-doc-parser 的下载量有多少?

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

@api-platform/api-doc-parser 还在维护吗?

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