@ahoo-wang/fetcher-openapi 2.1.1 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +174 -22
  2. package/README.zh-CN.md +174 -22
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -13,10 +13,13 @@ ultra-lightweight HTTP client.
13
13
 
14
14
  ## Features
15
15
 
16
- - 📦 **Ultra-lightweight** - Minimal overhead TypeScript types only
17
- - 🦺 **Full TypeScript Support** - Complete type definitions for OpenAPI 3.x specification
18
- - 🧩 **Modular Design** - Import only what you need
16
+ - 📦 **Ultra-lightweight** - Zero runtime overhead, TypeScript types only (~2KB)
17
+ - 🦺 **Full TypeScript Support** - Complete type definitions for OpenAPI 3.0+ specification
18
+ - 🧩 **Modular Design** - Import only what you need from specific modules
19
19
  - 🎯 **Framework Agnostic** - Works with any OpenAPI-compatible tooling
20
+ - 🔧 **Extension Support** - Built-in support for OpenAPI extensions (x-\* properties)
21
+ - 📚 **Comprehensive Coverage** - All OpenAPI 3.0+ features including schemas, parameters, responses, security, etc.
22
+ - 🏗️ **Type-Safe Development** - Leverage TypeScript's type system for API development
20
23
 
21
24
  ## Installation
22
25
 
@@ -31,7 +34,12 @@ npm install @ahoo-wang/fetcher-openapi
31
34
  Import OpenAPI specification types:
32
35
 
33
36
  ```typescript
34
- import type { OpenAPI, Schema, Operation } from '@ahoo-wang/fetcher-openapi';
37
+ import type {
38
+ OpenAPI,
39
+ Schema,
40
+ Operation,
41
+ Components,
42
+ } from '@ahoo-wang/fetcher-openapi';
35
43
 
36
44
  const openAPI: OpenAPI = {
37
45
  openapi: '3.0.1',
@@ -43,6 +51,7 @@ const openAPI: OpenAPI = {
43
51
  '/users': {
44
52
  get: {
45
53
  summary: 'Get users',
54
+ operationId: 'getUsers',
46
55
  responses: {
47
56
  '200': {
48
57
  description: 'A list of users',
@@ -61,6 +70,85 @@ const openAPI: OpenAPI = {
61
70
  },
62
71
  },
63
72
  },
73
+ components: {
74
+ schemas: {
75
+ User: {
76
+ type: 'object',
77
+ properties: {
78
+ id: { type: 'integer' },
79
+ name: { type: 'string' },
80
+ email: { type: 'string', format: 'email' },
81
+ },
82
+ required: ['id', 'name'],
83
+ },
84
+ },
85
+ },
86
+ };
87
+ ```
88
+
89
+ ### Working with Schemas
90
+
91
+ Define complex data structures with full OpenAPI Schema support:
92
+
93
+ ```typescript
94
+ import type { Schema, Discriminator, XML } from '@ahoo-wang/fetcher-openapi';
95
+
96
+ const userSchema: Schema = {
97
+ type: 'object',
98
+ properties: {
99
+ id: { type: 'integer', minimum: 1 },
100
+ name: { type: 'string', minLength: 1, maxLength: 100 },
101
+ email: { type: 'string', format: 'email' },
102
+ role: { type: 'string', enum: ['admin', 'user', 'guest'] },
103
+ createdAt: { type: 'string', format: 'date-time' },
104
+ },
105
+ required: ['id', 'name', 'email'],
106
+ };
107
+
108
+ const polymorphicSchema: Schema = {
109
+ oneOf: [
110
+ { $ref: '#/components/schemas/Admin' },
111
+ { $ref: '#/components/schemas/User' },
112
+ ],
113
+ discriminator: {
114
+ propertyName: 'role',
115
+ mapping: {
116
+ admin: '#/components/schemas/Admin',
117
+ user: '#/components/schemas/User',
118
+ },
119
+ },
120
+ };
121
+ ```
122
+
123
+ ### Extensions Support
124
+
125
+ Use OpenAPI extensions for custom functionality:
126
+
127
+ ```typescript
128
+ import type { Operation, CommonExtensions } from '@ahoo-wang/fetcher-openapi';
129
+
130
+ const operationWithExtensions: Operation & CommonExtensions = {
131
+ summary: 'Get user profile',
132
+ operationId: 'getUserProfile',
133
+ 'x-internal': false,
134
+ 'x-deprecated': {
135
+ message: 'Use getUser instead',
136
+ since: '2.0.0',
137
+ removedIn: '3.0.0',
138
+ replacement: 'getUser',
139
+ },
140
+ 'x-tags': ['users', 'profile'],
141
+ 'x-order': 1,
142
+ responses: {
143
+ '200': {
144
+ description: 'User profile',
145
+ content: {
146
+ 'application/json': {
147
+ schema: { $ref: '#/components/schemas/UserProfile' },
148
+ },
149
+ },
150
+ },
151
+ },
64
152
  };
65
153
  ```
