@dazhicheng/openapi 1.0.0 → 1.1.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/CHANGELOG.md +7 -0
- package/dist/cli.d.ts +2 -0
- package/dist/index.d.ts +135 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/log.d.ts +2 -0
- package/dist/mockGenerator.d.ts +8 -0
- package/dist/openAPIParserMock/index.d.ts +7 -0
- package/dist/openAPIParserMock/primitives.d.ts +31 -0
- package/dist/openAPIParserMock/utils.d.ts +8 -0
- package/dist/serviceGenerator.d.ts +478 -0
- package/dist/util.d.ts +35 -0
- package/package.json +41 -4
- package/src/cli.ts +24 -104
- package/src/index.ts +251 -0
- package/src/log.ts +6 -0
- package/src/mockGenerator.ts +239 -0
- package/src/openAPIParserMock/index.ts +254 -0
- package/src/openAPIParserMock/primitives.ts +30 -0
- package/src/openAPIParserMock/utils.ts +54 -0
- package/src/serviceGenerator.ts +1065 -0
- package/src/typings.d.ts +6 -0
- package/src/util.ts +203 -0
- package/templates/interface.njk +54 -0
- package/templates/serviceController.njk +38 -0
- package/templates/serviceIndex.njk +14 -0
- package/tsconfig.json +2 -1
- package/openapi.config.ts +0 -16
- package/src/discovery.ts +0 -44
- package/src/fetcher.ts +0 -19
- package/src/formatter.ts +0 -16
- package/src/generator.ts +0 -187
- package/src/parser.ts +0 -336
- package/src/types.ts +0 -91
package/src/parser.ts
DELETED
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import type { OperationObject, ParameterObject, SchemaObject, SwaggerDoc } from "./types";
|
|
2
|
-
|
|
3
|
-
export interface ParsedInterface {
|
|
4
|
-
name: string;
|
|
5
|
-
description?: string;
|
|
6
|
-
properties: ParsedProperty[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface ParsedProperty {
|
|
10
|
-
name: string;
|
|
11
|
-
type: string;
|
|
12
|
-
required: boolean;
|
|
13
|
-
description?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ParsedAPI {
|
|
17
|
-
path: string;
|
|
18
|
-
method: string;
|
|
19
|
-
name: string;
|
|
20
|
-
summary?: string;
|
|
21
|
-
description?: string;
|
|
22
|
-
deprecated?: boolean;
|
|
23
|
-
tag?: string;
|
|
24
|
-
pathParams: ParsedProperty[];
|
|
25
|
-
queryParams: ParsedProperty[];
|
|
26
|
-
bodyType?: string;
|
|
27
|
-
bodyRequired?: boolean;
|
|
28
|
-
responseType: string;
|
|
29
|
-
hasFormData?: boolean;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface ParseResult {
|
|
33
|
-
interfaces: ParsedInterface[];
|
|
34
|
-
apis: ParsedAPI[];
|
|
35
|
-
tags: Array<{ name: string; description?: string }>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface ParseOptions {
|
|
39
|
-
/** 路径前缀,拼接到每个 API 路径前面 */
|
|
40
|
-
pathPrefix?: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function parseSwaggerDoc(doc: SwaggerDoc, options?: ParseOptions): ParseResult {
|
|
44
|
-
const isV3 = !!doc.openapi;
|
|
45
|
-
const definitions = isV3 ? doc.components?.schemas : doc.definitions;
|
|
46
|
-
const interfaces: ParsedInterface[] = [];
|
|
47
|
-
const apis: ParsedAPI[] = [];
|
|
48
|
-
const prefix = options?.pathPrefix ?? "";
|
|
49
|
-
|
|
50
|
-
if (definitions) {
|
|
51
|
-
for (const [rawName, schema] of Object.entries(definitions)) {
|
|
52
|
-
const name = sanitizeTypeName(rawName);
|
|
53
|
-
interfaces.push({
|
|
54
|
-
name,
|
|
55
|
-
description: schema.description || schema.title,
|
|
56
|
-
properties: parseSchemaProperties(schema, definitions),
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
for (const [path, pathItem] of Object.entries(doc.paths)) {
|
|
62
|
-
const methods = ["get", "post", "put", "delete", "patch"] as const;
|
|
63
|
-
|
|
64
|
-
for (const method of methods) {
|
|
65
|
-
const operation = pathItem[method] as OperationObject | undefined;
|
|
66
|
-
if (!operation) continue;
|
|
67
|
-
|
|
68
|
-
const fullPath = prefix ? `${prefix}${path}` : path;
|
|
69
|
-
const api = parseOperation(fullPath, method, operation, definitions || {}, isV3);
|
|
70
|
-
apis.push(api);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
interfaces,
|
|
76
|
-
apis,
|
|
77
|
-
tags: doc.tags || [],
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function parseOperation(
|
|
82
|
-
path: string,
|
|
83
|
-
method: string,
|
|
84
|
-
op: OperationObject,
|
|
85
|
-
definitions: Record<string, SchemaObject>,
|
|
86
|
-
isV3: boolean,
|
|
87
|
-
): ParsedAPI {
|
|
88
|
-
const pathParams: ParsedProperty[] = [];
|
|
89
|
-
const queryParams: ParsedProperty[] = [];
|
|
90
|
-
let bodyType: string | undefined;
|
|
91
|
-
let bodyRequired: boolean | undefined;
|
|
92
|
-
let hasFormData = false;
|
|
93
|
-
|
|
94
|
-
if (op.parameters) {
|
|
95
|
-
for (const param of op.parameters) {
|
|
96
|
-
const prop = paramToProperty(param, definitions);
|
|
97
|
-
switch (param.in) {
|
|
98
|
-
case "path":
|
|
99
|
-
pathParams.push(prop);
|
|
100
|
-
break;
|
|
101
|
-
case "query":
|
|
102
|
-
queryParams.push(prop);
|
|
103
|
-
break;
|
|
104
|
-
case "body":
|
|
105
|
-
bodyType = resolveSchemaType(param.schema, definitions);
|
|
106
|
-
bodyRequired = param.required;
|
|
107
|
-
break;
|
|
108
|
-
case "formData":
|
|
109
|
-
hasFormData = true;
|
|
110
|
-
queryParams.push(prop);
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (isV3 && op.requestBody) {
|
|
117
|
-
const content = op.requestBody.content;
|
|
118
|
-
const mediaType = content?.["application/json"] || content?.["multipart/form-data"];
|
|
119
|
-
if (mediaType?.schema) {
|
|
120
|
-
bodyType = resolveSchemaType(mediaType.schema, definitions);
|
|
121
|
-
bodyRequired = op.requestBody.required;
|
|
122
|
-
if (content?.["multipart/form-data"]) hasFormData = true;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const responseType = resolveResponseType(op, definitions, isV3);
|
|
127
|
-
|
|
128
|
-
const fnName = generateFunctionName(op.operationId, method, path);
|
|
129
|
-
|
|
130
|
-
return {
|
|
131
|
-
path,
|
|
132
|
-
method: method.toUpperCase(),
|
|
133
|
-
name: fnName,
|
|
134
|
-
summary: op.summary,
|
|
135
|
-
description: op.description,
|
|
136
|
-
deprecated: op.deprecated,
|
|
137
|
-
tag: op.tags?.[0],
|
|
138
|
-
pathParams,
|
|
139
|
-
queryParams,
|
|
140
|
-
bodyType,
|
|
141
|
-
bodyRequired,
|
|
142
|
-
responseType,
|
|
143
|
-
hasFormData,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function paramToProperty(param: ParameterObject, definitions: Record<string, SchemaObject>): ParsedProperty {
|
|
148
|
-
let type: string;
|
|
149
|
-
if (param.schema) {
|
|
150
|
-
type = resolveSchemaType(param.schema, definitions);
|
|
151
|
-
} else {
|
|
152
|
-
type = mapSwaggerType(param.type, param.format, param.items, definitions);
|
|
153
|
-
}
|
|
154
|
-
if (param.enum) {
|
|
155
|
-
type = param.enum.map(v => (typeof v === "string" ? `'${v}'` : v)).join(" | ");
|
|
156
|
-
}
|
|
157
|
-
return {
|
|
158
|
-
name: param.name,
|
|
159
|
-
type,
|
|
160
|
-
required: param.required ?? false,
|
|
161
|
-
description: param.description,
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function resolveResponseType(op: OperationObject, definitions: Record<string, SchemaObject>, isV3: boolean): string {
|
|
166
|
-
const resp = op.responses?.["200"] || op.responses?.["201"];
|
|
167
|
-
if (!resp) return "void";
|
|
168
|
-
|
|
169
|
-
if (isV3) {
|
|
170
|
-
const content = resp.content;
|
|
171
|
-
const mediaType = content?.["application/json"] || content?.["*/*"];
|
|
172
|
-
if (mediaType?.schema) {
|
|
173
|
-
return resolveSchemaType(mediaType.schema, definitions);
|
|
174
|
-
}
|
|
175
|
-
return "void";
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (resp.schema) {
|
|
179
|
-
return resolveSchemaType(resp.schema, definitions);
|
|
180
|
-
}
|
|
181
|
-
return "void";
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export function resolveSchemaType(schema: SchemaObject | undefined, definitions: Record<string, SchemaObject>): string {
|
|
185
|
-
if (!schema) return "any";
|
|
186
|
-
|
|
187
|
-
if (schema.$ref) {
|
|
188
|
-
return sanitizeTypeName(extractRefName(schema.$ref));
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (schema.originalRef) {
|
|
192
|
-
return sanitizeTypeName(schema.originalRef);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (schema.allOf) {
|
|
196
|
-
const types = schema.allOf.map(s => resolveSchemaType(s, definitions));
|
|
197
|
-
return types.join(" & ");
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (schema.oneOf || schema.anyOf) {
|
|
201
|
-
const arr = schema.oneOf || schema.anyOf!;
|
|
202
|
-
const types = arr.map(s => resolveSchemaType(s, definitions));
|
|
203
|
-
return types.join(" | ");
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (schema.enum) {
|
|
207
|
-
return schema.enum.map(v => (typeof v === "string" ? `'${v}'` : v)).join(" | ");
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return mapSwaggerType(schema.type, schema.format, schema.items, definitions);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function mapSwaggerType(
|
|
214
|
-
type?: string,
|
|
215
|
-
format?: string,
|
|
216
|
-
items?: SchemaObject,
|
|
217
|
-
definitions?: Record<string, SchemaObject>,
|
|
218
|
-
): string {
|
|
219
|
-
switch (type) {
|
|
220
|
-
case "integer":
|
|
221
|
-
case "number":
|
|
222
|
-
case "float":
|
|
223
|
-
case "double":
|
|
224
|
-
return "number";
|
|
225
|
-
case "string":
|
|
226
|
-
if (format === "binary") return "File";
|
|
227
|
-
return "string";
|
|
228
|
-
case "boolean":
|
|
229
|
-
return "boolean";
|
|
230
|
-
case "array":
|
|
231
|
-
if (items) {
|
|
232
|
-
const itemType = resolveSchemaType(items, definitions || {});
|
|
233
|
-
return `${itemType}[]`;
|
|
234
|
-
}
|
|
235
|
-
return "any[]";
|
|
236
|
-
case "object":
|
|
237
|
-
return "Record<string, any>";
|
|
238
|
-
case "file":
|
|
239
|
-
return "File";
|
|
240
|
-
default:
|
|
241
|
-
return "any";
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function parseSchemaProperties(schema: SchemaObject, definitions: Record<string, SchemaObject>): ParsedProperty[] {
|
|
246
|
-
const props: ParsedProperty[] = [];
|
|
247
|
-
const requiredSet = new Set(schema.required || []);
|
|
248
|
-
|
|
249
|
-
const allProperties: Record<string, SchemaObject> = {};
|
|
250
|
-
|
|
251
|
-
if (schema.allOf) {
|
|
252
|
-
for (const sub of schema.allOf) {
|
|
253
|
-
if (sub.$ref) {
|
|
254
|
-
const refName = extractRefName(sub.$ref);
|
|
255
|
-
const refSchema = definitions[refName];
|
|
256
|
-
if (refSchema?.properties) {
|
|
257
|
-
Object.assign(allProperties, refSchema.properties);
|
|
258
|
-
if (refSchema.required) {
|
|
259
|
-
refSchema.required.forEach(r => requiredSet.add(r));
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
if (sub.properties) {
|
|
264
|
-
Object.assign(allProperties, sub.properties);
|
|
265
|
-
if (sub.required) {
|
|
266
|
-
sub.required.forEach(r => requiredSet.add(r));
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (schema.properties) {
|
|
273
|
-
Object.assign(allProperties, schema.properties);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
for (const [name, propSchema] of Object.entries(allProperties)) {
|
|
277
|
-
let type = resolveSchemaType(propSchema, definitions);
|
|
278
|
-
if (propSchema.enum) {
|
|
279
|
-
type = propSchema.enum.map(v => (typeof v === "string" ? `'${v}'` : v)).join(" | ");
|
|
280
|
-
}
|
|
281
|
-
props.push({
|
|
282
|
-
name,
|
|
283
|
-
type,
|
|
284
|
-
required: requiredSet.has(name),
|
|
285
|
-
description: propSchema.description,
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return props;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function extractRefName(ref: string): string {
|
|
293
|
-
const parts = ref.split("/");
|
|
294
|
-
return parts[parts.length - 1]!;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/** 移除泛型符号等不合法 TS 标识符字符 */
|
|
298
|
-
export function sanitizeTypeName(name: string): string {
|
|
299
|
-
return name
|
|
300
|
-
.replace(/«/g, "_")
|
|
301
|
-
.replace(/»/g, "")
|
|
302
|
-
.replace(/[<>]/g, "_")
|
|
303
|
-
.replace(/[,\s]/g, "_")
|
|
304
|
-
.replace(/[^a-zA-Z0-9_$]/g, "")
|
|
305
|
-
.replace(/_+/g, "_")
|
|
306
|
-
.replace(/_$/, "");
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function generateFunctionName(operationId: string | undefined, method: string, path: string): string {
|
|
310
|
-
if (operationId) {
|
|
311
|
-
return sanitizeFnName(operationId);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const segments = path
|
|
315
|
-
.split("/")
|
|
316
|
-
.filter(Boolean)
|
|
317
|
-
.map(seg => {
|
|
318
|
-
if (seg.startsWith("{") && seg.endsWith("}")) {
|
|
319
|
-
return "By" + capitalize(seg.slice(1, -1));
|
|
320
|
-
}
|
|
321
|
-
return capitalize(seg);
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
return method.toLowerCase() + segments.join("");
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
function sanitizeFnName(name: string): string {
|
|
328
|
-
return name
|
|
329
|
-
.replace(/[^a-zA-Z0-9_$]/g, "_")
|
|
330
|
-
.replace(/^(\d)/, "_$1")
|
|
331
|
-
.replace(/Using(GET|POST|PUT|DELETE|PATCH)(_\d+)?$/, "");
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
function capitalize(str: string): string {
|
|
335
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
336
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
export interface ServiceConfig {
|
|
2
|
-
/** 服务名称,用于生成文件名 (如 'user' -> user.ts) */
|
|
3
|
-
name: string;
|
|
4
|
-
/** Swagger/OpenAPI 文档地址 */
|
|
5
|
-
url: string;
|
|
6
|
-
/** 服务描述 */
|
|
7
|
-
description?: string;
|
|
8
|
-
/** 路径前缀,会拼接到每个 API 路径前面(如 '/iam') */
|
|
9
|
-
pathPrefix?: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface OpenAPIConfig {
|
|
13
|
-
/** 生成文件输出目录 (相对于 packages/openapi) */
|
|
14
|
-
outputDir: string;
|
|
15
|
-
/** 服务列表(手动配置,与 gateway 二选一) */
|
|
16
|
-
services?: ServiceConfig[];
|
|
17
|
-
/** 网关地址,自动发现所有微服务文档(与 services 二选一) */
|
|
18
|
-
gateway?: string;
|
|
19
|
-
/** 请求函数的导入路径 */
|
|
20
|
-
requestImportPath: string;
|
|
21
|
-
/** 请求函数名称 */
|
|
22
|
-
requestFnName?: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// ---- Swagger / OpenAPI 文档解析相关类型 ----
|
|
26
|
-
|
|
27
|
-
export interface SwaggerDoc {
|
|
28
|
-
swagger?: string;
|
|
29
|
-
openapi?: string;
|
|
30
|
-
info?: { title?: string; version?: string };
|
|
31
|
-
basePath?: string;
|
|
32
|
-
paths: Record<string, PathItem>;
|
|
33
|
-
definitions?: Record<string, SchemaObject>;
|
|
34
|
-
components?: { schemas?: Record<string, SchemaObject> };
|
|
35
|
-
tags?: Array<{ name: string; description?: string }>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface PathItem {
|
|
39
|
-
[method: string]: OperationObject | undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface OperationObject {
|
|
43
|
-
tags?: string[];
|
|
44
|
-
summary?: string;
|
|
45
|
-
description?: string;
|
|
46
|
-
operationId?: string;
|
|
47
|
-
parameters?: ParameterObject[];
|
|
48
|
-
requestBody?: RequestBodyObject;
|
|
49
|
-
responses?: Record<string, ResponseObject>;
|
|
50
|
-
deprecated?: boolean;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface ParameterObject {
|
|
54
|
-
name: string;
|
|
55
|
-
in: "query" | "path" | "header" | "body" | "formData";
|
|
56
|
-
required?: boolean;
|
|
57
|
-
type?: string;
|
|
58
|
-
format?: string;
|
|
59
|
-
description?: string;
|
|
60
|
-
schema?: SchemaObject;
|
|
61
|
-
items?: SchemaObject;
|
|
62
|
-
enum?: Array<string | number>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export interface RequestBodyObject {
|
|
66
|
-
required?: boolean;
|
|
67
|
-
content?: Record<string, { schema?: SchemaObject }>;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface ResponseObject {
|
|
71
|
-
description?: string;
|
|
72
|
-
schema?: SchemaObject;
|
|
73
|
-
content?: Record<string, { schema?: SchemaObject }>;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface SchemaObject {
|
|
77
|
-
type?: string;
|
|
78
|
-
format?: string;
|
|
79
|
-
$ref?: string;
|
|
80
|
-
items?: SchemaObject;
|
|
81
|
-
properties?: Record<string, SchemaObject>;
|
|
82
|
-
required?: string[];
|
|
83
|
-
description?: string;
|
|
84
|
-
enum?: Array<string | number>;
|
|
85
|
-
allOf?: SchemaObject[];
|
|
86
|
-
oneOf?: SchemaObject[];
|
|
87
|
-
anyOf?: SchemaObject[];
|
|
88
|
-
title?: string;
|
|
89
|
-
additionalProperties?: boolean | SchemaObject;
|
|
90
|
-
originalRef?: string;
|
|
91
|
-
}
|