Skip to content

@ahoo-wang/fetcher-openapi

@ahoo-wang/fetcher-openapi 包为 OpenAPI 3.x 规范 提供全面的 TypeScript 类型定义。它没有任何运行时依赖,被 generator 包用于在生成类型安全的 API 客户端代码之前解析和验证 OpenAPI 文档。

源码: packages/openapi/src/

安装

bash
pnpm add @ahoo-wang/fetcher-openapi

独立包

此包没有对等依赖,也没有运行时代码。它仅导出 TypeScript 接口和类型,适用于任何处理 OpenAPI 文档的 TypeScript 项目。

架构

mermaid
graph TB
    subgraph sg_1 ["OpenAPI Document"]
        JSON["openapi.json / openapi.yaml"]
    end

    subgraph sg_2 ["@ahoo-wang/fetcher-openapi"]
        ROOT["OpenAPI<br>Root document"]
        INFO["Info / Contact / License<br>API metadata"]
        SERVER["Server / ServerVariable<br>Server configuration"]
        PATHS["Paths / PathItem / Operation<br>API endpoints"]
        PARAM["Parameter / RequestBody / MediaType<br>Request types"]
        RESP["Responses / Response / Link<br>Response types"]
        SCHEMA["Schema / Discriminator<br>Data models"]
        COMP["Components<br>Reusable definitions"]
        SEC["SecurityScheme / OAuthFlows<br>Authentication"]
        REF["Reference<br>$ref support"]
        EXT["Extensible<br>x- extensions"]
    end

    subgraph sg_3 ["Consumers"]
        GEN["@ahoo-wang/fetcher-generator<br>Code generation"]
        VIEWER["@ahoo-wang/fetcher-viewer<br>API documentation"]
    end

    JSON --> ROOT
    ROOT --> INFO
    ROOT --> SERVER
    ROOT --> PATHS
    ROOT --> COMP
    ROOT --> SEC
    PATHS --> PARAM
    PATHS --> RESP
    COMP --> SCHEMA
    COMP --> PARAM
    COMP --> RESP
    COMP --> SEC
    PARAM --> SCHEMA
    PARAM --> REF
    RESP --> REF
    ROOT --> REF
    ROOT --> EXT

    GEN --> ROOT
    VIEWER --> ROOT

    style JSON fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style ROOT fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style INFO fill:#161b22,stroke:#30363d,color:#e6edf3
    style SERVER fill:#161b22,stroke:#30363d,color:#e6edf3
    style PATHS fill:#161b22,stroke:#30363d,color:#e6edf3
    style PARAM fill:#161b22,stroke:#30363d,color:#e6edf3
    style RESP fill:#161b22,stroke:#30363d,color:#e6edf3
    style SCHEMA fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style COMP fill:#161b22,stroke:#30363d,color:#e6edf3
    style SEC fill:#161b22,stroke:#30363d,color:#e6edf3
    style REF fill:#161b22,stroke:#30363d,color:#e6edf3
    style EXT fill:#161b22,stroke:#30363d,color:#e6edf3
    style GEN fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style VIEWER fill:#2d333b,stroke:#6d5dfc,color:#e6edf3

类型层次结构

mermaid
classDiagram
    class OpenAPI {
        +openapi: string
        +info: Info
        +servers?: Server[]
        +paths: Paths
        +components?: Components
        +security?: SecurityRequirement[]
        +tags?: Tag[]
        +externalDocs?: ExternalDocumentation
    }

    class Paths {
        +[path: string]: PathItem
    }

    class PathItem {
        +get?: Operation
        +put?: Operation
        +post?: Operation
        +delete?: Operation
        +patch?: Operation
        +parameters?: (Parameter | Reference)[]
    }

    class Operation {
        +tags?: string[]
        +summary?: string
        +operationId?: string
        +parameters?: (Parameter | Reference)[]
        +requestBody?: RequestBody | Reference
        +responses: Responses
    }

    class Schema {
        +type?: SchemaType
        +properties?: Record~string, Schema | Reference~
        +required?: string[]
        +items?: Schema | Reference
        +allOf?: (Schema | Reference)[]
    }

    class Components {
        +schemas?: Record~string, Schema~
        +responses?: Record~string, Response~
        +parameters?: Record~string, Parameter~
        +securitySchemes?: Record~string, SecurityScheme~
    }

    OpenAPI --> Paths
    Paths --> PathItem
    PathItem --> Operation
    Operation --> Schema
    OpenAPI --> Components
    Components --> Schema

