Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Transforms a Hydra API doc in an intermediate representation that can be used for various tasks such as creating smart API clients, scaffolding code or building administration interfaces.

License

NotificationsYou must be signed in to change notification settings

api-platform/api-doc-parser

Repository files navigation

API doc parser

API Doc Parser

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

CIGitHub Licensenpm bundle sizenpm versionNPM Downloads


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

✨ Key Features

  • Unified output – one normalizedApi 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

UsingNPM:

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

UsingPnpm:

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

WithYarn:

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

UsingBun:

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

🚀 Usage

Hydra

import{parseHydraDocumentation}from"@api-platform/api-doc-parser";const{ api, response, status}=awaitparseHydraDocumentation("https://demo.api-platform.com",);

OpenAPI v2 (formerly known as Swagger)

import{parseSwaggerDocumentation}from"@api-platform/api-doc-parser";const{ api, response, status}=awaitparseSwaggerDocumentation("https://demo.api-platform.com/docs.json",);

OpenAPI v3

import{parseOpenApi3Documentation}from"@api-platform/api-doc-parser";const{ api, response, status}=awaitparseOpenApi3Documentation("https://demo.api-platform.com/docs.jsonopenapi?spec_version=3.0.0",);

GraphQL

import{parseGraphQl}from"@api-platform/api-doc-parser";const{ api, response}=awaitparseGraphQl("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

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

Swagger

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

Hydra

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

GraphQL

functionparseGraphQl(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.

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

Resource

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

interfaceResource{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;}

Field

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

interfaceField{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.

interfaceParameter{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.

typeFieldType=|"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.

interfaceOperation{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 patternAGET (read) orPUT/PATCH (update) endpointmust match:
/books/{id} (regex ^[^{}]+/{[^{}]+}/?$).
books may be singular (/book/{id}).
Schema discoveryGET → first searchesresponses → 200 → content → application/json; if missing, falls back tocomponents (component name must be singular, e.g.Book).
PUT/PATCH → onlyrequestBody → content → application/json is considered.
If both GET & PUT/PATCH schemas exist, their fields aremerged.
Collection pathsA create (POST) or list (GET) endpointmust 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) becomeembedded 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 asreferences to that resource.
• As a result, fields such asreviews (object/array) andreview_ids (scalar/array of IDs) each point to thesameReview resource, one flaggedembedded, the otherreference.
Parameter extractionParameters are readonly 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, pleaseopen a Pull Requestto include it in the library.

🤝 Contributing

Contributions are welcome! To contribute:

  1. Read ourCode of Conduct.

  2. Fork the repository and create a feature branch.

  3. Ensure you have thelatest version ofpnpm installed.

  4. Install dependencies

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

    pnpm lint:fix
    pnpm format
  6. Run tests

    pnpmtest
  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 byKévin Dunglas. Sponsored byLes-Tilleuls.coop.

🔒 License

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

About

Transforms a Hydra API doc in an intermediate representation that can be used for various tasks such as creating smart API clients, scaffolding code or building administration interfaces.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors34


[8]ページ先頭

©2009-2025 Movatter.jp