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