根文档(OpenAPI)

OpenAPI 接口表示根 OpenAPI 文档。(openAPI.ts:41)

typescript
import type { OpenAPI } from '@ahoo-wang/fetcher-openapi';

const spec: OpenAPI = {
  openapi: '3.0.3',
  info: {
    title: 'My API',
    version: '1.0.0',
  },
  paths: {
    '/users': {
      get: {
        operationId: 'listUsers',
        parameters: [
          { name: 'limit', in: 'query', schema: { type: 'integer' } },
        ],
        responses: {
          '200': {
            description: 'Successful response',
            content: {
              'application/json': {
                schema: {
                  type: 'array',
                  items: { $ref: '#/components/schemas/User' },
                },
              },
            },
          },
        },
      },
    },
  },
  components: {
    schemas: {
      User: {
        type: 'object',
        required: ['id', 'name'],
        properties: {
          id: { type: 'integer', format: 'int64' },
          name: { type: 'string' },
          email: { type: 'string', format: 'email' },
        },
      },
    },
  },
};

Schema 类型

Schema 接口是最复杂的类型,支持 OpenAPI 3.x 中使用的所有 JSON Schema 特性。(schema.ts:91)

mermaid
graph TD
    subgraph sg_1 ["Schema Category"]
        GEN["General Properties<br>title, description, type, format,<br>nullable, deprecated, example, default"]
        NUM["Numeric Constraints<br>minimum, maximum,<br>exclusiveMinimum, exclusiveMaximum, multipleOf"]
        STR["String Constraints<br>minLength, maxLength, pattern"]
        ARR["Array Constraints<br>items, minItems, maxItems, uniqueItems"]
        OBJ["Object Constraints<br>properties, required,<br>additionalProperties"]
        COMP["Composition<br>allOf, anyOf, oneOf, not"]
        POLY["Polymorphism<br>discriminator"]
        ENUM["Enumeration<br>enum"]
    end

    SCHEMA["Schema"] --> GEN
    SCHEMA --> NUM
    SCHEMA --> STR
    SCHEMA --> ARR
    SCHEMA --> OBJ
    SCHEMA --> COMP
    SCHEMA --> POLY
    SCHEMA --> ENUM

    style SCHEMA fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style GEN fill:#161b22,stroke:#30363d,color:#e6edf3
    style NUM fill:#161b22,stroke:#30363d,color:#e6edf3
    style STR fill:#161b22,stroke:#30363d,color:#e6edf3
    style ARR fill:#161b22,stroke:#30363d,color:#e6edf3
    style OBJ fill:#161b22,stroke:#30363d,color:#e6edf3
    style COMP fill:#161b22,stroke:#30363d,color:#e6edf3
    style POLY fill:#161b22,stroke:#30363d,color:#e6edf3
    style ENUM fill:#161b22,stroke:#30363d,color:#e6edf3

SchemaType

OpenAPI/JSON Schema 支持的原始类型:(base-types.ts:44)

typescript
type SchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'null';

关键 Schema 属性