66
154
 
@@ -68,24 +156,88 @@ const openAPI: OpenAPI = {
68
156
 
69
157
  ### Core Types
70
158
 
71
- - `OpenAPI` - Root document object
72
- - `Info` - API metadata
73
- - `Server` - Server configuration
74
- - `Paths` - API paths and operations
75
- - `Operation` - Single API operation
76
- - `Schema` - Data schema definition
77
- - `Parameter` - Operation parameter
78
- - `RequestBody` - Request body definition
79
- - `Response` - Response definition
80
- - `Components` - Reusable components
81
- - `SecurityScheme` - Security scheme definition
82
-
83
- ### Utility Types
84
-
85
- - `ExtractSchemaType<S>` - Extract TypeScript type from OpenAPI Schema
86
- - `ExtractRequestBodyType<Op, Components>` - Extract request body type from Operation
87
- - `ExtractOkResponseBodyType<Op, Components>` - Extract successful response body type from Operation
88
- - `ResolveReference<R, Components>` - Resolve $ref references to actual types
159
+ #### Document Structure
160
+
161
+ - `OpenAPI` - Root OpenAPI document object
162
+ - `Info` - API metadata (title, version, description, etc.)
163
+ - `Contact` - Contact information for the API
164
+ - `License` - License information
165
+ - `Server` - Server configuration with variables
166
+ - `Paths` - Collection of API paths and their operations
167
+ - `Components` - Reusable components (schemas, parameters, responses, etc.)
168
+ - `Tag` - API grouping and documentation tags
169
+
170
+ #### Operations & Parameters
171
+
172
+ - `Operation` - Single API operation (GET, POST, etc.)
173
+ - `Parameter` - Operation parameter (query, path, header, cookie)
174
+ - `RequestBody` - Request body definition with content types
175
+ - `Response` - Response definition with status codes
176
+ - `MediaType` - Content type definitions with schemas
177
+ - `Encoding` - Serialization rules for request/response bodies
178
+
179
+ #### Data Schemas
180
+
181
+ - `Schema` - JSON Schema-based data structure definitions
182
+ - `Discriminator` - Polymorphism support with discriminator fields
183
+ - `XML` - XML serialization configuration
184
+ - `Reference` - JSON Reference ($ref) for reusable components
185
+
186
+ #### Security
187
+
188
+ - `SecurityScheme` - Authentication scheme definitions
189
+ - `SecurityRequirement` - Required security schemes for operations
190
+
191
+ ### Extensions & Utilities
192
+
193
+ #### Extension Support
194
+
195
+ - `Extensible` - Base interface for objects supporting extensions
196
+ - `CommonExtensions` - Predefined extension properties (x-internal, x-deprecated, etc.)
197
+
198
+ #### Type Utilities
199
+
200
+ - `HTTPMethod` - Supported HTTP methods ('get', 'post', 'put', 'delete', etc.)
201
+ - `ParameterLocation` - Parameter locations ('query', 'header', 'path', 'cookie')
202
+ - `SchemaType` - JSON Schema primitive types ('string', 'number', 'boolean', etc.)
203
+
204
+ ### Advanced Usage
205
+
206
+ #### Modular Imports
207
+
208
+ Import only the types you need for better tree-shaking:
209
+
210
+ ```typescript
211
+ // Import specific types
212
+ import type { OpenAPI, Schema, Operation } from '@ahoo-wang/fetcher-openapi';
213
+
214
+ // Or import from specific modules
215
+ import type { OpenAPI } from '@ahoo-wang/fetcher-openapi/openAPI';
216
+ import type { Schema } from '@ahoo-wang/fetcher-openapi/schema';
217
+ import type { Operation } from '@ahoo-wang/fetcher-openapi/paths';
218
+ ```
219
+
220
+ #### Type-Safe API Development
221
+
222
+ Use these types to build type-safe API clients and documentation:
223
+
224
+ ```typescript
225
+ function validateOpenAPI(doc: OpenAPI): boolean {
226
+ // TypeScript will catch type errors at compile time
227
+ return doc.openapi.startsWith('3.');
228
+ }
229
+
230
+ function createOperation(
231
+ path: string,
232
+ method: HTTPMethod,
233
+ config: Partial<Operation>,
234
+ ): Operation {
235
+ return {
236
+ operationId: `${method}${path.replace(/\//g, '')}`,
237
+ ...config,
238
+ };
239
+ }
240
+ ```
89
241
 
90
242
  ## License
91
243
 
package/README.zh-CN.md CHANGED
@@ -12,10 +12,13 @@
12
12
 
13
13
  ## 功能特性
14
14
 
15
- - 📦 **超轻量级** - 最小化开销的 TypeScript 类型定义
16
- - 🦺 **完整 TypeScript 支持** - OpenAPI 3.x 规范的完整类型定义
17
- - 🧩 **模块化设计** - 按需导入所需功能
15
+ - 📦 **超轻量级** - 零运行时开销,仅 TypeScript 类型(~2KB)
16
+ - 🦺 **完整 TypeScript 支持** - OpenAPI 3.0+ 规范的完整类型定义
17
+ - 🧩 **模块化设计** - 从特定模块按需导入所需功能
18
18
  - 🎯 **框架无关** - 适用于任何与 OpenAPI 兼容的工具
19
+ - 🔧 **扩展支持** - 内置支持 OpenAPI 扩展(x-\* 属性)
20
+ - 📚 **全面覆盖** - 涵盖所有 OpenAPI 3.0+ 功能,包括模式、参数、响应、安全等
21
+ - 🏗️ **类型安全开发** - 利用 TypeScript 类型系统进行 API 开发
19
22
 
20
23
  ## 安装
21
24
 
@@ -30,7 +33,12 @@ npm install @ahoo-wang/fetcher-openapi
30
33
  导入 OpenAPI 规范类型:
31
34
 
32
35
  ```typescript
33
- import type { OpenAPI, Schema, Operation } from '@ahoo-wang/fetcher-openapi';
36
+ import type {
37
+ OpenAPI,
38
+ Schema,
39
+ Operation,
40
+ Components,
41
+ } from '@ahoo-wang/fetcher-openapi';
34
42
 
35
43
  const openAPI: OpenAPI = {
36
44
  openapi: '3.0.1',
@@ -42,6 +50,7 @@ const openAPI: OpenAPI = {
42
50
  '/users': {
43
51
  get: {
44
52
  summary: '获取用户列表',
53
+ operationId: 'getUsers',
45
54
  responses: {
46
55
  '200': {
47
56
  description: '用户列表',
@@ -60,6 +69,85 @@ const openAPI: OpenAPI = {
60
69
  },
61
70
  },
62
71
  },
72
+ components: {
73
+ schemas: {
74
+ User: {
75
+ type: 'object',
76
+ properties: {
77
+ id: { type: 'integer' },
78
+ name: { type: 'string' },
79
+ email: { type: 'string', format: 'email' },
80
+ },
81
+ required: ['id', 'name'],
82
+ },
83
+ },
84
+ },
85
+ };
86
+ ```
87
+
88
+ ### 使用模式
89
+
90
+ 使用完整的 OpenAPI Schema 支持定义复杂数据结构:
91
+
92
+ ```typescript
93
+ import type { Schema, Discriminator, XML } from '@ahoo-wang/fetcher-openapi';
94
+
95
+ const userSchema: Schema = {
96
+ type: 'object',
97
+ properties: {
98
+ id: { type: 'integer', minimum: 1 },
99
+ name: { type: 'string', minLength: 1, maxLength: 100 },
100
+ email: { type: 'string', format: 'email' },
101
+ role: { type: 'string', enum: ['admin', 'user', 'guest'] },
102
+ createdAt: { type: 'string', format: 'date-time' },
103
+ },
104
+ required: ['id', 'name', 'email'],
105
+ };
106
+
107
+ const polymorphicSchema: Schema = {
108
+ oneOf: [
109
+ { $ref: '#/components/schemas/Admin' },
110
+ { $ref: '#/components/schemas/User' },
111
+ ],
112
+ discriminator: {
113
+ propertyName: 'role',
114
+ mapping: {
115
+ admin: '#/components/schemas/Admin',
116
+ user: '#/components/schemas/User',
117
+ },
118
+ },
119
+ };
120
+ ```
121
+
122
+ ### 扩展支持
123
+
124
+ 使用 OpenAPI 扩展实现自定义功能:
125
+
126
+ ```typescript
127
+ import type { Operation, CommonExtensions } from '@ahoo-wang/fetcher-openapi';
128
+
129
+ const operationWithExtensions: Operation & CommonExtensions = {
130
+ summary: '获取用户资料',
131
+ operationId: 'getUserProfile',
132
+ 'x-internal': false,
133
+ 'x-deprecated': {
134
+ message: '请使用 getUser 替代',
135
+ since: '2.0.0',
136
+ removedIn: '3.0.0',
137
+ replacement: 'getUser',
138
+ },
139
+ 'x-tags': ['users', 'profile'],
140
+ 'x-order': 1,
141
+ responses: {
142
+ '200': {
143
+ description: '用户资料',
144
+ content: {
145
+ 'application/json': {
146
+ schema: { $ref: '#/components/schemas/UserProfile' },
147
+ },
148
+ },
149
+ },
150
+ },
63
151
  };
64
152
  ```
65
153
 
@@ -67,24 +155,88 @@ const openAPI: OpenAPI = {
67
155
 
68
156
  ### 核心类型
69
157
 
70
- - `OpenAPI` - 根文档对象
71
- - `Info` - API 元数据
72
- - `Server` - 服务器配置
73
- - `Paths` - API 路径和操作
74
- - `Operation` - 单个 API 操作
75
- - `Schema` - 数据模式定义
76
- - `Parameter` - 操作参数
77
- - `RequestBody` - 请求体定义
78
- - `Response` - 响应定义
79
- - `Components` - 可复用组件
80
- - `SecurityScheme` - 安全方案定义
81
-
82
- ### 工具类型
83
-
84
- - `ExtractSchemaType<S>` - 从 OpenAPI Schema 中提取 TypeScript 类型
85
- - `ExtractRequestBodyType<Op, Components>` - 从 Operation 中提取请求体类型
86
- - `ExtractOkResponseBodyType<Op, Components>` - 从 Operation 中提取成功响应体类型
87
- - `ResolveReference<R, Components>` - 解析 $ref 引用为实际类型
158
+ #### 文档结构
159
+
160
+ - `OpenAPI` - OpenAPI 根文档对象
161
+ - `Info` - API 元数据(标题、版本、描述等)
162
+ - `Contact` - API 联系信息
163
+ - `License` - 许可证信息
164
+ - `Server` - 带变量的服务器配置
165
+ - `Paths` - API 路径及其操作的集合
166
+ - `Components` - 可复用组件(模式、参数、响应等)
167
+ - `Tag` - API 分组和文档标签
168
+
169
+ #### 操作和参数
170
+
171
+ - `Operation` - 单个 API 操作(GET、POST 等)
172
+ - `Parameter` - 操作参数(查询、路径、头部、cookie)
173
+ - `RequestBody` - 带内容类型的请求体定义
174
+ - `Response` - 带状态码的响应定义
175
+ - `MediaType` - 带模式的媒体类型定义
176
+ - `Encoding` - 请求/响应体的序列化规则
177
+
178
+ #### 数据模式
179
+
180
+ - `Schema` - 基于 JSON Schema 的数据结构定义
181
+ - `Discriminator` - 带判别字段的多态支持
182
+ - `XML` - XML 序列化配置
183
+ - `Reference` - 可复用组件的 JSON 引用($ref)
184
+
185
+ #### 安全
186
+
187
+ - `SecurityScheme` - 认证方案定义
188
+ - `SecurityRequirement` - 操作所需的安全方案
189
+
190
+ ### 扩展和工具
191
+
192
+ #### 扩展支持
193
+
194
+ - `Extensible` - 支持扩展的对象基础接口
195
+ - `CommonExtensions` - 预定义扩展属性(x-internal、x-deprecated 等)
196
+
197
+ #### 类型工具
198
+
199
+ - `HTTPMethod` - 支持的 HTTP 方法('get'、'post'、'put'、'delete' 等)
200
+ - `ParameterLocation` - 参数位置('query'、'header'、'path'、'cookie')
201
+ - `SchemaType` - JSON Schema 原始类型('string'、'number'、'boolean' 等)
202
+
203
+ ### 高级用法
204
+
205
+ #### 模块化导入
206
+
207
+ 仅导入所需的类型以实现更好的树摇优化:
208
+
209
+ ```typescript
210
+ // 导入特定类型
211
+ import type { OpenAPI, Schema, Operation } from '@ahoo-wang/fetcher-openapi';
212
+
213
+ // 或从特定模块导入
214
+ import type { OpenAPI } from '@ahoo-wang/fetcher-openapi/openAPI';
215
+ import type { Schema } from '@ahoo-wang/fetcher-openapi/schema';
216
+ import type { Operation } from '@ahoo-wang/fetcher-openapi/paths';
217
+ ```
218
+
219
+ #### 类型安全 API 开发
220
+
221
+ 使用这些类型构建类型安全的 API 客户端和文档:
222
+
223
+ ```typescript
224
+ function validateOpenAPI(doc: OpenAPI): boolean {
225
+ // TypeScript 会在编译时捕获类型错误
226
+ return doc.openapi.startsWith('3.');
227
+ }
228
+
229
+ function createOperation(
230
+ path: string,
231
+ method: HTTPMethod,
232
+ config: Partial<Operation>,
233
+ ): Operation {
234
+ return {
235
+ operationId: `${method}${path.replace(/\//g, '')}`,
236
+ ...config,
237
+ };
238
+ }
239
+ ```
88
240
 
89
241
  ## 许可证
90
242
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher-openapi",
3
- "version": "2.1.1",
3
+ "version": "2.2.2",
4
4
  "description": "OpenAPI Specification TypeScript types for Fetcher - A modern, ultra-lightweight HTTP client for browsers and Node.js. Provides complete TypeScript support with type inference for OpenAPI 3.x schemas.",
5
5
  "keywords": [
6
6
  "fetch",