@gopowerteam/request-generate 0.3.2 → 0.3.3
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/dist/index.mjs +2 -7
- package/package.json +6 -9
- package/dist/chunk-C0xms8kb.cjs +0 -34
- package/dist/index.cjs +0 -1109
- package/dist/index.d.cts +0 -739
- package/dist/vite-plugin/index.cjs +0 -238
- package/dist/vite-plugin/index.d.cts +0 -13
package/dist/index.cjs
DELETED
|
@@ -1,1109 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_chunk = require('./chunk-C0xms8kb.cjs');
|
|
3
|
-
let node_fs = require("node:fs");
|
|
4
|
-
node_fs = require_chunk.__toESM(node_fs);
|
|
5
|
-
let node_path = require("node:path");
|
|
6
|
-
node_path = require_chunk.__toESM(node_path);
|
|
7
|
-
let node_process = require("node:process");
|
|
8
|
-
node_process = require_chunk.__toESM(node_process);
|
|
9
|
-
let chalk = require("chalk");
|
|
10
|
-
chalk = require_chunk.__toESM(chalk);
|
|
11
|
-
let ora = require("ora");
|
|
12
|
-
ora = require_chunk.__toESM(ora);
|
|
13
|
-
let handlebars = require("handlebars");
|
|
14
|
-
handlebars = require_chunk.__toESM(handlebars);
|
|
15
|
-
let _apidevtools_swagger_parser = require("@apidevtools/swagger-parser");
|
|
16
|
-
_apidevtools_swagger_parser = require_chunk.__toESM(_apidevtools_swagger_parser);
|
|
17
|
-
let node_crypto = require("node:crypto");
|
|
18
|
-
node_crypto = require_chunk.__toESM(node_crypto);
|
|
19
|
-
|
|
20
|
-
//#region src/utils/get-services-options.ts
|
|
21
|
-
/**
|
|
22
|
-
* 创建单服务配置项
|
|
23
|
-
* @param options
|
|
24
|
-
* @param name
|
|
25
|
-
* @param application
|
|
26
|
-
*/
|
|
27
|
-
function createOptions(options, name, application) {
|
|
28
|
-
const { service, openapi } = (() => {
|
|
29
|
-
if (!application) return {
|
|
30
|
-
service: "",
|
|
31
|
-
openapi: options.openapi
|
|
32
|
-
};
|
|
33
|
-
if (typeof application === "string") return {
|
|
34
|
-
service: application,
|
|
35
|
-
openapi: options.openapi
|
|
36
|
-
};
|
|
37
|
-
else return {
|
|
38
|
-
service: application.key,
|
|
39
|
-
openapi: application.openapi
|
|
40
|
-
};
|
|
41
|
-
})();
|
|
42
|
-
return {
|
|
43
|
-
name,
|
|
44
|
-
application: service,
|
|
45
|
-
input: `${options.gateway}/${service}/${openapi}`.replace(/\/{2,3}/g, "/"),
|
|
46
|
-
output: name ? node_path.default.join(options.output, name) : options.output,
|
|
47
|
-
exportModels: options.exportModels
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* 创建服务项
|
|
52
|
-
* @param options
|
|
53
|
-
* @returns GenerateApplicationOptions[]
|
|
54
|
-
*/
|
|
55
|
-
function generateServiceOptions(options) {
|
|
56
|
-
if (options.applications && Object.keys(options.applications).length) return Object.entries(options.applications).map(([name, application]) => createOptions(options, name, application));
|
|
57
|
-
else return [createOptions(options, "default")];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/download/index.ts
|
|
62
|
-
const DefaultDownloadDir = ".request";
|
|
63
|
-
var Download = class Download {
|
|
64
|
-
static options;
|
|
65
|
-
static async startup(options) {
|
|
66
|
-
const applicationOptions = generateServiceOptions(options);
|
|
67
|
-
if (!node_fs.existsSync(DefaultDownloadDir)) node_fs.mkdirSync(DefaultDownloadDir);
|
|
68
|
-
await Promise.all(applicationOptions.map((options) => Download.downloadOpenAPIFile(options)));
|
|
69
|
-
}
|
|
70
|
-
static async downloadOpenAPIFile(option) {
|
|
71
|
-
const data = await (await fetch(option.input)).json();
|
|
72
|
-
const filePath = node_path.join(".request", `${option.name}.json`);
|
|
73
|
-
node_fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
|
74
|
-
return data;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
//#endregion
|
|
79
|
-
//#region src/config/enum.config.ts
|
|
80
|
-
let OpenAPIVersion = /* @__PURE__ */ function(OpenAPIVersion) {
|
|
81
|
-
OpenAPIVersion[OpenAPIVersion["V2"] = 2] = "V2";
|
|
82
|
-
OpenAPIVersion[OpenAPIVersion["V3"] = 3] = "V3";
|
|
83
|
-
return OpenAPIVersion;
|
|
84
|
-
}({});
|
|
85
|
-
|
|
86
|
-
//#endregion
|
|
87
|
-
//#region src/utils/get-camel-name.ts
|
|
88
|
-
function getCamelName(value) {
|
|
89
|
-
return value.replace(/^[^a-z]+/gi, "").replace(/\W+/g, "_").replace(/[^\w-]+/g, "_").replace(/^\S/, (s) => s.toUpperCase()).replace(/_[a-z]/gi, (s) => s.toUpperCase()).replace(/_/g, "").trim();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
//#endregion
|
|
93
|
-
//#region src/entities/model.ts
|
|
94
|
-
var Model = class {
|
|
95
|
-
name;
|
|
96
|
-
fields;
|
|
97
|
-
imports;
|
|
98
|
-
constructor(name) {
|
|
99
|
-
this.name = name;
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
//#endregion
|
|
104
|
-
//#region src/entities/field.ts
|
|
105
|
-
var Field = class {
|
|
106
|
-
name;
|
|
107
|
-
required;
|
|
108
|
-
type;
|
|
109
|
-
ref;
|
|
110
|
-
enums;
|
|
111
|
-
description;
|
|
112
|
-
imports;
|
|
113
|
-
constructor(name, required) {
|
|
114
|
-
this.name = name;
|
|
115
|
-
this.required = required;
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/utils/get-built-in-type.ts
|
|
121
|
-
const TYPE_MAPPINGS$1 = new Map([["MapStringObject", "Record<string, any>"]]);
|
|
122
|
-
/**
|
|
123
|
-
* 获取对应类型
|
|
124
|
-
*/
|
|
125
|
-
function getBuiltInType(ref) {
|
|
126
|
-
return TYPE_MAPPINGS$1.get(ref);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
//#endregion
|
|
130
|
-
//#region src/utils/get-mapped-type.ts
|
|
131
|
-
const TYPE_MAPPINGS = new Map([
|
|
132
|
-
["file", "binary"],
|
|
133
|
-
["any", "any"],
|
|
134
|
-
["object", "any"],
|
|
135
|
-
["array", "any[]"],
|
|
136
|
-
["boolean", "boolean"],
|
|
137
|
-
["byte", "number"],
|
|
138
|
-
["int", "number"],
|
|
139
|
-
["integer", "number"],
|
|
140
|
-
["float", "number"],
|
|
141
|
-
["double", "number"],
|
|
142
|
-
["short", "number"],
|
|
143
|
-
["long", "number"],
|
|
144
|
-
["number", "number"],
|
|
145
|
-
["char", "string"],
|
|
146
|
-
["date", "string"],
|
|
147
|
-
["date-time", "string"],
|
|
148
|
-
["password", "string"],
|
|
149
|
-
["string", "string"],
|
|
150
|
-
["void", "void"],
|
|
151
|
-
["null", "null"]
|
|
152
|
-
]);
|
|
153
|
-
/**
|
|
154
|
-
* 获取对应类型
|
|
155
|
-
*/
|
|
156
|
-
function getMappedType(type = "object", format) {
|
|
157
|
-
if (format === "binary") return "binary";
|
|
158
|
-
return TYPE_MAPPINGS.get(type) || "any";
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
//#endregion
|
|
162
|
-
//#region src/parse/v2/strip-namespace.ts
|
|
163
|
-
/**
|
|
164
|
-
* Strip (OpenAPI) namespaces fom values.
|
|
165
|
-
* @param value
|
|
166
|
-
*/
|
|
167
|
-
function stripNamespace$1(value) {
|
|
168
|
-
return value.trim().replace(/^#\/definitions\//, "").replace(/^#\/parameters\//, "").replace(/^#\/responses\//, "").replace(/^#\/securityDefinitions\//, "");
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
//#endregion
|
|
172
|
-
//#region src/parse/v2/parse-schema-type.ts
|
|
173
|
-
function parseSchemaType$1(schema) {
|
|
174
|
-
if ("$ref" in schema && schema.$ref) {
|
|
175
|
-
const ref = getCamelName(stripNamespace$1(schema.$ref));
|
|
176
|
-
return {
|
|
177
|
-
type: "any",
|
|
178
|
-
ref,
|
|
179
|
-
imports: [ref]
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
if (!("$ref" in schema) && schema.type !== "array" && typeof schema.type === "string" && !schema.allOf && !schema.anyOf && !schema.oneOf) return {
|
|
183
|
-
type: getMappedType(schema.type || "any"),
|
|
184
|
-
ref: void 0,
|
|
185
|
-
enums: schema.enum
|
|
186
|
-
};
|
|
187
|
-
if (!("$ref" in schema) && schema.type === "array" && schema.items && "$ref" in schema.items && schema.items.$ref) {
|
|
188
|
-
const ref = getCamelName(stripNamespace$1(schema.items.$ref));
|
|
189
|
-
const type = getBuiltInType(ref);
|
|
190
|
-
return {
|
|
191
|
-
type: "any[]",
|
|
192
|
-
ref: `${type ?? ref}[]`,
|
|
193
|
-
imports: type ? void 0 : [ref]
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
if (!("$ref" in schema) && schema.type === "array" && schema.items && !("$ref" in schema.items)) return {
|
|
197
|
-
type: `${getMappedType(schema.items.type)}[]`,
|
|
198
|
-
ref: void 0
|
|
199
|
-
};
|
|
200
|
-
if (!("$ref" in schema) && schema.type === "object") return {
|
|
201
|
-
type: getMappedType(schema.type),
|
|
202
|
-
ref: "any"
|
|
203
|
-
};
|
|
204
|
-
throw new Error("无法解析相应的schema");
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
//#endregion
|
|
208
|
-
//#region src/parse/v2/parse-field.ts
|
|
209
|
-
function parseField$1(name, properties, required = false) {
|
|
210
|
-
const field = new Field(name, required);
|
|
211
|
-
const { type, ref, imports, enums } = parseSchemaType$1(properties);
|
|
212
|
-
field.type = type;
|
|
213
|
-
field.ref = ref;
|
|
214
|
-
field.imports = imports;
|
|
215
|
-
field.enums = enums;
|
|
216
|
-
if (!("$ref" in properties)) field.description = properties.description;
|
|
217
|
-
return field;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
//#endregion
|
|
221
|
-
//#region src/parse/v2/parse-fields.ts
|
|
222
|
-
function parseFields$1(schema) {
|
|
223
|
-
return Object.entries(schema.properties || {}).map(([name, property]) => {
|
|
224
|
-
var _schema$required;
|
|
225
|
-
return parseField$1(name, property, (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(name));
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
//#endregion
|
|
230
|
-
//#region src/parse/v2/parse-model.ts
|
|
231
|
-
function parseModel$1(name, definition) {
|
|
232
|
-
const model = new Model(name);
|
|
233
|
-
model.fields = parseFields$1(definition);
|
|
234
|
-
const imports = model.fields.filter((field) => field.imports).reduce((r, m) => [...r, ...m.imports || []], []).filter((m) => m !== name);
|
|
235
|
-
model.imports = Array.from(new Set(imports));
|
|
236
|
-
return model;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
//#endregion
|
|
240
|
-
//#region src/parse/v2/parse-models.ts
|
|
241
|
-
function parseModels$1(document) {
|
|
242
|
-
const models = [];
|
|
243
|
-
if (document.definitions) {
|
|
244
|
-
for (const definitionName in document.definitions) if (Object.getOwnPropertyNames(document.definitions).includes(definitionName)) {
|
|
245
|
-
const definition = document.definitions[definitionName];
|
|
246
|
-
const model = parseModel$1(getCamelName(definitionName), definition);
|
|
247
|
-
models.push(model);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return models;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
//#endregion
|
|
254
|
-
//#region src/entities/service.ts
|
|
255
|
-
var Service = class {
|
|
256
|
-
constructor(name) {
|
|
257
|
-
var _Generate$options;
|
|
258
|
-
this.name = name;
|
|
259
|
-
this.responseType = ((_Generate$options = Generate.options) === null || _Generate$options === void 0 || (_Generate$options = _Generate$options.exportServices) === null || _Generate$options === void 0 ? void 0 : _Generate$options.responseType) || "promise";
|
|
260
|
-
}
|
|
261
|
-
name;
|
|
262
|
-
application;
|
|
263
|
-
imports = [];
|
|
264
|
-
operations = [];
|
|
265
|
-
responseType;
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
//#endregion
|
|
269
|
-
//#region src/utils/get-service-name.ts
|
|
270
|
-
/**
|
|
271
|
-
* 获取服务名称
|
|
272
|
-
* @param path
|
|
273
|
-
* @param method
|
|
274
|
-
* @param operationObject
|
|
275
|
-
* @param tags
|
|
276
|
-
* @returns 服务名称
|
|
277
|
-
*/
|
|
278
|
-
function getServiceName(path, method, operationObject, tags) {
|
|
279
|
-
var _Generate$options;
|
|
280
|
-
const resolve = (_Generate$options = Generate.options) === null || _Generate$options === void 0 || (_Generate$options = _Generate$options.exportServices) === null || _Generate$options === void 0 ? void 0 : _Generate$options.serviceResolve;
|
|
281
|
-
if (resolve) return resolve({
|
|
282
|
-
path,
|
|
283
|
-
method,
|
|
284
|
-
object: operationObject,
|
|
285
|
-
tags
|
|
286
|
-
});
|
|
287
|
-
else {
|
|
288
|
-
var _operationObject$tags;
|
|
289
|
-
return ((_operationObject$tags = operationObject.tags) === null || _operationObject$tags === void 0 ? void 0 : _operationObject$tags.map((tag) => getCamelName(tag))) || "Default";
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
//#endregion
|
|
294
|
-
//#region src/entities/operation.ts
|
|
295
|
-
var Operation = class {
|
|
296
|
-
name;
|
|
297
|
-
method;
|
|
298
|
-
path;
|
|
299
|
-
description;
|
|
300
|
-
responseRef;
|
|
301
|
-
responseType;
|
|
302
|
-
parametersPath = [];
|
|
303
|
-
parametersQuery = [];
|
|
304
|
-
parametersBody;
|
|
305
|
-
imports = [];
|
|
306
|
-
constructor(name, method, path) {
|
|
307
|
-
var _Generate$options;
|
|
308
|
-
this.name = name;
|
|
309
|
-
this.method = method;
|
|
310
|
-
this.path = path;
|
|
311
|
-
this.responseType = ((_Generate$options = Generate.options) === null || _Generate$options === void 0 || (_Generate$options = _Generate$options.exportServices) === null || _Generate$options === void 0 ? void 0 : _Generate$options.responseType) || "promise";
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
//#endregion
|
|
316
|
-
//#region src/utils/get-operation-name.ts
|
|
317
|
-
/**
|
|
318
|
-
* 获取服务名称
|
|
319
|
-
* @param path
|
|
320
|
-
* @param method
|
|
321
|
-
* @param operationObject
|
|
322
|
-
* @returns string
|
|
323
|
-
*/
|
|
324
|
-
function getOperationName(path, method, operationObject) {
|
|
325
|
-
var _Generate$options;
|
|
326
|
-
const resolve = (_Generate$options = Generate.options) === null || _Generate$options === void 0 || (_Generate$options = _Generate$options.exportServices) === null || _Generate$options === void 0 ? void 0 : _Generate$options.operationResolve;
|
|
327
|
-
if (resolve) return resolve({
|
|
328
|
-
path,
|
|
329
|
-
method,
|
|
330
|
-
object: operationObject
|
|
331
|
-
});
|
|
332
|
-
else return operationObject.operationId || "";
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
//#endregion
|
|
336
|
-
//#region src/entities/operation-parameter.ts
|
|
337
|
-
var OperationParameter = class {
|
|
338
|
-
in;
|
|
339
|
-
name;
|
|
340
|
-
type;
|
|
341
|
-
ref;
|
|
342
|
-
enums;
|
|
343
|
-
description;
|
|
344
|
-
required;
|
|
345
|
-
imports = [];
|
|
346
|
-
};
|
|
347
|
-
|
|
348
|
-
//#endregion
|
|
349
|
-
//#region src/parse/v2/parse-parameters-body.ts
|
|
350
|
-
function parseParametersBody$1(parameters) {
|
|
351
|
-
const requestBody = parameters.find((parameter) => !("$ref" in parameter) && parameter.in === "body");
|
|
352
|
-
if (requestBody && "schema" in requestBody) {
|
|
353
|
-
const { type, ref, imports } = parseSchemaType$1(requestBody.schema);
|
|
354
|
-
const parameter = new OperationParameter();
|
|
355
|
-
parameter.name = "requestBody";
|
|
356
|
-
parameter.in = "body";
|
|
357
|
-
parameter.type = type;
|
|
358
|
-
parameter.ref = ref;
|
|
359
|
-
parameter.imports = imports || [];
|
|
360
|
-
return parameter;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
//#endregion
|
|
365
|
-
//#region src/parse/v2/parse-parameters-path.ts
|
|
366
|
-
function parseParametersPath$1(parameters) {
|
|
367
|
-
return parameters.reduce((r, p) => {
|
|
368
|
-
if (!("$ref" in p) && p.in === "path") {
|
|
369
|
-
const { type, ref, imports, enums } = parseSchemaType$1(p);
|
|
370
|
-
const parameter = new OperationParameter();
|
|
371
|
-
parameter.name = p.name;
|
|
372
|
-
parameter.description = p.description;
|
|
373
|
-
parameter.in = "path";
|
|
374
|
-
parameter.type = type;
|
|
375
|
-
parameter.ref = ref;
|
|
376
|
-
parameter.imports = imports || [];
|
|
377
|
-
parameter.enums = enums;
|
|
378
|
-
r.push(parameter);
|
|
379
|
-
}
|
|
380
|
-
return r;
|
|
381
|
-
}, []);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
//#endregion
|
|
385
|
-
//#region src/parse/v2/parse-parameters-query.ts
|
|
386
|
-
function parseParametersQuery$1(parameters) {
|
|
387
|
-
var _Generate$options;
|
|
388
|
-
const excludeParams = (_Generate$options = Generate.options) === null || _Generate$options === void 0 || (_Generate$options = _Generate$options.exportServices) === null || _Generate$options === void 0 ? void 0 : _Generate$options.excludeQueryParams;
|
|
389
|
-
return parameters.reduce((r, p) => {
|
|
390
|
-
if (!("$ref" in p) && p.in === "query" && !(excludeParams && excludeParams.includes(p.name.split(".")[0]))) {
|
|
391
|
-
const { type, ref, imports, enums } = parseSchemaType$1(p);
|
|
392
|
-
const parameter = new OperationParameter();
|
|
393
|
-
parameter.name = p.name;
|
|
394
|
-
parameter.description = p.description;
|
|
395
|
-
parameter.in = "query";
|
|
396
|
-
parameter.type = type;
|
|
397
|
-
parameter.ref = ref;
|
|
398
|
-
parameter.required = p.required;
|
|
399
|
-
parameter.imports = imports || [];
|
|
400
|
-
parameter.enums = enums;
|
|
401
|
-
if (p.name.includes(".") && !p.name.startsWith(".") && !p.name.endsWith(".")) parameter.name = `"${p.name}"`;
|
|
402
|
-
r.push(parameter);
|
|
403
|
-
}
|
|
404
|
-
return r;
|
|
405
|
-
}, []);
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
//#endregion
|
|
409
|
-
//#region src/parse/v2/parse-operation.ts
|
|
410
|
-
function parseOperation$1(path, method, operationObject) {
|
|
411
|
-
var _operation$parameters;
|
|
412
|
-
const operation = new Operation(getOperationName(path, method, operationObject), method, path);
|
|
413
|
-
operation.description = operationObject.summary || operation.description;
|
|
414
|
-
if (operationObject.parameters) {
|
|
415
|
-
operation.parametersBody = parseParametersBody$1(operationObject.parameters);
|
|
416
|
-
operation.parametersPath = parseParametersPath$1(operationObject.parameters);
|
|
417
|
-
operation.parametersQuery = parseParametersQuery$1(operationObject.parameters);
|
|
418
|
-
}
|
|
419
|
-
const responseSchema = parseResponseType$1(operationObject.responses);
|
|
420
|
-
operation.imports = Array.from(new Set([
|
|
421
|
-
...((_operation$parameters = operation.parametersBody) === null || _operation$parameters === void 0 ? void 0 : _operation$parameters.imports) || [],
|
|
422
|
-
...operation.parametersPath.flatMap((p) => p.imports) || [],
|
|
423
|
-
...operation.parametersQuery.flatMap((p) => p.imports) || [],
|
|
424
|
-
...(responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.imports) || []
|
|
425
|
-
]));
|
|
426
|
-
operation.responseRef = (responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.ref) || "void";
|
|
427
|
-
return operation;
|
|
428
|
-
}
|
|
429
|
-
function parseResponseType$1(responses) {
|
|
430
|
-
const response = responses === null || responses === void 0 ? void 0 : responses["200"];
|
|
431
|
-
if (response && "$ref" in response) return parseSchemaType$1(response);
|
|
432
|
-
if (response && "schema" in response && (response === null || response === void 0 ? void 0 : response.schema)) return parseSchemaType$1(response === null || response === void 0 ? void 0 : response.schema);
|
|
433
|
-
return {
|
|
434
|
-
type: "void",
|
|
435
|
-
ref: "void",
|
|
436
|
-
imports: []
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
//#endregion
|
|
441
|
-
//#region src/parse/v2/parse-service.ts
|
|
442
|
-
function parseService$1(path, method, operationObject, tags, services) {
|
|
443
|
-
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
444
|
-
const names = toNames(getServiceName(path, method, operationObject, tags || []));
|
|
445
|
-
const operation = parseOperation$1(path, method, operationObject);
|
|
446
|
-
names.forEach((name) => {
|
|
447
|
-
let service = services.find((service) => service.name === name);
|
|
448
|
-
if (!service) {
|
|
449
|
-
service = new Service(name);
|
|
450
|
-
services.push(service);
|
|
451
|
-
}
|
|
452
|
-
if (service) service.operations.push(operation);
|
|
453
|
-
service.operations.flatMap((operation) => operation.imports).forEach((model) => {
|
|
454
|
-
if (service && !service.imports.includes(model)) service.imports.push(model);
|
|
455
|
-
});
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
//#endregion
|
|
460
|
-
//#region src/parse/v2/parse-services.ts
|
|
461
|
-
function parseServices$1(document) {
|
|
462
|
-
const services = [];
|
|
463
|
-
Object.entries(document.paths).forEach(([path, pathObject]) => {
|
|
464
|
-
if (pathObject) Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
465
|
-
parseService$1(path, method, operationObject, document.tags || [], services);
|
|
466
|
-
});
|
|
467
|
-
});
|
|
468
|
-
return services;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
//#endregion
|
|
472
|
-
//#region src/parse/v2/index.ts
|
|
473
|
-
function parseV2(document) {
|
|
474
|
-
return {
|
|
475
|
-
models: parseModels$1(document),
|
|
476
|
-
services: parseServices$1(document)
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
//#endregion
|
|
481
|
-
//#region src/parse/v3/strip-namespace.ts
|
|
482
|
-
/**
|
|
483
|
-
* Strip (OpenAPI) namespaces fom values.
|
|
484
|
-
* @param value
|
|
485
|
-
*/
|
|
486
|
-
function stripNamespace(value) {
|
|
487
|
-
return value.trim().replace(/^#\/components\/schemas\//, "").replace(/^#\/components\/responses\//, "").replace(/^#\/components\/parameters\//, "").replace(/^#\/components\/examples\//, "").replace(/^#\/components\/requestBodies\//, "").replace(/^#\/components\/headers\//, "").replace(/^#\/components\/securitySchemes\//, "").replace(/^#\/components\/links\//, "").replace(/^#\/components\/callbacks\//, "");
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
//#endregion
|
|
491
|
-
//#region src/parse/v3/parse-schema-type.ts
|
|
492
|
-
function parseSchemaType(schema) {
|
|
493
|
-
if ("$ref" in schema) {
|
|
494
|
-
const ref = getCamelName(stripNamespace(schema.$ref));
|
|
495
|
-
return {
|
|
496
|
-
type: "any",
|
|
497
|
-
ref,
|
|
498
|
-
imports: [ref]
|
|
499
|
-
};
|
|
500
|
-
}
|
|
501
|
-
if (!("$ref" in schema) && schema.type !== "array" && !schema.allOf && !schema.anyOf && !schema.oneOf) return {
|
|
502
|
-
type: getMappedType(schema.type || "any"),
|
|
503
|
-
ref: void 0,
|
|
504
|
-
enums: schema.enum
|
|
505
|
-
};
|
|
506
|
-
if (schema.type === "array" && "$ref" in schema.items) {
|
|
507
|
-
const ref = getCamelName(stripNamespace(schema.items.$ref));
|
|
508
|
-
const type = getBuiltInType(ref);
|
|
509
|
-
return {
|
|
510
|
-
type: "any[]",
|
|
511
|
-
ref: `${type ?? ref}[]`,
|
|
512
|
-
imports: type ? void 0 : [ref]
|
|
513
|
-
};
|
|
514
|
-
}
|
|
515
|
-
if (schema.type === "array" && !("$ref" in schema.items)) return {
|
|
516
|
-
type: `${getMappedType(schema.items.type)}[]`,
|
|
517
|
-
ref: void 0
|
|
518
|
-
};
|
|
519
|
-
if (schema.allOf || schema.anyOf || schema.oneOf) {
|
|
520
|
-
const ofSchema = schema.allOf || schema.anyOf || schema.oneOf;
|
|
521
|
-
const ofSchemaArray = ofSchema === null || ofSchema === void 0 ? void 0 : ofSchema.map((s) => parseSchemaType(s));
|
|
522
|
-
if (ofSchemaArray) {
|
|
523
|
-
const hasRef = ofSchemaArray.some((s) => s.ref);
|
|
524
|
-
return {
|
|
525
|
-
type: hasRef ? "any" : ofSchemaArray.map((s) => s.type).join("|"),
|
|
526
|
-
ref: hasRef ? ofSchemaArray.map((s) => s.ref && getCamelName(s.ref) || s.type).join("|") : void 0,
|
|
527
|
-
imports: hasRef ? ofSchemaArray.reduce((r, s) => (s.ref && !r.includes(s.ref) && r.push(getCamelName(s.ref)), r), []) : void 0
|
|
528
|
-
};
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
throw new Error("无法解析相应的schema");
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
//#endregion
|
|
535
|
-
//#region src/parse/v3/parse-field.ts
|
|
536
|
-
function parseField(name, properties, required = false) {
|
|
537
|
-
const field = new Field(name, required);
|
|
538
|
-
const { type, ref, imports, enums } = parseSchemaType(properties);
|
|
539
|
-
field.type = type;
|
|
540
|
-
field.ref = ref;
|
|
541
|
-
field.imports = imports;
|
|
542
|
-
field.enums = enums;
|
|
543
|
-
if (!("$ref" in properties)) field.description = properties.description;
|
|
544
|
-
return field;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
//#endregion
|
|
548
|
-
//#region src/parse/v3/parse-fields.ts
|
|
549
|
-
function parseFields(schema) {
|
|
550
|
-
return Object.entries(schema.properties || {}).map(([name, property]) => {
|
|
551
|
-
var _schema$required;
|
|
552
|
-
return parseField(name, property, (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(name));
|
|
553
|
-
});
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
//#endregion
|
|
557
|
-
//#region src/parse/v3/parse-model.ts
|
|
558
|
-
function parseModel(name, definition) {
|
|
559
|
-
const model = new Model(name);
|
|
560
|
-
model.fields = parseFields(definition);
|
|
561
|
-
const imports = model.fields.filter((field) => field.imports).reduce((r, m) => [...r, ...m.imports || []], []).filter((m) => m !== name);
|
|
562
|
-
model.imports = Array.from(new Set(imports));
|
|
563
|
-
return model;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
//#endregion
|
|
567
|
-
//#region src/parse/v3/parse-models.ts
|
|
568
|
-
function parseModels(document) {
|
|
569
|
-
const models = [];
|
|
570
|
-
if (document.components) for (const definitionName in document.components.schemas) {
|
|
571
|
-
const { schemas } = document.components;
|
|
572
|
-
if (Object.getOwnPropertyNames(schemas).includes(definitionName)) {
|
|
573
|
-
const definition = document.components.schemas[definitionName];
|
|
574
|
-
const model = parseModel(getCamelName(definitionName), definition);
|
|
575
|
-
models.push(model);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
return models;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
//#endregion
|
|
582
|
-
//#region src/parse/v3/parse-parameters-body.ts
|
|
583
|
-
function parseParametersBody(requestBody) {
|
|
584
|
-
const { type, ref, imports } = parseBodyType(requestBody);
|
|
585
|
-
const parameter = new OperationParameter();
|
|
586
|
-
parameter.name = "requestBody";
|
|
587
|
-
parameter.in = "body";
|
|
588
|
-
parameter.type = type;
|
|
589
|
-
parameter.ref = ref;
|
|
590
|
-
parameter.imports = imports || [];
|
|
591
|
-
return parameter;
|
|
592
|
-
}
|
|
593
|
-
function parseBodyType(requestBody) {
|
|
594
|
-
var _requestBody$content$;
|
|
595
|
-
if ("$ref" in requestBody) return parseSchemaType(requestBody);
|
|
596
|
-
if ("content" in requestBody && (requestBody === null || requestBody === void 0 || (_requestBody$content$ = requestBody.content["application/json"]) === null || _requestBody$content$ === void 0 ? void 0 : _requestBody$content$.schema)) {
|
|
597
|
-
var _requestBody$content$2;
|
|
598
|
-
return parseSchemaType(requestBody === null || requestBody === void 0 || (_requestBody$content$2 = requestBody.content["application/json"]) === null || _requestBody$content$2 === void 0 ? void 0 : _requestBody$content$2.schema);
|
|
599
|
-
}
|
|
600
|
-
throw new Error("无法解析RequestBody Schema");
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
//#endregion
|
|
604
|
-
//#region src/parse/v3/parse-parameters-path.ts
|
|
605
|
-
function parseParametersPath(parameters) {
|
|
606
|
-
return parameters.reduce((r, p) => {
|
|
607
|
-
if (!("$ref" in p) && p.in === "path" && p.schema) {
|
|
608
|
-
const { type, ref, imports, enums } = parseSchemaType(p.schema);
|
|
609
|
-
const parameter = new OperationParameter();
|
|
610
|
-
parameter.name = p.name;
|
|
611
|
-
parameter.description = p.description;
|
|
612
|
-
parameter.in = "path";
|
|
613
|
-
parameter.type = type;
|
|
614
|
-
parameter.ref = ref;
|
|
615
|
-
parameter.imports = imports || [];
|
|
616
|
-
parameter.enums = enums;
|
|
617
|
-
r.push(parameter);
|
|
618
|
-
}
|
|
619
|
-
return r;
|
|
620
|
-
}, []);
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
//#endregion
|
|
624
|
-
//#region src/parse/v3/parse-parameters-query.ts
|
|
625
|
-
function parseParametersQuery(parameters) {
|
|
626
|
-
var _Generate$options;
|
|
627
|
-
const excludeParams = (_Generate$options = Generate.options) === null || _Generate$options === void 0 || (_Generate$options = _Generate$options.exportServices) === null || _Generate$options === void 0 ? void 0 : _Generate$options.excludeQueryParams;
|
|
628
|
-
return parameters.reduce((r, p) => {
|
|
629
|
-
if (!("$ref" in p) && p.in === "query" && p.schema && !(excludeParams && excludeParams.includes(p.name))) {
|
|
630
|
-
const { type, ref, imports, enums } = parseSchemaType(p.schema);
|
|
631
|
-
const parameter = new OperationParameter();
|
|
632
|
-
parameter.name = p.name;
|
|
633
|
-
parameter.description = p.description;
|
|
634
|
-
parameter.in = "query";
|
|
635
|
-
parameter.type = type;
|
|
636
|
-
parameter.ref = ref;
|
|
637
|
-
parameter.required = p.required;
|
|
638
|
-
parameter.imports = imports || [];
|
|
639
|
-
parameter.enums = enums;
|
|
640
|
-
if (p.name.includes(".") && !p.name.startsWith(".") && !p.name.endsWith(".")) parameter.name = `"${p.name}"`;
|
|
641
|
-
r.push(parameter);
|
|
642
|
-
}
|
|
643
|
-
return r;
|
|
644
|
-
}, []);
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
//#endregion
|
|
648
|
-
//#region src/parse/v3/parse-operation.ts
|
|
649
|
-
function parseOperation(path, method, operationObject) {
|
|
650
|
-
var _operation$parameters;
|
|
651
|
-
const operation = new Operation(getOperationName(path, method, operationObject), method, path);
|
|
652
|
-
operation.description = operationObject.summary;
|
|
653
|
-
if (operationObject.requestBody) operation.parametersBody = parseParametersBody(operationObject.requestBody);
|
|
654
|
-
if (operationObject.parameters) {
|
|
655
|
-
operation.parametersPath = parseParametersPath(operationObject.parameters);
|
|
656
|
-
operation.parametersQuery = parseParametersQuery(operationObject.parameters);
|
|
657
|
-
}
|
|
658
|
-
const responseSchema = parseResponseType(operationObject.responses);
|
|
659
|
-
operation.imports = Array.from(new Set([
|
|
660
|
-
...((_operation$parameters = operation.parametersBody) === null || _operation$parameters === void 0 ? void 0 : _operation$parameters.imports) || [],
|
|
661
|
-
...operation.parametersPath.flatMap((p) => p.imports) || [],
|
|
662
|
-
...operation.parametersQuery.flatMap((p) => p.imports) || [],
|
|
663
|
-
...(responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.imports) || []
|
|
664
|
-
]));
|
|
665
|
-
operation.responseRef = (responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.ref) || (responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.type) || "void";
|
|
666
|
-
return operation;
|
|
667
|
-
}
|
|
668
|
-
function parseResponseType(responses) {
|
|
669
|
-
const response = responses === null || responses === void 0 ? void 0 : responses["200"];
|
|
670
|
-
const medias = ["*/*", "application/json"];
|
|
671
|
-
if (response && "$ref" in response) return parseSchemaType(responses);
|
|
672
|
-
if (response && "content" in response && medias.some((media) => {
|
|
673
|
-
var _response$content;
|
|
674
|
-
return !!(response === null || response === void 0 || (_response$content = response.content) === null || _response$content === void 0 || (_response$content = _response$content[media]) === null || _response$content === void 0 ? void 0 : _response$content.schema);
|
|
675
|
-
})) {
|
|
676
|
-
var _response$content3;
|
|
677
|
-
const mediaType = medias.find((media) => {
|
|
678
|
-
var _response$content2;
|
|
679
|
-
return !!(response === null || response === void 0 || (_response$content2 = response.content) === null || _response$content2 === void 0 || (_response$content2 = _response$content2[media]) === null || _response$content2 === void 0 ? void 0 : _response$content2.schema);
|
|
680
|
-
});
|
|
681
|
-
return parseSchemaType(response === null || response === void 0 || (_response$content3 = response.content) === null || _response$content3 === void 0 || (_response$content3 = _response$content3[mediaType]) === null || _response$content3 === void 0 ? void 0 : _response$content3.schema);
|
|
682
|
-
}
|
|
683
|
-
return {
|
|
684
|
-
type: "void",
|
|
685
|
-
ref: "void",
|
|
686
|
-
imports: []
|
|
687
|
-
};
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
//#endregion
|
|
691
|
-
//#region src/parse/v3/parse-service.ts
|
|
692
|
-
function parseService(path, method, operationObject, tags, services) {
|
|
693
|
-
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
694
|
-
const names = toNames(getServiceName(path, method, operationObject, tags || []));
|
|
695
|
-
const operation = parseOperation(path, method, operationObject);
|
|
696
|
-
names.forEach((name) => {
|
|
697
|
-
let service = services.find((service) => service.name === name);
|
|
698
|
-
if (!service) {
|
|
699
|
-
service = new Service(name);
|
|
700
|
-
services.push(service);
|
|
701
|
-
}
|
|
702
|
-
if (service) service.operations.push(operation);
|
|
703
|
-
service.operations.flatMap((operation) => operation.imports).forEach((model) => {
|
|
704
|
-
if (service && !service.imports.includes(model)) service.imports.push(model);
|
|
705
|
-
});
|
|
706
|
-
});
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
//#endregion
|
|
710
|
-
//#region src/parse/v3/parse-services.ts
|
|
711
|
-
function parseServices(document) {
|
|
712
|
-
const services = [];
|
|
713
|
-
Object.entries(document.paths).forEach(([path, pathObject]) => {
|
|
714
|
-
if (pathObject) Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
715
|
-
parseService(path, method, operationObject, document.tags || [], services);
|
|
716
|
-
});
|
|
717
|
-
});
|
|
718
|
-
return services;
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
//#endregion
|
|
722
|
-
//#region src/parse/v3/index.ts
|
|
723
|
-
function parseV3(document) {
|
|
724
|
-
return {
|
|
725
|
-
models: parseModels(document),
|
|
726
|
-
services: parseServices(document)
|
|
727
|
-
};
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
//#endregion
|
|
731
|
-
//#region src/progress.ts
|
|
732
|
-
const progressMaps = /* @__PURE__ */ new Map();
|
|
733
|
-
function createProgress(name) {
|
|
734
|
-
const spinner = (0, ora.default)(formatProgressText(name)).start();
|
|
735
|
-
progressMaps.set(name, {
|
|
736
|
-
spinner,
|
|
737
|
-
model: {
|
|
738
|
-
total: 0,
|
|
739
|
-
value: 0
|
|
740
|
-
},
|
|
741
|
-
service: {
|
|
742
|
-
total: 0,
|
|
743
|
-
value: 0
|
|
744
|
-
}
|
|
745
|
-
});
|
|
746
|
-
}
|
|
747
|
-
function startProgress(name, options) {
|
|
748
|
-
const progress = progressMaps.get(name);
|
|
749
|
-
if (progress) {
|
|
750
|
-
progress.model = {
|
|
751
|
-
total: options.models,
|
|
752
|
-
value: 0
|
|
753
|
-
};
|
|
754
|
-
progress.service = {
|
|
755
|
-
total: options.services,
|
|
756
|
-
value: 0
|
|
757
|
-
};
|
|
758
|
-
updateProgressText(name, progress);
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
function formatProgressText(name, progress) {
|
|
762
|
-
const toStateText = (text) => `${chalk.default.green(text)}`;
|
|
763
|
-
const toNameText = (text) => `${chalk.default.cyan(text).padEnd(20, " ")}`;
|
|
764
|
-
const toModelText = (text) => `${chalk.default.greenBright("Model")}:${chalk.default.gray(text).padEnd(10, " ")}`;
|
|
765
|
-
const toServiceText = (text) => `${chalk.default.greenBright("Service")}:${chalk.default.gray(text).padEnd(10, " ")}`;
|
|
766
|
-
const isFinish = (progress === null || progress === void 0 ? void 0 : progress.model.value) === (progress === null || progress === void 0 ? void 0 : progress.model.total) && (progress === null || progress === void 0 ? void 0 : progress.service.value) === (progress === null || progress === void 0 ? void 0 : progress.service.total);
|
|
767
|
-
if (progress) return `${toStateText(isFinish ? "生成完成" : "生成中")} | ${toNameText(name)} | ${toModelText(`${progress.model.value} / ${progress.model.total}`)} | ${toServiceText(`${progress.service.value} / ${progress.service.total}`)}`;
|
|
768
|
-
else return `${toStateText("开始请求")} | ${toNameText(name)}`;
|
|
769
|
-
}
|
|
770
|
-
function updateProgressText(name, progress) {
|
|
771
|
-
if ((progress === null || progress === void 0 ? void 0 : progress.model.value) === (progress === null || progress === void 0 ? void 0 : progress.model.total) && (progress === null || progress === void 0 ? void 0 : progress.service.value) === (progress === null || progress === void 0 ? void 0 : progress.service.total)) progress.spinner.succeed(formatProgressText(name, progress));
|
|
772
|
-
else progress.spinner.text = formatProgressText(name, progress);
|
|
773
|
-
}
|
|
774
|
-
function updateProgress(name, type) {
|
|
775
|
-
const progress = progressMaps.get(name);
|
|
776
|
-
if (!progress) return;
|
|
777
|
-
progress[type].value++;
|
|
778
|
-
updateProgressText(name, progress);
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
//#endregion
|
|
782
|
-
//#region src/template-helpers/equal.helper.ts
|
|
783
|
-
const equalHelper = {
|
|
784
|
-
name: "equal",
|
|
785
|
-
fn(v1, v2, options) {
|
|
786
|
-
if (v1 === v2) return options.fn(this);
|
|
787
|
-
else return options.inverse(this);
|
|
788
|
-
}
|
|
789
|
-
};
|
|
790
|
-
|
|
791
|
-
//#endregion
|
|
792
|
-
//#region src/template-helpers/include-query-params.ts
|
|
793
|
-
const includeQueryParams = {
|
|
794
|
-
name: "include-query-params",
|
|
795
|
-
fn(operations, options) {
|
|
796
|
-
if (operations.some((x) => x.parametersQuery.length > 0)) return options.fn(this);
|
|
797
|
-
else return options.inverse(this);
|
|
798
|
-
}
|
|
799
|
-
};
|
|
800
|
-
|
|
801
|
-
//#endregion
|
|
802
|
-
//#region src/template-helpers/is-array.helper.ts
|
|
803
|
-
const isArrayHelper = {
|
|
804
|
-
name: "is-array",
|
|
805
|
-
fn(v1, options) {
|
|
806
|
-
if (Array.isArray(v1)) return options.fn(this);
|
|
807
|
-
else return options.inverse(this);
|
|
808
|
-
}
|
|
809
|
-
};
|
|
810
|
-
|
|
811
|
-
//#endregion
|
|
812
|
-
//#region src/template-helpers/to-upper.helper.ts
|
|
813
|
-
const toUpperHelper = {
|
|
814
|
-
name: "to-upper",
|
|
815
|
-
fn(v1, onlyFirst) {
|
|
816
|
-
if (onlyFirst) return v1.replace(/^\S/, (s) => {
|
|
817
|
-
return s.toUpperCase();
|
|
818
|
-
});
|
|
819
|
-
else return v1.toUpperCase();
|
|
820
|
-
}
|
|
821
|
-
};
|
|
822
|
-
|
|
823
|
-
//#endregion
|
|
824
|
-
//#region src/template.ts
|
|
825
|
-
function registerHandlebarTemplates() {
|
|
826
|
-
registerHandlebarPartials();
|
|
827
|
-
registerHandlebarHelpers();
|
|
828
|
-
}
|
|
829
|
-
function registerHandlebarPartials() {
|
|
830
|
-
registerHandlebarPartial("is-required");
|
|
831
|
-
registerHandlebarPartial("export-header");
|
|
832
|
-
registerHandlebarPartial("export-description");
|
|
833
|
-
registerHandlebarPartial("export-model-import");
|
|
834
|
-
registerHandlebarPartial("export-model-type");
|
|
835
|
-
registerHandlebarPartial("export-model-field");
|
|
836
|
-
registerHandlebarPartial("export-schema-type");
|
|
837
|
-
registerHandlebarPartial("export-service-import");
|
|
838
|
-
registerHandlebarPartial("export-service-class");
|
|
839
|
-
registerHandlebarPartial("export-service-namespace");
|
|
840
|
-
registerHandlebarPartial("export-service-namespace-type");
|
|
841
|
-
registerHandlebarPartial("export-service-operation");
|
|
842
|
-
registerHandlebarPartial("export-operation-params-path");
|
|
843
|
-
registerHandlebarPartial("export-operation-params-query");
|
|
844
|
-
registerHandlebarPartial("export-operation-params-body");
|
|
845
|
-
registerHandlebarPartial("export-operation-response");
|
|
846
|
-
}
|
|
847
|
-
function registerHandlebarHelpers() {
|
|
848
|
-
registerHandlebarHelper(equalHelper);
|
|
849
|
-
registerHandlebarHelper(isArrayHelper);
|
|
850
|
-
registerHandlebarHelper(toUpperHelper);
|
|
851
|
-
registerHandlebarHelper(includeQueryParams);
|
|
852
|
-
}
|
|
853
|
-
function registerHandlebarHelper(helper) {
|
|
854
|
-
handlebars.default.registerHelper(helper.name, helper.fn);
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* 注册Handlebar模板
|
|
858
|
-
*/
|
|
859
|
-
function registerHandlebarPartial(input) {
|
|
860
|
-
const template = loadHandlebarTemplate(`partials/${input}`);
|
|
861
|
-
handlebars.default.registerPartial(input, template);
|
|
862
|
-
}
|
|
863
|
-
/**
|
|
864
|
-
* 加载模板
|
|
865
|
-
* @returns 模板内容
|
|
866
|
-
*/
|
|
867
|
-
function loadHandlebarTemplate(input) {
|
|
868
|
-
const templatePath = node_path.resolve(__dirname, "templates", `${input}.hbs`);
|
|
869
|
-
return node_fs.readFileSync(templatePath, "utf-8");
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
//#endregion
|
|
873
|
-
//#region src/utils/get-openapi-document.ts
|
|
874
|
-
/**
|
|
875
|
-
* Load and parse te open api spec. If the file extension is ".yml" or ".yaml"
|
|
876
|
-
* we will try to parse the file as a YAML spec, otherwise we will fall back
|
|
877
|
-
* on parsing the file as JSON.
|
|
878
|
-
* @param input Path or url
|
|
879
|
-
*/
|
|
880
|
-
async function getOpenApiDocument(input) {
|
|
881
|
-
return await _apidevtools_swagger_parser.default.parse(input);
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
//#endregion
|
|
885
|
-
//#region src/utils/get-openapi-version.ts
|
|
886
|
-
/**
|
|
887
|
-
* 获取OpenAPI版本
|
|
888
|
-
* @param document
|
|
889
|
-
* @returns OpenAPI版本
|
|
890
|
-
*/
|
|
891
|
-
function getOpenAPIVersion(document) {
|
|
892
|
-
const version = (document === null || document === void 0 ? void 0 : document.swagger) || (document === null || document === void 0 ? void 0 : document.openapi);
|
|
893
|
-
if (typeof version === "string") {
|
|
894
|
-
const v = Number.parseInt(version.charAt(0));
|
|
895
|
-
if (Object.values(OpenAPIVersion).includes(v)) return v;
|
|
896
|
-
}
|
|
897
|
-
throw new Error(`无法识别的OPENAPI版本: "${String(version)}"`);
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
//#endregion
|
|
901
|
-
//#region src/generate/write-config.ts
|
|
902
|
-
const ConfigFilePath = node_path.default.resolve(".request", ".config.json");
|
|
903
|
-
/**
|
|
904
|
-
* 生成应用配置文件
|
|
905
|
-
*/
|
|
906
|
-
function updateOptionsFromLocalConfig(options, output) {
|
|
907
|
-
const config = readLocalConfig();
|
|
908
|
-
const toUpdateOptions = /* @__PURE__ */ new Map();
|
|
909
|
-
options = options.filter((item) => {
|
|
910
|
-
const md5 = isNeedUpdate(item.name, config, output);
|
|
911
|
-
if (md5) {
|
|
912
|
-
toUpdateOptions.set(item.name, md5);
|
|
913
|
-
return true;
|
|
914
|
-
} else return false;
|
|
915
|
-
});
|
|
916
|
-
options.forEach((item) => {
|
|
917
|
-
item.input = node_path.default.resolve(".request", `${item.name}.json`);
|
|
918
|
-
});
|
|
919
|
-
writeLocalConfig(toUpdateOptions, config);
|
|
920
|
-
return options;
|
|
921
|
-
}
|
|
922
|
-
/**
|
|
923
|
-
* 比较文件的MD5值判断文件是否更新
|
|
924
|
-
*/
|
|
925
|
-
function isNeedUpdate(name, config, output) {
|
|
926
|
-
const file = node_path.default.resolve(".request", `${name}.json`);
|
|
927
|
-
const outputDir = node_path.default.resolve(output);
|
|
928
|
-
if (!node_fs.default.existsSync(file)) throw new Error(`未找到相应的配置文件: ${file}`);
|
|
929
|
-
const data = node_fs.default.readFileSync(file);
|
|
930
|
-
const md5 = node_crypto.default.createHash("md5").update(data.toString()).digest("hex");
|
|
931
|
-
if (!node_fs.default.existsSync(node_path.default.join(outputDir, name))) return md5;
|
|
932
|
-
const configItem = config.find((x) => x.name === name);
|
|
933
|
-
if (!configItem || configItem.md5 !== md5) return md5;
|
|
934
|
-
}
|
|
935
|
-
function readLocalConfig() {
|
|
936
|
-
if (!node_fs.default.existsSync(ConfigFilePath)) return [];
|
|
937
|
-
const data = node_fs.default.readFileSync(ConfigFilePath, "utf-8");
|
|
938
|
-
return JSON.parse(data);
|
|
939
|
-
}
|
|
940
|
-
function writeLocalConfig(toUpdateOptions, config) {
|
|
941
|
-
toUpdateOptions.forEach((md5, name) => {
|
|
942
|
-
const configItem = config.find((x) => x.name === name);
|
|
943
|
-
if (configItem) {
|
|
944
|
-
configItem.md5 = md5;
|
|
945
|
-
configItem.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
946
|
-
} else config.push({
|
|
947
|
-
name,
|
|
948
|
-
md5,
|
|
949
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
950
|
-
});
|
|
951
|
-
});
|
|
952
|
-
node_fs.default.writeFileSync(ConfigFilePath, JSON.stringify(config, null, 2));
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
//#endregion
|
|
956
|
-
//#region src/generate/write-file.ts
|
|
957
|
-
/**
|
|
958
|
-
*
|
|
959
|
-
* @param output
|
|
960
|
-
* @param content
|
|
961
|
-
*/
|
|
962
|
-
function writeFile(output, content) {
|
|
963
|
-
node_fs.writeFileSync(output, content, "utf-8");
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
//#endregion
|
|
967
|
-
//#region src/generate/write-model.ts
|
|
968
|
-
function writeModel(model, output) {
|
|
969
|
-
const templateSource = loadHandlebarTemplate("export-model");
|
|
970
|
-
writeFile(output, handlebars.default.compile(templateSource)(model));
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
//#endregion
|
|
974
|
-
//#region src/generate/write-models.ts
|
|
975
|
-
/**
|
|
976
|
-
* 写入Model文件
|
|
977
|
-
* @param client
|
|
978
|
-
* @param options
|
|
979
|
-
*/
|
|
980
|
-
function writeModels(client, options) {
|
|
981
|
-
if (!options.exportModels || !client.models) return;
|
|
982
|
-
const output = node_path.join(options.output, "models");
|
|
983
|
-
if (node_fs.existsSync(output)) node_fs.rmSync(output, {
|
|
984
|
-
recursive: true,
|
|
985
|
-
force: true
|
|
986
|
-
});
|
|
987
|
-
node_fs.mkdirSync(output, { recursive: true });
|
|
988
|
-
client.models.forEach((model) => {
|
|
989
|
-
const filename = `${model.name}.ts`;
|
|
990
|
-
writeModel(model, node_path.join(output, filename));
|
|
991
|
-
updateProgress(options.name || "default", "model");
|
|
992
|
-
});
|
|
993
|
-
}
|
|
994
|
-
|
|
995
|
-
//#endregion
|
|
996
|
-
//#region src/generate/write-service.ts
|
|
997
|
-
/**
|
|
998
|
-
* 写入Service文件
|
|
999
|
-
* @param service
|
|
1000
|
-
* @param output
|
|
1001
|
-
*/
|
|
1002
|
-
function writeService(service, output) {
|
|
1003
|
-
const templateSource = loadHandlebarTemplate("export-service");
|
|
1004
|
-
writeFile(output, handlebars.default.compile(templateSource)(service));
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
//#endregion
|
|
1008
|
-
//#region src/generate/write-services.ts
|
|
1009
|
-
function writeServices(client, options) {
|
|
1010
|
-
if (!client.services) return;
|
|
1011
|
-
const output = node_path.join(options.output, "services");
|
|
1012
|
-
if (node_fs.existsSync(output)) node_fs.rmSync(output, {
|
|
1013
|
-
recursive: true,
|
|
1014
|
-
force: true
|
|
1015
|
-
});
|
|
1016
|
-
node_fs.mkdirSync(output, { recursive: true });
|
|
1017
|
-
client.services.forEach((service) => {
|
|
1018
|
-
const filename = `${service.name}Service.ts`;
|
|
1019
|
-
service.application = Generate.options.appendService === false ? "" : options.application;
|
|
1020
|
-
writeService(service, node_path.join(output, filename));
|
|
1021
|
-
updateProgress(options.name || "default", "service");
|
|
1022
|
-
});
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
//#endregion
|
|
1026
|
-
//#region src/generate/index.ts
|
|
1027
|
-
var Generate = class Generate {
|
|
1028
|
-
static options;
|
|
1029
|
-
/**
|
|
1030
|
-
* 生成入口
|
|
1031
|
-
* @param options
|
|
1032
|
-
* @returns Promise<void>
|
|
1033
|
-
*/
|
|
1034
|
-
static async startup(options) {
|
|
1035
|
-
Generate.options = options;
|
|
1036
|
-
registerHandlebarTemplates();
|
|
1037
|
-
let applicationOptions = generateServiceOptions(options);
|
|
1038
|
-
applicationOptions = updateOptionsFromLocalConfig(applicationOptions, options.output);
|
|
1039
|
-
const applications = [];
|
|
1040
|
-
for (const applicationOption of applicationOptions) {
|
|
1041
|
-
const client = await Generate.generateApplicationClient(applicationOption);
|
|
1042
|
-
applications.push({
|
|
1043
|
-
client,
|
|
1044
|
-
options: applicationOption
|
|
1045
|
-
});
|
|
1046
|
-
}
|
|
1047
|
-
applications.forEach((application) => {
|
|
1048
|
-
createProgress(application.options.name || "default");
|
|
1049
|
-
Generate.writeClient(application.client, application.options);
|
|
1050
|
-
});
|
|
1051
|
-
}
|
|
1052
|
-
static async getApiDocument(input) {
|
|
1053
|
-
try {
|
|
1054
|
-
return await getOpenApiDocument(input);
|
|
1055
|
-
} catch {
|
|
1056
|
-
console.error(`请求文件[${input}]失败,请稍后重试.`);
|
|
1057
|
-
node_process.default.exit(0);
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
/**
|
|
1061
|
-
* 生成应用
|
|
1062
|
-
*/
|
|
1063
|
-
static async generateApplicationClient(options) {
|
|
1064
|
-
const document = await Generate.getApiDocument(options.input);
|
|
1065
|
-
const version = getOpenAPIVersion(document);
|
|
1066
|
-
return Generate.generateClient(document, version);
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* 生成对象信息
|
|
1070
|
-
* @param document
|
|
1071
|
-
* @param version
|
|
1072
|
-
* @returns GenerateClient
|
|
1073
|
-
*/
|
|
1074
|
-
static generateClient(document, version) {
|
|
1075
|
-
switch (version) {
|
|
1076
|
-
case OpenAPIVersion.V2: return parseV2(document);
|
|
1077
|
-
case OpenAPIVersion.V3: return parseV3(document);
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
/**
|
|
1081
|
-
* 写入Client对象
|
|
1082
|
-
* @param client
|
|
1083
|
-
* @param options
|
|
1084
|
-
*/
|
|
1085
|
-
static writeClient(client, options) {
|
|
1086
|
-
startProgress(options.name || "default", {
|
|
1087
|
-
models: client.models.length,
|
|
1088
|
-
services: client.services.length
|
|
1089
|
-
});
|
|
1090
|
-
writeModels(client, options);
|
|
1091
|
-
writeServices(client, options);
|
|
1092
|
-
}
|
|
1093
|
-
};
|
|
1094
|
-
|
|
1095
|
-
//#endregion
|
|
1096
|
-
//#region src/define-config.ts
|
|
1097
|
-
function defineConfig(config) {
|
|
1098
|
-
return config;
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
//#endregion
|
|
1102
|
-
//#region src/index.ts
|
|
1103
|
-
const generate = Generate.startup;
|
|
1104
|
-
const download = Download.startup;
|
|
1105
|
-
|
|
1106
|
-
//#endregion
|
|
1107
|
-
exports.defineConfig = defineConfig;
|
|
1108
|
-
exports.download = download;
|
|
1109
|
-
exports.generate = generate;
|