属性类型描述
typeSchemaType | SchemaType[]数据类型
formatstring扩展格式(如 int64date-timeemail
propertiesRecord<string, Schema | Reference>对象属性
requiredstring[]必填属性名
itemsSchema | Reference数组项的 Schema
allOf(Schema | Reference)[]必须匹配所有 Schema
anyOf(Schema | Reference)[]匹配任一 Schema
oneOf(Schema | Reference)[]精确匹配一个 Schema
enumany[]允许的值
nullableboolean是否允许 null
discriminatorDiscriminator多态支持

Paths 和 Operations

PathItem

描述单个 URL 路径上可用的操作。支持所有 HTTP 方法。(paths.ts:76)

属性类型描述
getOperationGET 操作
putOperationPUT 操作
postOperationPOST 操作
deleteOperationDELETE 操作
patchOperationPATCH 操作
headOperationHEAD 操作
optionsOperationOPTIONS 操作
traceOperationTRACE 操作
parameters(Parameter | Reference)[]所有操作共享的参数

Operation

描述单个 API 操作。(paths.ts:44)

属性类型描述
operationIdstring唯一操作标识符
tagsstring[]文档标签
summarystring简要摘要
descriptionstring详细描述
parameters(Parameter | Reference)[]操作参数
requestBodyRequestBody | Reference请求体定义
responsesResponses可能的响应
deprecatedboolean该操作是否已弃用
securitySecurityRequirement[]安全要求
callbacksRecord<string, Callback | Reference>带外回调

Parameters 和 Request Bodies

Parameter

描述单个操作参数。(parameters.ts:40)

属性类型描述
namestring参数名
inParameterLocation位置:queryheaderpathcookie
requiredboolean参数是否必填
schemaSchema | Reference参数 Schema
descriptionstring参数描述
deprecatedboolean参数是否已弃用
typescript
type ParameterLocation = 'query' | 'header' | 'path' | 'cookie';

RequestBody

属性类型描述
contentRecord<string, MediaType>媒体类型映射
requiredboolean请求体是否必填
descriptionstring请求体描述

MediaType

属性类型描述
schemaSchema | Reference此媒体类型的 Schema
exampleany示例值
examplesRecord<string, Example | Reference>命名示例
encodingRecord<string, Encoding>编码信息

Responses

Response

描述 API 操作的单个响应。(responses.ts:52)

属性类型描述
descriptionstring响应描述
headersRecord<string, Header | Reference>响应头
contentRecord<string, MediaType>响应体媒体类型
linksRecord<string, Link | Reference>后续操作链接

Responses

一个将 HTTP 状态码映射到 Response 对象的容器:

typescript
interface Responses {
  default?: Response | Reference;   // 回退响应
  [httpCode: string]: Response | Reference | undefined;  // 如 "200"、"404"、"500"
}

Components

Components 对象保存可通过 $ref 在整个文档中引用的可复用定义。(components.ts:42)

属性类型描述
schemasRecord<string, Schema>可复用的数据模型
responsesRecord<string, Response>可复用的响应定义
parametersRecord<string, Parameter>可复用的参数
requestBodiesRecord<string, RequestBody>可复用的请求体
headersRecord<string, Header | Reference>可复用的请求头
securitySchemesRecord<string, SecurityScheme>可复用的安全方案
linksRecord<string, Link>可复用的链接
callbacksRecord<string, Callback>可复用的回调
examplesRecord<string, Example | Reference>可复用的示例

References

Reference 类型支持基于 JSON Pointer 的引用($ref),指向可复用的组件。(reference.ts:23)

typescript
interface Reference {
  $ref: string;
}

// 在 Schema 中使用
const userRef: Reference = { $ref: '#/components/schemas/User' };
const responseRef: Reference = { $ref: '#/components/responses/NotFound' };

IsReference<T> 工具类型帮助区分引用和内联定义:

typescript
type IsReference<T> = T extends { $ref: string } ? T : never;

Security

SecurityScheme

支持四种认证类型:(security.ts:63)

类型描述附加属性
apiKey位于 header、query 或 cookie 中的 API 密钥namein
httpHTTP 认证schemebearerFormat
oauth2OAuth 2.0flows(OAuthFlows)
openIdConnectOpenID ConnectopenIdConnectUrl

OAuthFlows

属性类型描述
implicitOAuthFlow隐式授权流程
passwordOAuthFlow资源所有者密码流程
clientCredentialsOAuthFlow客户端凭证流程
authorizationCodeOAuthFlow授权码流程

扩展支持(Extensible)

大多数类型都扩展了 Extensible 接口,允许使用 x- 前缀的自定义属性:(extensions.ts:22)

typescript
interface Extensible {
  [extension: `x-${string}`]: any;
}

// 使用示例
const operation: Operation = {
  responses: { '200': { description: 'OK' } },
  'x-rate-limit': 100,
  'x-cache-ttl': 300,
};

Generator 如何使用此包

generator 包读取 OpenAPI 文档并将其类型映射为 TypeScript 代码:

mermaid
sequenceDiagram
autonumber

    participant User as Developer
    participant CLI as fetcher-generator
    participant Reader as YAML/JSON Reader
    participant Types as @ahoo-wang/fetcher-openapi
    participant Morph as ts-morph
    participant FS as File System

    User->>CLI: fetcher generate --input openapi.yaml
    CLI->>Reader: Read and parse spec file
    Reader-->>CLI: Parsed document
    CLI->>CLI: Validate as OpenAPI type
    CLI->>CLI: Iterate paths and schemas

    loop For each Schema in components.schemas
        CLI->>Morph: Generate TypeScript interface
        Morph->>FS: Write interface file
    end

    loop For each Operation in paths
        CLI->>Morph: Generate @api/@get/@post class
        Morph->>FS: Write API service file
    end

    CLI-->>User: Generated files in output directory
mermaid
flowchart LR
    subgraph sg_1 ["Input"]
        SPEC["openapi.json / yaml / URL"]
    end

    subgraph sg_2 ["@ahoo-wang/fetcher-openapi"]
        TYPED["Typed OpenAPI Document<br>OpenAPI, Schema, Paths, ..."]
    end

    subgraph sg_3 ["@ahoo-wang/fetcher-generator"]
        PARSE["Parse & Validate"]
        SCHEMA_GEN["Generate TS interfaces<br>from Schema"]
        API_GEN["Generate @api classes<br>from Operation"]
        WOW_GEN["Generate Wow CQRS<br>from tags"]
    end

    subgraph sg_4 ["Output"]
        INTERFACES["*.ts interfaces"]
        SERVICES["*.ts API services"]
        CQRS["*.ts CQRS clients"]
    end

    SPEC --> PARSE
    PARSE --> TYPED
    TYPED --> SCHEMA_GEN
    TYPED --> API_GEN
    TYPED --> WOW_GEN
    SCHEMA_GEN --> INTERFACES
    API_GEN --> SERVICES
    WOW_GEN --> CQRS

    style SPEC fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style TYPED fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style PARSE fill:#161b22,stroke:#30363d,color:#e6edf3
    style SCHEMA_GEN fill:#161b22,stroke:#30363d,color:#e6edf3
    style API_GEN fill:#161b22,stroke:#30363d,color:#e6edf3
    style WOW_GEN fill:#161b22,stroke:#30363d,color:#e6edf3
    style INTERFACES fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style SERVICES fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style CQRS fill:#2d333b,stroke:#6d5dfc,color:#e6edf3

从 OpenAPI 构造到生成的 TypeScript 的映射:

OpenAPI 构造生成的 TypeScript
components.schemas.*TypeScript interfaceenum
paths.*.get/post/put/delete@get/@post/@put/@del 装饰的方法
in: pathparameters@path() 参数
in: queryparameters@query() 参数
in: headerparameters@header() 参数
requestBody@body() 参数
responses.200.content.application/json.schema返回类型

导出 API 总结

导出类型源码文件
OpenAPI接口openAPI.ts
Schema接口schema.ts
Discriminator接口schema.ts
Paths接口paths.ts
PathItem接口paths.ts
Operation接口paths.ts
Parameter接口parameters.ts
RequestBody接口parameters.ts
MediaType接口parameters.ts
Responses接口responses.ts
Response接口responses.ts
Components接口components.ts
Reference接口reference.ts
SecurityScheme接口security.ts
OAuthFlows接口security.ts
Info接口info.ts
Server接口server.ts
Tag接口tags.ts
Extensible接口extensions.ts
SchemaType类型base-types.ts
ParameterLocation类型base-types.ts
HTTPMethod类型base-types.ts

相关页面

  • Generator - 使用此包生成 TypeScript API 客户端
  • Viewer - 使用这些类型进行 API 文档渲染
  • Decorator - 生成的代码使用这些装饰器
  • 包概览 - 生态系统中的所有包

基于 Apache License 2.0 发布。