openapi-schema-type 1.0.0
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.
- package/README.md +246 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +32 -0
- package/typings/openapi_spec_3.1_schema.d.ts +1048 -0
- package/typings/openapi_spec_3.2_schema.d.ts +1112 -0
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# OpenAPI Schema Type
|
|
2
|
+
|
|
3
|
+
OpenAPI Schema 对象的 TypeScript 类型定义。
|
|
4
|
+
|
|
5
|
+
## 简介
|
|
6
|
+
|
|
7
|
+
本项目提供了 OpenAPI 3.2 和 3.1 规范中 Schema 对象的完整 TypeScript 类型定义。它基于 JSON Schema 2020-12 规范,特别针对 OpenAPI 的使用场景进行了优化,提供了类型安全的方式来定义和使用 OpenAPI Schema。
|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- ✅ 完整的 OpenAPI 3.2 和 3.1 Schema 对象支持
|
|
12
|
+
- ✅ 基于 JSON Schema 2020-12 规范
|
|
13
|
+
- ✅ 包含所有官方词汇表:
|
|
14
|
+
- **Core**: 核心标识和引用机制(`$id`, `$ref`, `$schema` 等)
|
|
15
|
+
- **Applicator**: 应用器,定义如何应用子模式(`properties`, `items`, `allOf` 等)
|
|
16
|
+
- **Unevaluated**: 处理未求值位置(`unevaluatedItems`, `unevaluatedProperties`)
|
|
17
|
+
- **Validation**: 验证约束(`type`, `minimum`, `maxLength` 等)
|
|
18
|
+
- **Meta-Data**: 元数据注解(`title`, `description`, `examples` 等)
|
|
19
|
+
- **Format**: 格式注解(`format`)
|
|
20
|
+
- **Content**: 内容编码(`contentEncoding`, `contentMediaType` 等)
|
|
21
|
+
- ✅ 排除布尔值模式以符合 OpenAPI 规范
|
|
22
|
+
- ✅ 支持 OpenAPI 特有的扩展字段(`x-*`)
|
|
23
|
+
- ✅ 支持引用对象(`ReferenceObject`)
|
|
24
|
+
- ✅ 完整的 TypeScript 类型提示和文档注释
|
|
25
|
+
|
|
26
|
+
## 安装
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install openapi-schema-type
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 使用方法
|
|
33
|
+
|
|
34
|
+
### 基本用法
|
|
35
|
+
|
|
36
|
+
安装包后,使用 ES6 模块语法导入需要的类型:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// 导入需要的类型
|
|
40
|
+
import { InfoObject, SchemaObject, ReferenceObject, ComponentsObject } from "openapi-schema-type";
|
|
41
|
+
|
|
42
|
+
// OpenAPI Schema 对象定义
|
|
43
|
+
const userSchema: SchemaObject = {
|
|
44
|
+
title: "User",
|
|
45
|
+
description: "用户信息模式",
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
name: {
|
|
49
|
+
type: "string",
|
|
50
|
+
minLength: 1,
|
|
51
|
+
maxLength: 100
|
|
52
|
+
},
|
|
53
|
+
age: {
|
|
54
|
+
type: "integer",
|
|
55
|
+
minimum: 0,
|
|
56
|
+
maximum: 150
|
|
57
|
+
},
|
|
58
|
+
email: {
|
|
59
|
+
type: "string",
|
|
60
|
+
format: "email"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
required: ["name", "email"],
|
|
64
|
+
additionalProperties: false
|
|
65
|
+
};
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 使用引用对象
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
// 使用 $ref 引用
|
|
72
|
+
import { SchemaObject, ReferenceObject } from "openapi-schema-type";
|
|
73
|
+
|
|
74
|
+
const addressSchema: SchemaObject | ReferenceObject = {
|
|
75
|
+
$ref: "#/components/schemas/Address"
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// 带有额外信息的引用对象
|
|
79
|
+
const detailedRef: ReferenceObject = {
|
|
80
|
+
$ref: "#/components/schemas/User",
|
|
81
|
+
summary: "用户引用",
|
|
82
|
+
description: "引用用户模式定义"
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 高级示例
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// 导入多个类型
|
|
90
|
+
import { SchemaObject, ComponentsObject, InfoObject } from "openapi-schema-type";
|
|
91
|
+
|
|
92
|
+
// 使用组合关键字
|
|
93
|
+
const polymorphicSchema: SchemaObject = {
|
|
94
|
+
oneOf: [
|
|
95
|
+
{ type: "string" },
|
|
96
|
+
{ type: "number" },
|
|
97
|
+
{
|
|
98
|
+
type: "object",
|
|
99
|
+
properties: {
|
|
100
|
+
value: { type: "string" }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// 使用扩展字段
|
|
107
|
+
const extendedSchema: SchemaObject = {
|
|
108
|
+
type: "object",
|
|
109
|
+
properties: {
|
|
110
|
+
name: { type: "string" },
|
|
111
|
+
"x-internal-id": {
|
|
112
|
+
type: "string",
|
|
113
|
+
description: "内部使用的标识符"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// 完整的组件定义示例
|
|
119
|
+
const components: ComponentsObject = {
|
|
120
|
+
schemas: {
|
|
121
|
+
User: {
|
|
122
|
+
type: "object",
|
|
123
|
+
properties: {
|
|
124
|
+
id: { type: "string", format: "uuid" },
|
|
125
|
+
name: { type: "string" },
|
|
126
|
+
email: { type: "string", format: "email" },
|
|
127
|
+
createdAt: { type: "string", format: "date-time" }
|
|
128
|
+
},
|
|
129
|
+
required: ["id", "name", "email"]
|
|
130
|
+
},
|
|
131
|
+
Address: {
|
|
132
|
+
type: "object",
|
|
133
|
+
properties: {
|
|
134
|
+
street: { type: "string" },
|
|
135
|
+
city: { type: "string" },
|
|
136
|
+
country: { type: "string" },
|
|
137
|
+
postalCode: { type: "string" }
|
|
138
|
+
},
|
|
139
|
+
required: ["street", "city", "country"]
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## API 文档
|
|
146
|
+
|
|
147
|
+
### 主要类型
|
|
148
|
+
|
|
149
|
+
包直接导出所有 OpenAPI 类型定义,无需使用命名空间:
|
|
150
|
+
|
|
151
|
+
#### `SchemaObject`
|
|
152
|
+
|
|
153
|
+
OpenAPI 规范中的 Schema 对象类型,基于 JSON Schema 2020-12,但排除了布尔值模式。
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { SchemaObject } from "openapi-schema-type";
|
|
157
|
+
|
|
158
|
+
type SchemaObject = Exclude<JSONSchemaMeta202012, boolean>;
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### `ReferenceObject`
|
|
162
|
+
|
|
163
|
+
用于引用其他组件的引用对象类型。
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { ReferenceObject } from "openapi-schema-type";
|
|
167
|
+
|
|
168
|
+
interface ReferenceObject {
|
|
169
|
+
$ref: string;
|
|
170
|
+
summary?: string;
|
|
171
|
+
description?: string;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### `SpecificationExtensionKey` 和 `SpecificationExtensions`
|
|
176
|
+
|
|
177
|
+
OpenAPI 扩展字段的类型定义。
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { SpecificationExtensionKey, SpecificationExtensions } from "openapi-schema-type";
|
|
181
|
+
|
|
182
|
+
type SpecificationExtensionKey = `x-${string}`;
|
|
183
|
+
type SpecificationExtensions = Record<SpecificationExtensionKey, unknown>;
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### 导出的类型
|
|
187
|
+
|
|
188
|
+
安装包后,可以通过 ES6 模块导入以下类型:
|
|
189
|
+
|
|
190
|
+
- `SchemaObject`: Schema 对象类型
|
|
191
|
+
- `ReferenceObject`: 引用对象类型
|
|
192
|
+
- `InfoObject`: Info 对象类型
|
|
193
|
+
- `ComponentsObject`: Components 对象类型
|
|
194
|
+
- `PathsObject`: Paths 对象类型
|
|
195
|
+
- `OperationObject`: Operation 对象类型
|
|
196
|
+
- `ParameterObject`: Parameter 对象类型
|
|
197
|
+
- `RequestBodyObject`: RequestBody 对象类型
|
|
198
|
+
- `ResponseObject`: Response 对象类型
|
|
199
|
+
- `ResponsesObject`: Responses 对象类型
|
|
200
|
+
- `MediaTypeObject`: MediaType 对象类型
|
|
201
|
+
- `EncodingObject`: Encoding 对象类型
|
|
202
|
+
- `ExampleObject`: Example 对象类型
|
|
203
|
+
- `LinkObject`: Link 对象类型
|
|
204
|
+
- `HeaderObject`: Header 对象类型
|
|
205
|
+
- `SecuritySchemeObject`: SecurityScheme 对象类型
|
|
206
|
+
- `SecurityRequirementObject`: SecurityRequirement 对象类型
|
|
207
|
+
- `TagObject`: Tag 对象类型
|
|
208
|
+
- `ExternalDocumentationObject`: ExternalDocumentation 对象类型
|
|
209
|
+
- `ServerObject`: Server 对象类型
|
|
210
|
+
- `ServerVariableObject`: ServerVariable 对象类型
|
|
211
|
+
- `CallbacksObject`: Callbacks 对象类型
|
|
212
|
+
- `SpecificationExtensions`: OpenAPI 扩展字段类型
|
|
213
|
+
- `MapOfString`: 字符串映射类型
|
|
214
|
+
- `OpenAPIDocument`: 完整的 OpenAPI 文档类型
|
|
215
|
+
|
|
216
|
+
## 版本支持
|
|
217
|
+
|
|
218
|
+
本项目支持以下 OpenAPI 规范版本:
|
|
219
|
+
|
|
220
|
+
- ✅ **OpenAPI 3.2** (基于 2025-09-17 的 schema)
|
|
221
|
+
- ✅ **OpenAPI 3.1** (基于 2025-09-15 的 schema)
|
|
222
|
+
|
|
223
|
+
## 兼容性
|
|
224
|
+
|
|
225
|
+
- TypeScript 4.5+
|
|
226
|
+
- Node.js 14+
|
|
227
|
+
|
|
228
|
+
## 相关资源
|
|
229
|
+
|
|
230
|
+
- [OpenAPI 官方网站](https://www.openapis.org/)
|
|
231
|
+
- [OpenAPI 3.2 规范](https://spec.openapis.org/oas/v3.2.0)
|
|
232
|
+
- [OpenAPI 3.1 规范](https://spec.openapis.org/oas/v3.1.0)
|
|
233
|
+
- [JSON Schema 官方网站](https://json-schema.org/)
|
|
234
|
+
- [JSON Schema 2020-12 规范](https://json-schema.org/draft/2020-12/)
|
|
235
|
+
|
|
236
|
+
## 许可证
|
|
237
|
+
|
|
238
|
+
ISC
|
|
239
|
+
|
|
240
|
+
## 贡献
|
|
241
|
+
|
|
242
|
+
欢迎提交 Issue 和 Pull Request!
|
|
243
|
+
|
|
244
|
+
## 仓库
|
|
245
|
+
|
|
246
|
+
[https://github.com/ww-k/openapi-schema-type](https://github.com/ww-k/openapi-schema-type)
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./typings/openapi_spec_3.2_schema.d";
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
throw new Error("No js, just typescript type");
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "openapi-schema-type",
|
|
3
|
+
"description": "OpenAPI Schema object type definition",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/ww-k/openapi-schema-type.git"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"import": "./index.js",
|
|
13
|
+
"require": "./index.js"
|
|
14
|
+
},
|
|
15
|
+
"./typings": "./typings/*.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"main": "./index.js",
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"files": [
|
|
21
|
+
"index.js",
|
|
22
|
+
"index.d.ts",
|
|
23
|
+
"typings"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
27
|
+
},
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"json-schema-meta-type": "^1.0.0"
|
|
31
|
+
}
|
|
32
|
+
}
|