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