@gopowerteam/request-generate 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-7BMEVKSG.mjs +18 -0
- package/dist/chunk-XXPGZHWZ.js +10 -0
- package/dist/index.js +4 -7
- package/dist/index.mjs +896 -0
- package/dist/vite-plugin/index.d.ts +13 -0
- package/dist/vite-plugin/index.js +173 -0
- package/dist/vite-plugin/index.mjs +173 -0
- package/package.json +8 -2
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// ../../node_modules/.pnpm/tsup@6.3.0_mwhvu7sfp6vq5ryuwb6hlbjfka/node_modules/tsup/assets/esm_shims.js
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
import path from "path";
|
|
11
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
12
|
+
var getDirname = () => path.dirname(getFilename());
|
|
13
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
__publicField,
|
|
17
|
+
__dirname
|
|
18
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
exports.__publicField = __publicField;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _class; var _class2; var _class3;
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _class; var _class2; var _class3;
|
|
2
|
+
|
|
3
|
+
var _chunkXXPGZHWZjs = require('./chunk-XXPGZHWZ.js');
|
|
7
4
|
|
|
8
5
|
// src/utils/get-openapi-document.ts
|
|
9
6
|
var _swaggerparser = require('@apidevtools/swagger-parser'); var _swaggerparser2 = _interopRequireDefault(_swaggerparser);
|
|
@@ -889,7 +886,7 @@ var _Generate = class {
|
|
|
889
886
|
}
|
|
890
887
|
};
|
|
891
888
|
var Generate = _Generate;
|
|
892
|
-
__publicField(Generate, "options");
|
|
889
|
+
_chunkXXPGZHWZjs.__publicField.call(void 0, Generate, "options");
|
|
893
890
|
|
|
894
891
|
// src/index.ts
|
|
895
892
|
var src_default = Generate.startup;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__dirname,
|
|
3
|
+
__publicField
|
|
4
|
+
} from "./chunk-7BMEVKSG.mjs";
|
|
5
|
+
|
|
6
|
+
// src/utils/get-openapi-document.ts
|
|
7
|
+
import swaggerParse from "@apidevtools/swagger-parser";
|
|
8
|
+
var getOpenApiDocument = async (input) => {
|
|
9
|
+
return await swaggerParse.parse(input);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/config/enum.config.ts
|
|
13
|
+
var OpenAPIVersion = /* @__PURE__ */ ((OpenAPIVersion2) => {
|
|
14
|
+
OpenAPIVersion2[OpenAPIVersion2["V2"] = 2] = "V2";
|
|
15
|
+
OpenAPIVersion2[OpenAPIVersion2["V3"] = 3] = "V3";
|
|
16
|
+
return OpenAPIVersion2;
|
|
17
|
+
})(OpenAPIVersion || {});
|
|
18
|
+
|
|
19
|
+
// src/utils/get-openapi-version.ts
|
|
20
|
+
function getOpenAPIVersion(document) {
|
|
21
|
+
const version = (document == null ? void 0 : document.swagger) || (document == null ? void 0 : document.openapi);
|
|
22
|
+
if (typeof version === "string") {
|
|
23
|
+
const v = Number.parseInt(version.charAt(0));
|
|
24
|
+
if (Object.values(OpenAPIVersion).includes(v)) {
|
|
25
|
+
return v;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`\u65E0\u6CD5\u8BC6\u522B\u7684OPENAPI\u7248\u672C: "${String(version)}"`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/utils/get-services-options.ts
|
|
32
|
+
import path from "node:path";
|
|
33
|
+
function createOptions(options, name, application = "") {
|
|
34
|
+
return {
|
|
35
|
+
name,
|
|
36
|
+
input: `${options.gateway}${application}${options.openapi}`,
|
|
37
|
+
output: name ? path.join(options.output, name) : options.output,
|
|
38
|
+
exportModels: options.exportModels
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function generateServiceOptions(options) {
|
|
42
|
+
if (options.applications && Object.keys(options.applications).length) {
|
|
43
|
+
return Object.entries(options.applications).map(
|
|
44
|
+
([name, application]) => createOptions(options, name, application)
|
|
45
|
+
);
|
|
46
|
+
} else {
|
|
47
|
+
return [createOptions(options)];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/utils/get-camel-name.ts
|
|
52
|
+
function getCamelName(value) {
|
|
53
|
+
return value.replace(/^[^a-zA-Z]+/g, "").replace(/[^\w-]+/g, "-").replace(/-/g, "").replace(/^\S/, (s) => s.toUpperCase()).trim();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/entities/model.ts
|
|
57
|
+
var Model = class {
|
|
58
|
+
name;
|
|
59
|
+
fields;
|
|
60
|
+
imports;
|
|
61
|
+
constructor(name) {
|
|
62
|
+
this.name = name;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/entities/field.ts
|
|
67
|
+
var Field = class {
|
|
68
|
+
name;
|
|
69
|
+
required;
|
|
70
|
+
type;
|
|
71
|
+
ref;
|
|
72
|
+
enums;
|
|
73
|
+
description;
|
|
74
|
+
imports;
|
|
75
|
+
constructor(name, required) {
|
|
76
|
+
this.name = name;
|
|
77
|
+
this.required = required;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/utils/get-mapped-type.ts
|
|
82
|
+
var TYPE_MAPPINGS = /* @__PURE__ */ new Map([
|
|
83
|
+
["file", "binary"],
|
|
84
|
+
["any", "any"],
|
|
85
|
+
["object", "any"],
|
|
86
|
+
["array", "any[]"],
|
|
87
|
+
["boolean", "boolean"],
|
|
88
|
+
["byte", "number"],
|
|
89
|
+
["int", "number"],
|
|
90
|
+
["integer", "number"],
|
|
91
|
+
["float", "number"],
|
|
92
|
+
["double", "number"],
|
|
93
|
+
["short", "number"],
|
|
94
|
+
["long", "number"],
|
|
95
|
+
["number", "number"],
|
|
96
|
+
["char", "string"],
|
|
97
|
+
["date", "string"],
|
|
98
|
+
["date-time", "string"],
|
|
99
|
+
["password", "string"],
|
|
100
|
+
["string", "string"],
|
|
101
|
+
["void", "void"],
|
|
102
|
+
["null", "null"]
|
|
103
|
+
]);
|
|
104
|
+
var getMappedType = (type = "object", format) => {
|
|
105
|
+
if (format === "binary") {
|
|
106
|
+
return "binary";
|
|
107
|
+
}
|
|
108
|
+
return TYPE_MAPPINGS.get(type) || "any";
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// src/parse/v2/strip-namespace.ts
|
|
112
|
+
var stripNamespace = (value) => {
|
|
113
|
+
return value.trim().replace(/^#\/definitions\//, "").replace(/^#\/parameters\//, "").replace(/^#\/responses\//, "").replace(/^#\/securityDefinitions\//, "");
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// src/parse/v2/parse-schema-type.ts
|
|
117
|
+
function parseSchemaType(schema) {
|
|
118
|
+
if ("$ref" in schema && schema.$ref) {
|
|
119
|
+
const ref = getCamelName(stripNamespace(schema.$ref));
|
|
120
|
+
return {
|
|
121
|
+
type: "any",
|
|
122
|
+
ref,
|
|
123
|
+
imports: [ref]
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (!("$ref" in schema) && schema.type !== "array" && typeof schema.type === "string" && !schema.allOf && !schema.anyOf && !schema.oneOf) {
|
|
127
|
+
return {
|
|
128
|
+
type: getMappedType(schema.type || "any"),
|
|
129
|
+
ref: void 0,
|
|
130
|
+
enums: schema.enum
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (!("$ref" in schema) && schema.type === "array" && schema.items && "$ref" in schema.items && schema.items.$ref) {
|
|
134
|
+
const ref = getCamelName(stripNamespace(schema.items.$ref));
|
|
135
|
+
return {
|
|
136
|
+
type: "any[]",
|
|
137
|
+
ref: `${ref}[]`,
|
|
138
|
+
imports: [ref]
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
if (!("$ref" in schema) && schema.type === "array" && schema.items && !("$ref" in schema.items)) {
|
|
142
|
+
return {
|
|
143
|
+
type: `${getMappedType(schema.items.type)}[]`,
|
|
144
|
+
ref: void 0
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
throw new Error("\u65E0\u6CD5\u89E3\u6790\u76F8\u5E94\u7684schema");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/parse/v2/parse-field.ts
|
|
151
|
+
function parseField(name, properties, required = false) {
|
|
152
|
+
const field = new Field(name, required);
|
|
153
|
+
const { type, ref, imports, enums } = parseSchemaType(properties);
|
|
154
|
+
field.type = type;
|
|
155
|
+
field.ref = ref;
|
|
156
|
+
field.imports = imports;
|
|
157
|
+
field.enums = enums;
|
|
158
|
+
if (!("$ref" in properties)) {
|
|
159
|
+
field.description = properties.description;
|
|
160
|
+
}
|
|
161
|
+
return field;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// src/parse/v2/parse-fields.ts
|
|
165
|
+
function parseFields(schema) {
|
|
166
|
+
return Object.entries(schema.properties || {}).map(
|
|
167
|
+
([name, property]) => {
|
|
168
|
+
var _a;
|
|
169
|
+
return parseField(name, property, (_a = schema.required) == null ? void 0 : _a.includes(name));
|
|
170
|
+
}
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/parse/v2/parse-model.ts
|
|
175
|
+
function parseModel(name, definition) {
|
|
176
|
+
const model = new Model(name);
|
|
177
|
+
model.fields = parseFields(definition);
|
|
178
|
+
const imports = model.fields.filter((field) => field.imports).reduce((r, m) => [...r, ...m.imports || []], []).filter((m) => m !== name);
|
|
179
|
+
model.imports = Array.from(new Set(imports));
|
|
180
|
+
return model;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/parse/v2/parse-models.ts
|
|
184
|
+
function parseModels(document) {
|
|
185
|
+
const models = [];
|
|
186
|
+
if (document.definitions) {
|
|
187
|
+
for (const definitionName in document.definitions) {
|
|
188
|
+
if (Object.getOwnPropertyNames(document.definitions).includes(
|
|
189
|
+
definitionName
|
|
190
|
+
)) {
|
|
191
|
+
const definition = document.definitions[definitionName];
|
|
192
|
+
const model = parseModel(getCamelName(definitionName), definition);
|
|
193
|
+
models.push(model);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return models;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/entities/service.ts
|
|
201
|
+
var Service = class {
|
|
202
|
+
constructor(name) {
|
|
203
|
+
var _a, _b;
|
|
204
|
+
this.name = name;
|
|
205
|
+
this.responseType = ((_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.responseType) || "promise";
|
|
206
|
+
}
|
|
207
|
+
name;
|
|
208
|
+
imports = [];
|
|
209
|
+
operations = [];
|
|
210
|
+
responseType;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/utils/get-service-name.ts
|
|
214
|
+
function getServiceName(path5, method, operationObject, tags) {
|
|
215
|
+
var _a, _b, _c;
|
|
216
|
+
const resolve2 = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.serviceResolve;
|
|
217
|
+
if (resolve2) {
|
|
218
|
+
return resolve2({ path: path5, method, object: operationObject, tags });
|
|
219
|
+
} else {
|
|
220
|
+
return ((_c = operationObject.tags) == null ? void 0 : _c.map((tag) => getCamelName(tag))) || "Default";
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/entities/operation.ts
|
|
225
|
+
var Operation = class {
|
|
226
|
+
name;
|
|
227
|
+
method;
|
|
228
|
+
path;
|
|
229
|
+
description;
|
|
230
|
+
responseRef;
|
|
231
|
+
responseType;
|
|
232
|
+
parametersPath = [];
|
|
233
|
+
parametersQuery = [];
|
|
234
|
+
parametersBody;
|
|
235
|
+
imports = [];
|
|
236
|
+
constructor(name, method, path5) {
|
|
237
|
+
var _a, _b;
|
|
238
|
+
this.name = name;
|
|
239
|
+
this.method = method;
|
|
240
|
+
this.path = path5;
|
|
241
|
+
this.responseType = ((_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.responseType) || "promise";
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// src/utils/get-operation-name.ts
|
|
246
|
+
function getOperationName(path5, method, operationObject) {
|
|
247
|
+
var _a, _b;
|
|
248
|
+
const resolve2 = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.operationResolve;
|
|
249
|
+
if (resolve2) {
|
|
250
|
+
return resolve2({ path: path5, method, object: operationObject });
|
|
251
|
+
} else {
|
|
252
|
+
return operationObject.operationId || "";
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/entities/operation-parameter.ts
|
|
257
|
+
var OperationParameter = class {
|
|
258
|
+
in;
|
|
259
|
+
name;
|
|
260
|
+
type;
|
|
261
|
+
ref;
|
|
262
|
+
enums;
|
|
263
|
+
description;
|
|
264
|
+
required;
|
|
265
|
+
imports = [];
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// src/parse/v2/parse-parameters-body.ts
|
|
269
|
+
function parseParametersBody(parameters) {
|
|
270
|
+
const requestBody = parameters.find(
|
|
271
|
+
(parameter) => !("$ref" in parameter) && parameter.in === "body"
|
|
272
|
+
);
|
|
273
|
+
if (requestBody && "schema" in requestBody) {
|
|
274
|
+
const { type, ref, imports } = parseSchemaType(requestBody.schema);
|
|
275
|
+
const parameter = new OperationParameter();
|
|
276
|
+
parameter.name = "requestBody";
|
|
277
|
+
parameter.in = "body";
|
|
278
|
+
parameter.type = type;
|
|
279
|
+
parameter.ref = ref;
|
|
280
|
+
parameter.imports = imports || [];
|
|
281
|
+
return parameter;
|
|
282
|
+
} else {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// src/parse/v2/parse-parameters-path.ts
|
|
288
|
+
function parseParametersPath(parameters) {
|
|
289
|
+
return parameters.reduce((r, p) => {
|
|
290
|
+
if (!("$ref" in p) && p.in === "path" && p.schema) {
|
|
291
|
+
const { type, ref, imports, enums } = parseSchemaType(p.schema);
|
|
292
|
+
const parameter = new OperationParameter();
|
|
293
|
+
parameter.name = p.name;
|
|
294
|
+
parameter.description = p.description;
|
|
295
|
+
parameter.in = "path";
|
|
296
|
+
parameter.type = type;
|
|
297
|
+
parameter.ref = ref;
|
|
298
|
+
parameter.imports = imports || [];
|
|
299
|
+
parameter.enums = enums;
|
|
300
|
+
r.push(parameter);
|
|
301
|
+
}
|
|
302
|
+
return r;
|
|
303
|
+
}, []);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// src/parse/v2/parse-parameters-query.ts
|
|
307
|
+
function parseParametersQuery(parameters) {
|
|
308
|
+
var _a, _b;
|
|
309
|
+
const excludeParams = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.excludeQueryParams;
|
|
310
|
+
return parameters.reduce((r, p) => {
|
|
311
|
+
if (!("$ref" in p) && p.in === "query" && p.schema && !(excludeParams && excludeParams.includes(p.name))) {
|
|
312
|
+
const { type, ref, imports, enums } = parseSchemaType(p.schema);
|
|
313
|
+
const parameter = new OperationParameter();
|
|
314
|
+
parameter.name = p.name;
|
|
315
|
+
parameter.description = p.description;
|
|
316
|
+
parameter.in = "query";
|
|
317
|
+
parameter.type = type;
|
|
318
|
+
parameter.ref = ref;
|
|
319
|
+
parameter.required = p.required;
|
|
320
|
+
parameter.imports = imports || [];
|
|
321
|
+
parameter.enums = enums;
|
|
322
|
+
r.push(parameter);
|
|
323
|
+
}
|
|
324
|
+
return r;
|
|
325
|
+
}, []);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// src/parse/v2/parse-operation.ts
|
|
329
|
+
function parseOperation(path5, method, operationObject) {
|
|
330
|
+
var _a;
|
|
331
|
+
const name = getOperationName(path5, method, operationObject);
|
|
332
|
+
const operation = new Operation(name, method, path5);
|
|
333
|
+
operation.description = operationObject.summary || operation.description;
|
|
334
|
+
if (operationObject.parameters) {
|
|
335
|
+
operation.parametersBody = parseParametersBody(operationObject.parameters);
|
|
336
|
+
operation.parametersPath = parseParametersPath(operationObject.parameters);
|
|
337
|
+
operation.parametersQuery = parseParametersQuery(operationObject.parameters);
|
|
338
|
+
}
|
|
339
|
+
const responseSchema = parseResponseType(operationObject.responses);
|
|
340
|
+
operation.imports = Array.from(
|
|
341
|
+
/* @__PURE__ */ new Set([
|
|
342
|
+
...((_a = operation.parametersBody) == null ? void 0 : _a.imports) || [],
|
|
343
|
+
...operation.parametersPath.flatMap((p) => p.imports) || [],
|
|
344
|
+
...operation.parametersQuery.flatMap((p) => p.imports) || [],
|
|
345
|
+
...(responseSchema == null ? void 0 : responseSchema.imports) || []
|
|
346
|
+
])
|
|
347
|
+
);
|
|
348
|
+
operation.responseRef = (responseSchema == null ? void 0 : responseSchema.ref) || "void";
|
|
349
|
+
return operation;
|
|
350
|
+
}
|
|
351
|
+
function parseResponseType(responses) {
|
|
352
|
+
const SUCCESS_STATUS_CODE = "200";
|
|
353
|
+
const response = responses == null ? void 0 : responses[SUCCESS_STATUS_CODE];
|
|
354
|
+
if (response && "$ref" in response) {
|
|
355
|
+
return parseSchemaType(response);
|
|
356
|
+
}
|
|
357
|
+
if (response && "schema" in response && (response == null ? void 0 : response.schema)) {
|
|
358
|
+
return parseSchemaType(response == null ? void 0 : response.schema);
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
type: "void",
|
|
362
|
+
ref: "void",
|
|
363
|
+
imports: []
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/parse/v2/parse-service.ts
|
|
368
|
+
function parseService(path5, method, operationObject, tags, services) {
|
|
369
|
+
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
370
|
+
const names = toNames(
|
|
371
|
+
getServiceName(
|
|
372
|
+
path5,
|
|
373
|
+
method,
|
|
374
|
+
operationObject,
|
|
375
|
+
tags || []
|
|
376
|
+
)
|
|
377
|
+
);
|
|
378
|
+
const operation = parseOperation(path5, method, operationObject);
|
|
379
|
+
names.forEach((name) => {
|
|
380
|
+
let service = services.find((service2) => service2.name === name);
|
|
381
|
+
if (!service) {
|
|
382
|
+
service = new Service(name);
|
|
383
|
+
services.push(service);
|
|
384
|
+
}
|
|
385
|
+
if (service) {
|
|
386
|
+
service.operations.push(operation);
|
|
387
|
+
}
|
|
388
|
+
service.operations.flatMap((operation2) => operation2.imports).forEach((model) => {
|
|
389
|
+
if (service && !service.imports.includes(model)) {
|
|
390
|
+
service.imports.push(model);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// src/parse/v2/parse-services.ts
|
|
397
|
+
function parseServices(document) {
|
|
398
|
+
const services = [];
|
|
399
|
+
Object.entries(document.paths).forEach(([path5, pathObject]) => {
|
|
400
|
+
if (pathObject) {
|
|
401
|
+
Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
402
|
+
parseService(
|
|
403
|
+
path5,
|
|
404
|
+
method,
|
|
405
|
+
operationObject,
|
|
406
|
+
document.tags || [],
|
|
407
|
+
services
|
|
408
|
+
);
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
return services;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// src/parse/v2/index.ts
|
|
416
|
+
function parseV2(document) {
|
|
417
|
+
const models = parseModels(document);
|
|
418
|
+
const services = parseServices(document);
|
|
419
|
+
return {
|
|
420
|
+
models,
|
|
421
|
+
services
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// src/parse/v3/strip-namespace.ts
|
|
426
|
+
var stripNamespace2 = (value) => {
|
|
427
|
+
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\//, "");
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
// src/parse/v3/parse-schema-type.ts
|
|
431
|
+
function parseSchemaType2(schema) {
|
|
432
|
+
if ("$ref" in schema) {
|
|
433
|
+
const ref = getCamelName(stripNamespace2(schema.$ref));
|
|
434
|
+
return {
|
|
435
|
+
type: "any",
|
|
436
|
+
ref,
|
|
437
|
+
imports: [ref]
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
if (!("$ref" in schema) && schema.type !== "array" && !schema.allOf && !schema.anyOf && !schema.oneOf) {
|
|
441
|
+
return {
|
|
442
|
+
type: getMappedType(schema.type || "any"),
|
|
443
|
+
ref: void 0,
|
|
444
|
+
enums: schema.enum
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
if (schema.type === "array" && "$ref" in schema.items) {
|
|
448
|
+
const ref = getCamelName(stripNamespace2(schema.items.$ref));
|
|
449
|
+
return {
|
|
450
|
+
type: "any[]",
|
|
451
|
+
ref: `${ref}[]`,
|
|
452
|
+
imports: [ref]
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
if (schema.type === "array" && !("$ref" in schema.items)) {
|
|
456
|
+
return {
|
|
457
|
+
type: `${getMappedType(schema.items.type)}[]`,
|
|
458
|
+
ref: void 0
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
if (schema.allOf || schema.anyOf || schema.oneOf) {
|
|
462
|
+
const ofSchema = schema.allOf || schema.anyOf || schema.oneOf;
|
|
463
|
+
const ofSchemaArray = ofSchema == null ? void 0 : ofSchema.map((s) => parseSchemaType2(s));
|
|
464
|
+
if (ofSchemaArray) {
|
|
465
|
+
const hasRef = ofSchemaArray.some((s) => s.ref);
|
|
466
|
+
return {
|
|
467
|
+
type: hasRef ? "any" : ofSchemaArray.map((s) => s.type).join("|"),
|
|
468
|
+
ref: hasRef ? ofSchemaArray.map((s) => s.ref && getCamelName(s.ref) || s.type).join("|") : void 0,
|
|
469
|
+
imports: hasRef ? ofSchemaArray.reduce(
|
|
470
|
+
(r, s) => (s.ref && !r.includes(s.ref) && r.push(getCamelName(s.ref)), r),
|
|
471
|
+
[]
|
|
472
|
+
) : void 0
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
throw new Error("\u65E0\u6CD5\u89E3\u6790\u76F8\u5E94\u7684schema");
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// src/parse/v3/parse-field.ts
|
|
480
|
+
function parseField2(name, properties, required = false) {
|
|
481
|
+
const field = new Field(name, required);
|
|
482
|
+
const { type, ref, imports, enums } = parseSchemaType2(properties);
|
|
483
|
+
field.type = type;
|
|
484
|
+
field.ref = ref;
|
|
485
|
+
field.imports = imports;
|
|
486
|
+
field.enums = enums;
|
|
487
|
+
if (!("$ref" in properties)) {
|
|
488
|
+
field.description = properties.description;
|
|
489
|
+
}
|
|
490
|
+
return field;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// src/parse/v3/parse-fields.ts
|
|
494
|
+
function parseFields2(schema) {
|
|
495
|
+
return Object.entries(schema.properties || {}).map(
|
|
496
|
+
([name, property]) => {
|
|
497
|
+
var _a;
|
|
498
|
+
return parseField2(name, property, (_a = schema.required) == null ? void 0 : _a.includes(name));
|
|
499
|
+
}
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// src/parse/v3/parse-model.ts
|
|
504
|
+
function parseModel2(name, definition) {
|
|
505
|
+
const model = new Model(name);
|
|
506
|
+
model.fields = parseFields2(definition);
|
|
507
|
+
const imports = model.fields.filter((field) => field.imports).reduce((r, m) => [...r, ...m.imports || []], []).filter((m) => m !== name);
|
|
508
|
+
model.imports = Array.from(new Set(imports));
|
|
509
|
+
return model;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// src/parse/v3/parse-models.ts
|
|
513
|
+
function parseModels2(document) {
|
|
514
|
+
const models = [];
|
|
515
|
+
if (document.components) {
|
|
516
|
+
for (const definitionName in document.components.schemas) {
|
|
517
|
+
const { schemas } = document.components;
|
|
518
|
+
if (Object.getOwnPropertyNames(schemas).includes(definitionName)) {
|
|
519
|
+
const definition = document.components.schemas[definitionName];
|
|
520
|
+
const model = parseModel2(getCamelName(definitionName), definition);
|
|
521
|
+
models.push(model);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return models;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// src/parse/v3/parse-parameters-body.ts
|
|
529
|
+
function parseParametersBody2(requestBody) {
|
|
530
|
+
const { type, ref, imports } = parseBodyType(requestBody);
|
|
531
|
+
const parameter = new OperationParameter();
|
|
532
|
+
parameter.name = "requestBody";
|
|
533
|
+
parameter.in = "body";
|
|
534
|
+
parameter.type = type;
|
|
535
|
+
parameter.ref = ref;
|
|
536
|
+
parameter.imports = imports || [];
|
|
537
|
+
return parameter;
|
|
538
|
+
}
|
|
539
|
+
function parseBodyType(requestBody) {
|
|
540
|
+
var _a, _b;
|
|
541
|
+
if ("$ref" in requestBody) {
|
|
542
|
+
return parseSchemaType2(requestBody);
|
|
543
|
+
}
|
|
544
|
+
if ("content" in requestBody && ((_a = requestBody == null ? void 0 : requestBody.content["application/json"]) == null ? void 0 : _a.schema)) {
|
|
545
|
+
const schema = (_b = requestBody == null ? void 0 : requestBody.content["application/json"]) == null ? void 0 : _b.schema;
|
|
546
|
+
return parseSchemaType2(schema);
|
|
547
|
+
}
|
|
548
|
+
throw new Error("\u65E0\u6CD5\u89E3\u6790RequestBody Schema");
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// src/parse/v3/parse-parameters-path.ts
|
|
552
|
+
function parseParametersPath2(parameters) {
|
|
553
|
+
return parameters.reduce((r, p) => {
|
|
554
|
+
if (!("$ref" in p) && p.in === "path" && p.schema) {
|
|
555
|
+
const { type, ref, imports, enums } = parseSchemaType2(p.schema);
|
|
556
|
+
const parameter = new OperationParameter();
|
|
557
|
+
parameter.name = p.name;
|
|
558
|
+
parameter.description = p.description;
|
|
559
|
+
parameter.in = "path";
|
|
560
|
+
parameter.type = type;
|
|
561
|
+
parameter.ref = ref;
|
|
562
|
+
parameter.imports = imports || [];
|
|
563
|
+
parameter.enums = enums;
|
|
564
|
+
r.push(parameter);
|
|
565
|
+
}
|
|
566
|
+
return r;
|
|
567
|
+
}, []);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// src/parse/v3/parse-parameters-query.ts
|
|
571
|
+
function parseParametersQuery2(parameters) {
|
|
572
|
+
var _a, _b;
|
|
573
|
+
const excludeParams = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.excludeQueryParams;
|
|
574
|
+
return parameters.reduce((r, p) => {
|
|
575
|
+
if (!("$ref" in p) && p.in === "query" && p.schema && !(excludeParams && excludeParams.includes(p.name))) {
|
|
576
|
+
const { type, ref, imports, enums } = parseSchemaType2(p.schema);
|
|
577
|
+
const parameter = new OperationParameter();
|
|
578
|
+
parameter.name = p.name;
|
|
579
|
+
parameter.description = p.description;
|
|
580
|
+
parameter.in = "query";
|
|
581
|
+
parameter.type = type;
|
|
582
|
+
parameter.ref = ref;
|
|
583
|
+
parameter.required = p.required;
|
|
584
|
+
parameter.imports = imports || [];
|
|
585
|
+
parameter.enums = enums;
|
|
586
|
+
r.push(parameter);
|
|
587
|
+
}
|
|
588
|
+
return r;
|
|
589
|
+
}, []);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// src/parse/v3/parse-operation.ts
|
|
593
|
+
function parseOperation2(path5, method, operationObject) {
|
|
594
|
+
var _a;
|
|
595
|
+
const name = getOperationName(path5, method, operationObject);
|
|
596
|
+
const operation = new Operation(name, method, path5);
|
|
597
|
+
operation.description = operationObject.summary;
|
|
598
|
+
if (operationObject.requestBody) {
|
|
599
|
+
operation.parametersBody = parseParametersBody2(operationObject.requestBody);
|
|
600
|
+
}
|
|
601
|
+
if (operationObject.parameters) {
|
|
602
|
+
operation.parametersPath = parseParametersPath2(operationObject.parameters);
|
|
603
|
+
operation.parametersQuery = parseParametersQuery2(operationObject.parameters);
|
|
604
|
+
}
|
|
605
|
+
const responseSchema = parseResponseType2(operationObject.responses);
|
|
606
|
+
operation.imports = Array.from(
|
|
607
|
+
/* @__PURE__ */ new Set([
|
|
608
|
+
...((_a = operation.parametersBody) == null ? void 0 : _a.imports) || [],
|
|
609
|
+
...operation.parametersPath.flatMap((p) => p.imports) || [],
|
|
610
|
+
...operation.parametersQuery.flatMap((p) => p.imports) || [],
|
|
611
|
+
...(responseSchema == null ? void 0 : responseSchema.imports) || []
|
|
612
|
+
])
|
|
613
|
+
);
|
|
614
|
+
operation.responseRef = (responseSchema == null ? void 0 : responseSchema.ref) || "void";
|
|
615
|
+
return operation;
|
|
616
|
+
}
|
|
617
|
+
function parseResponseType2(responses) {
|
|
618
|
+
var _a, _b, _c, _d;
|
|
619
|
+
const SUCCESS_STATUS_CODE = "200";
|
|
620
|
+
const response = responses == null ? void 0 : responses[SUCCESS_STATUS_CODE];
|
|
621
|
+
if (response && "$ref" in response) {
|
|
622
|
+
return parseSchemaType2(responses);
|
|
623
|
+
}
|
|
624
|
+
if (response && "content" in response && ((_b = (_a = response == null ? void 0 : response.content) == null ? void 0 : _a["application/json"]) == null ? void 0 : _b.schema)) {
|
|
625
|
+
return parseSchemaType2((_d = (_c = response == null ? void 0 : response.content) == null ? void 0 : _c["application/json"]) == null ? void 0 : _d.schema);
|
|
626
|
+
}
|
|
627
|
+
return {
|
|
628
|
+
type: "void",
|
|
629
|
+
ref: "void",
|
|
630
|
+
imports: []
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// src/parse/v3/parse-service.ts
|
|
635
|
+
function parseService2(path5, method, operationObject, tags, services) {
|
|
636
|
+
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
637
|
+
const names = toNames(
|
|
638
|
+
getServiceName(
|
|
639
|
+
path5,
|
|
640
|
+
method,
|
|
641
|
+
operationObject,
|
|
642
|
+
tags || []
|
|
643
|
+
)
|
|
644
|
+
);
|
|
645
|
+
const operation = parseOperation2(path5, method, operationObject);
|
|
646
|
+
names.forEach((name) => {
|
|
647
|
+
let service = services.find((service2) => service2.name === name);
|
|
648
|
+
if (!service) {
|
|
649
|
+
service = new Service(name);
|
|
650
|
+
services.push(service);
|
|
651
|
+
}
|
|
652
|
+
if (service) {
|
|
653
|
+
service.operations.push(operation);
|
|
654
|
+
}
|
|
655
|
+
service.operations.flatMap((operation2) => operation2.imports).forEach((model) => {
|
|
656
|
+
if (service && !service.imports.includes(model)) {
|
|
657
|
+
service.imports.push(model);
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// src/parse/v3/parse-services.ts
|
|
664
|
+
function parseServices2(document) {
|
|
665
|
+
const services = [];
|
|
666
|
+
Object.entries(document.paths).forEach(([path5, pathObject]) => {
|
|
667
|
+
if (pathObject) {
|
|
668
|
+
Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
669
|
+
parseService2(
|
|
670
|
+
path5,
|
|
671
|
+
method,
|
|
672
|
+
operationObject,
|
|
673
|
+
document.tags || [],
|
|
674
|
+
services
|
|
675
|
+
);
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
return services;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// src/parse/v3/index.ts
|
|
683
|
+
function parseV3(document) {
|
|
684
|
+
const models = parseModels2(document);
|
|
685
|
+
const services = parseServices2(document);
|
|
686
|
+
return {
|
|
687
|
+
models,
|
|
688
|
+
services
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// src/template.ts
|
|
693
|
+
import * as fs from "node:fs";
|
|
694
|
+
import * as path2 from "node:path";
|
|
695
|
+
import Handlebars from "handlebars";
|
|
696
|
+
|
|
697
|
+
// src/template-helpers/equal.helper.ts
|
|
698
|
+
var equalHelper = {
|
|
699
|
+
name: "equal",
|
|
700
|
+
fn(v1, v2, options) {
|
|
701
|
+
if (v1 === v2) {
|
|
702
|
+
return options.fn(this);
|
|
703
|
+
} else {
|
|
704
|
+
return options.inverse(this);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
// src/template-helpers/is-array.helper.ts
|
|
710
|
+
var isArrayHelper = {
|
|
711
|
+
name: "is-array",
|
|
712
|
+
fn(v1, options) {
|
|
713
|
+
if (Array.isArray(v1)) {
|
|
714
|
+
return options.fn(this);
|
|
715
|
+
} else {
|
|
716
|
+
return options.inverse(this);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
// src/template-helpers/to-upper.helper.ts
|
|
722
|
+
var toUpperHelper = {
|
|
723
|
+
name: "to-upper",
|
|
724
|
+
fn(v1, onlyFirst) {
|
|
725
|
+
if (onlyFirst) {
|
|
726
|
+
return v1.replace(/^\S/, function(s) {
|
|
727
|
+
return s.toUpperCase();
|
|
728
|
+
});
|
|
729
|
+
} else {
|
|
730
|
+
return v1.toUpperCase();
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
// src/template.ts
|
|
736
|
+
function registerHandlebarTemplates() {
|
|
737
|
+
registerHandlebarPartials();
|
|
738
|
+
registerHandlebarHelpers();
|
|
739
|
+
}
|
|
740
|
+
function registerHandlebarPartials() {
|
|
741
|
+
registerHandlebarPartial("is-required");
|
|
742
|
+
registerHandlebarPartial("export-header");
|
|
743
|
+
registerHandlebarPartial("export-description");
|
|
744
|
+
registerHandlebarPartial("export-model-import");
|
|
745
|
+
registerHandlebarPartial("export-model-type");
|
|
746
|
+
registerHandlebarPartial("export-model-field");
|
|
747
|
+
registerHandlebarPartial("export-schema-type");
|
|
748
|
+
registerHandlebarPartial("export-service-import");
|
|
749
|
+
registerHandlebarPartial("export-service-class");
|
|
750
|
+
registerHandlebarPartial("export-service-namespace");
|
|
751
|
+
registerHandlebarPartial("export-service-namespace-type");
|
|
752
|
+
registerHandlebarPartial("export-service-operation");
|
|
753
|
+
registerHandlebarPartial("export-operation-params-path");
|
|
754
|
+
registerHandlebarPartial("export-operation-params-query");
|
|
755
|
+
registerHandlebarPartial("export-operation-params-body");
|
|
756
|
+
registerHandlebarPartial("export-operation-response");
|
|
757
|
+
}
|
|
758
|
+
function registerHandlebarHelpers() {
|
|
759
|
+
registerHandlebarHelper(equalHelper);
|
|
760
|
+
registerHandlebarHelper(isArrayHelper);
|
|
761
|
+
registerHandlebarHelper(toUpperHelper);
|
|
762
|
+
}
|
|
763
|
+
function registerHandlebarHelper(helper) {
|
|
764
|
+
Handlebars.registerHelper(helper.name, helper.fn);
|
|
765
|
+
}
|
|
766
|
+
function registerHandlebarPartial(input) {
|
|
767
|
+
const template = loadHandlebarTemplate(`partials/${input}`);
|
|
768
|
+
Handlebars.registerPartial(input, template);
|
|
769
|
+
}
|
|
770
|
+
function loadHandlebarTemplate(input) {
|
|
771
|
+
const templatePath = path2.resolve(__dirname, "templates", `${input}.hbs`);
|
|
772
|
+
return fs.readFileSync(templatePath, "utf-8");
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// src/generate/write-models.ts
|
|
776
|
+
import * as fs3 from "node:fs";
|
|
777
|
+
import * as path3 from "node:path";
|
|
778
|
+
|
|
779
|
+
// src/generate/write-model.ts
|
|
780
|
+
import Handlebars2 from "handlebars";
|
|
781
|
+
|
|
782
|
+
// src/generate/write-file.ts
|
|
783
|
+
import * as fs2 from "node:fs";
|
|
784
|
+
var ConsoleLogBlue = "\x1B[34m";
|
|
785
|
+
var ConsoleLogGreen = "\x1B[32m";
|
|
786
|
+
var ConsoleLogReset = "\x1B[0m";
|
|
787
|
+
function writeFile(output, content) {
|
|
788
|
+
var _a;
|
|
789
|
+
fs2.writeFileSync(output, content, "utf-8");
|
|
790
|
+
if ((_a = Generate.options) == null ? void 0 : _a.logger) {
|
|
791
|
+
console.log(
|
|
792
|
+
ConsoleLogBlue,
|
|
793
|
+
"Generate File: ",
|
|
794
|
+
ConsoleLogGreen,
|
|
795
|
+
output,
|
|
796
|
+
ConsoleLogReset
|
|
797
|
+
);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// src/generate/write-model.ts
|
|
802
|
+
function writeModel(model, output) {
|
|
803
|
+
const templateSource = loadHandlebarTemplate("export-model");
|
|
804
|
+
const template = Handlebars2.compile(templateSource);
|
|
805
|
+
const templateResult = template(model);
|
|
806
|
+
writeFile(output, templateResult);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// src/generate/write-models.ts
|
|
810
|
+
import rimraf from "rimraf";
|
|
811
|
+
function writeModels(client, options) {
|
|
812
|
+
if (!options.exportModels || !client.models) {
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
const output = path3.join(options.output, "models");
|
|
816
|
+
if (fs3.existsSync(output)) {
|
|
817
|
+
rimraf.sync(output);
|
|
818
|
+
}
|
|
819
|
+
fs3.mkdirSync(output, { recursive: true });
|
|
820
|
+
client.models.forEach((model) => {
|
|
821
|
+
const filename = `${model.name}.ts`;
|
|
822
|
+
writeModel(model, path3.join(output, filename));
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// src/generate/write-services.ts
|
|
827
|
+
import * as path4 from "node:path";
|
|
828
|
+
import * as fs4 from "node:fs";
|
|
829
|
+
import rimraf2 from "rimraf";
|
|
830
|
+
|
|
831
|
+
// src/generate/write-service.ts
|
|
832
|
+
import Handlebars3 from "handlebars";
|
|
833
|
+
function writeService(service, output) {
|
|
834
|
+
const templateSource = loadHandlebarTemplate("export-service");
|
|
835
|
+
const template = Handlebars3.compile(templateSource);
|
|
836
|
+
const templateResult = template(service);
|
|
837
|
+
writeFile(output, templateResult);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
// src/generate/write-services.ts
|
|
841
|
+
function writeServices(client, options) {
|
|
842
|
+
if (!client.services) {
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
const output = path4.join(options.output, "services");
|
|
846
|
+
if (fs4.existsSync(output)) {
|
|
847
|
+
rimraf2.sync(output);
|
|
848
|
+
}
|
|
849
|
+
fs4.mkdirSync(output, { recursive: true });
|
|
850
|
+
client.services.forEach((service) => {
|
|
851
|
+
const filename = `${service.name}Service.ts`;
|
|
852
|
+
writeService(service, path4.join(output, filename));
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// src/generate/index.ts
|
|
857
|
+
var _Generate = class {
|
|
858
|
+
static startup(options) {
|
|
859
|
+
_Generate.options = options;
|
|
860
|
+
registerHandlebarTemplates();
|
|
861
|
+
const applicationOptions = generateServiceOptions(options);
|
|
862
|
+
return Promise.all(
|
|
863
|
+
applicationOptions.map(
|
|
864
|
+
(applicationOption) => _Generate.generateApplication(applicationOption)
|
|
865
|
+
)
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
static async generateApplication(options) {
|
|
869
|
+
const document = await getOpenApiDocument(
|
|
870
|
+
options.input
|
|
871
|
+
);
|
|
872
|
+
const version = getOpenAPIVersion(document);
|
|
873
|
+
const client = _Generate.generateClient(document, version);
|
|
874
|
+
_Generate.writeClient(client, options);
|
|
875
|
+
}
|
|
876
|
+
static generateClient(document, version) {
|
|
877
|
+
switch (version) {
|
|
878
|
+
case 2 /* V2 */:
|
|
879
|
+
return parseV2(document);
|
|
880
|
+
case 3 /* V3 */:
|
|
881
|
+
return parseV3(document);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
static writeClient(client, options) {
|
|
885
|
+
writeModels(client, options);
|
|
886
|
+
writeServices(client, options);
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
var Generate = _Generate;
|
|
890
|
+
__publicField(Generate, "options");
|
|
891
|
+
|
|
892
|
+
// src/index.ts
|
|
893
|
+
var src_default = Generate.startup;
|
|
894
|
+
export {
|
|
895
|
+
src_default as default
|
|
896
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }require('../chunk-XXPGZHWZ.js');
|
|
2
|
+
|
|
3
|
+
// src/vite-plugin/index.ts
|
|
4
|
+
var _fs = require('fs'); var fs = _interopRequireWildcard(_fs);
|
|
5
|
+
var _path = require('path'); var path = _interopRequireWildcard(_path);
|
|
6
|
+
var _handlebars = require('handlebars'); var Handlebars = _interopRequireWildcard(_handlebars);
|
|
7
|
+
|
|
8
|
+
// src/vite-plugin/templates.ts
|
|
9
|
+
var useRequestFunctionCodeTemplate = `
|
|
10
|
+
export function useRequest(
|
|
11
|
+
select
|
|
12
|
+
) {
|
|
13
|
+
const service = select(serviceMap)
|
|
14
|
+
return new service()
|
|
15
|
+
}
|
|
16
|
+
`;
|
|
17
|
+
var useRequestFunctionTypeTemplate = `
|
|
18
|
+
export function useRequest<T>(
|
|
19
|
+
select: (services: typeof serviceMap) => { new (): T }
|
|
20
|
+
): T
|
|
21
|
+
`;
|
|
22
|
+
var importServiceTemplate = `
|
|
23
|
+
{{#if groups}}
|
|
24
|
+
{{#each groups}}
|
|
25
|
+
{{#each services}}
|
|
26
|
+
import { {{{name}}} as {{group}}_{{name}} } from '{{path}}'
|
|
27
|
+
{{/each}}
|
|
28
|
+
{{/each}}
|
|
29
|
+
{{else}}
|
|
30
|
+
{{#each services}}
|
|
31
|
+
import { {{{name}}} } from '{{path}}'
|
|
32
|
+
{{/each}}
|
|
33
|
+
{{/if}}
|
|
34
|
+
`;
|
|
35
|
+
var defineServiceMapTemplate = `
|
|
36
|
+
{{#if groups}}
|
|
37
|
+
const serviceMap = {
|
|
38
|
+
{{#each groups}}
|
|
39
|
+
{{{name}}}: {
|
|
40
|
+
{{#each services}}
|
|
41
|
+
{{{name}}}: {{group}}_{{name}},
|
|
42
|
+
{{/each}}
|
|
43
|
+
},
|
|
44
|
+
{{/each}}
|
|
45
|
+
}
|
|
46
|
+
{{else}}
|
|
47
|
+
const serviceMap = {
|
|
48
|
+
{{#each services}}
|
|
49
|
+
{{{name}}},
|
|
50
|
+
{{/each}}
|
|
51
|
+
}
|
|
52
|
+
{{/if}}
|
|
53
|
+
`;
|
|
54
|
+
var generateCodeTemplate = `
|
|
55
|
+
${importServiceTemplate.trim()}
|
|
56
|
+
|
|
57
|
+
${defineServiceMapTemplate.trim()}
|
|
58
|
+
|
|
59
|
+
${useRequestFunctionCodeTemplate.trim()}
|
|
60
|
+
`;
|
|
61
|
+
var generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
|
|
62
|
+
${importServiceTemplate.trim()}
|
|
63
|
+
|
|
64
|
+
${defineServiceMapTemplate.trim()}
|
|
65
|
+
|
|
66
|
+
${useRequestFunctionTypeTemplate.trim()}
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
|
|
70
|
+
// src/vite-plugin/index.ts
|
|
71
|
+
var MODULE_ID = "virtual:request";
|
|
72
|
+
var DECLARATION_FILE = "request.d.ts";
|
|
73
|
+
var viteConfig;
|
|
74
|
+
var vite_plugin_default = (options) => {
|
|
75
|
+
return {
|
|
76
|
+
name: "vite-plugin-request",
|
|
77
|
+
enforce: "pre",
|
|
78
|
+
resolveId(id) {
|
|
79
|
+
return id === MODULE_ID ? MODULE_ID : void 0;
|
|
80
|
+
},
|
|
81
|
+
configResolved(config) {
|
|
82
|
+
viteConfig = config;
|
|
83
|
+
},
|
|
84
|
+
load(id) {
|
|
85
|
+
if (id !== MODULE_ID)
|
|
86
|
+
return;
|
|
87
|
+
const paths = getServicePaths(options);
|
|
88
|
+
const services = getServiceItems(paths);
|
|
89
|
+
const groups = getServiceGroups(services);
|
|
90
|
+
if (services && services.length) {
|
|
91
|
+
generateDeclare(services, groups, options);
|
|
92
|
+
return generateCode(services, groups);
|
|
93
|
+
} else {
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
function getServicePaths(options) {
|
|
100
|
+
const paths = [];
|
|
101
|
+
const walk = (dir) => {
|
|
102
|
+
fs.readdirSync(dir).forEach(function(file) {
|
|
103
|
+
const fullpath = path.join(dir, file).replace(/\\/g, "/");
|
|
104
|
+
const stat = fs.statSync(fullpath);
|
|
105
|
+
if (stat.isFile() && fullpath.endsWith("Service.ts")) {
|
|
106
|
+
paths.push(fullpath);
|
|
107
|
+
} else if (stat.isDirectory()) {
|
|
108
|
+
const subdir = path.join(dir, file);
|
|
109
|
+
walk(subdir);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
const { replacement } = viteConfig.resolve.alias.find(
|
|
114
|
+
(alias) => alias.find === options.alias
|
|
115
|
+
);
|
|
116
|
+
if (fs.existsSync(path.resolve(options.dir))) {
|
|
117
|
+
walk(path.resolve(options.dir));
|
|
118
|
+
}
|
|
119
|
+
return paths.map(
|
|
120
|
+
(filepath) => filepath.replace(replacement.replace(/\\/g, "/"), options.alias)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
function getServiceItems(paths) {
|
|
124
|
+
const toCaseString = (str = "") => str.replace(/-(\w)/g, (_, $1) => $1.toUpperCase()).replace(/^\S/, (s) => s.toUpperCase());
|
|
125
|
+
return paths.map((filePath) => {
|
|
126
|
+
const [name] = /[^\\]+(?=\.ts$)/.exec(toCaseString(path.basename(filePath))) || [];
|
|
127
|
+
const [group] = /(?<=^.\/http\/)(.*?)(?=\/.*?Service\.ts$)/.exec(filePath) || [];
|
|
128
|
+
return {
|
|
129
|
+
name: toCaseString(name),
|
|
130
|
+
group: group !== "services" ? toCaseString(group) : void 0,
|
|
131
|
+
path: filePath.replace(/\.ts$/g, "")
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function getServiceGroups(services) {
|
|
136
|
+
return services.reduce((r, s) => {
|
|
137
|
+
if (!s.group) {
|
|
138
|
+
return r;
|
|
139
|
+
}
|
|
140
|
+
const group = r.find((x) => x.name === s.group) || {
|
|
141
|
+
name: s.group,
|
|
142
|
+
services: []
|
|
143
|
+
};
|
|
144
|
+
group.services.push(s);
|
|
145
|
+
if (!r.find((g) => g === group)) {
|
|
146
|
+
r.push(group);
|
|
147
|
+
}
|
|
148
|
+
return r;
|
|
149
|
+
}, []);
|
|
150
|
+
}
|
|
151
|
+
function generateCode(services, groups) {
|
|
152
|
+
const template = Handlebars.compile(generateCodeTemplate);
|
|
153
|
+
return template({
|
|
154
|
+
groups: groups.length ? groups : void 0,
|
|
155
|
+
services
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function generateDeclare(services, groups, options) {
|
|
159
|
+
const template = Handlebars.compile(generateDeclareTemplate);
|
|
160
|
+
const content = template({
|
|
161
|
+
groups: groups.length ? groups : void 0,
|
|
162
|
+
services,
|
|
163
|
+
MODULE_ID
|
|
164
|
+
});
|
|
165
|
+
fs.writeFileSync(
|
|
166
|
+
path.resolve(viteConfig.root, _nullishCoalesce(options.dts, () => ( DECLARATION_FILE))),
|
|
167
|
+
content.replace(/\r\n/g, "\n"),
|
|
168
|
+
"utf-8"
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
exports.default = vite_plugin_default;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import "../chunk-7BMEVKSG.mjs";
|
|
2
|
+
|
|
3
|
+
// src/vite-plugin/index.ts
|
|
4
|
+
import * as fs from "fs";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import * as Handlebars from "handlebars";
|
|
7
|
+
|
|
8
|
+
// src/vite-plugin/templates.ts
|
|
9
|
+
var useRequestFunctionCodeTemplate = `
|
|
10
|
+
export function useRequest(
|
|
11
|
+
select
|
|
12
|
+
) {
|
|
13
|
+
const service = select(serviceMap)
|
|
14
|
+
return new service()
|
|
15
|
+
}
|
|
16
|
+
`;
|
|
17
|
+
var useRequestFunctionTypeTemplate = `
|
|
18
|
+
export function useRequest<T>(
|
|
19
|
+
select: (services: typeof serviceMap) => { new (): T }
|
|
20
|
+
): T
|
|
21
|
+
`;
|
|
22
|
+
var importServiceTemplate = `
|
|
23
|
+
{{#if groups}}
|
|
24
|
+
{{#each groups}}
|
|
25
|
+
{{#each services}}
|
|
26
|
+
import { {{{name}}} as {{group}}_{{name}} } from '{{path}}'
|
|
27
|
+
{{/each}}
|
|
28
|
+
{{/each}}
|
|
29
|
+
{{else}}
|
|
30
|
+
{{#each services}}
|
|
31
|
+
import { {{{name}}} } from '{{path}}'
|
|
32
|
+
{{/each}}
|
|
33
|
+
{{/if}}
|
|
34
|
+
`;
|
|
35
|
+
var defineServiceMapTemplate = `
|
|
36
|
+
{{#if groups}}
|
|
37
|
+
const serviceMap = {
|
|
38
|
+
{{#each groups}}
|
|
39
|
+
{{{name}}}: {
|
|
40
|
+
{{#each services}}
|
|
41
|
+
{{{name}}}: {{group}}_{{name}},
|
|
42
|
+
{{/each}}
|
|
43
|
+
},
|
|
44
|
+
{{/each}}
|
|
45
|
+
}
|
|
46
|
+
{{else}}
|
|
47
|
+
const serviceMap = {
|
|
48
|
+
{{#each services}}
|
|
49
|
+
{{{name}}},
|
|
50
|
+
{{/each}}
|
|
51
|
+
}
|
|
52
|
+
{{/if}}
|
|
53
|
+
`;
|
|
54
|
+
var generateCodeTemplate = `
|
|
55
|
+
${importServiceTemplate.trim()}
|
|
56
|
+
|
|
57
|
+
${defineServiceMapTemplate.trim()}
|
|
58
|
+
|
|
59
|
+
${useRequestFunctionCodeTemplate.trim()}
|
|
60
|
+
`;
|
|
61
|
+
var generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
|
|
62
|
+
${importServiceTemplate.trim()}
|
|
63
|
+
|
|
64
|
+
${defineServiceMapTemplate.trim()}
|
|
65
|
+
|
|
66
|
+
${useRequestFunctionTypeTemplate.trim()}
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
|
|
70
|
+
// src/vite-plugin/index.ts
|
|
71
|
+
var MODULE_ID = "virtual:request";
|
|
72
|
+
var DECLARATION_FILE = "request.d.ts";
|
|
73
|
+
var viteConfig;
|
|
74
|
+
var vite_plugin_default = (options) => {
|
|
75
|
+
return {
|
|
76
|
+
name: "vite-plugin-request",
|
|
77
|
+
enforce: "pre",
|
|
78
|
+
resolveId(id) {
|
|
79
|
+
return id === MODULE_ID ? MODULE_ID : void 0;
|
|
80
|
+
},
|
|
81
|
+
configResolved(config) {
|
|
82
|
+
viteConfig = config;
|
|
83
|
+
},
|
|
84
|
+
load(id) {
|
|
85
|
+
if (id !== MODULE_ID)
|
|
86
|
+
return;
|
|
87
|
+
const paths = getServicePaths(options);
|
|
88
|
+
const services = getServiceItems(paths);
|
|
89
|
+
const groups = getServiceGroups(services);
|
|
90
|
+
if (services && services.length) {
|
|
91
|
+
generateDeclare(services, groups, options);
|
|
92
|
+
return generateCode(services, groups);
|
|
93
|
+
} else {
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
function getServicePaths(options) {
|
|
100
|
+
const paths = [];
|
|
101
|
+
const walk = (dir) => {
|
|
102
|
+
fs.readdirSync(dir).forEach(function(file) {
|
|
103
|
+
const fullpath = path.join(dir, file).replace(/\\/g, "/");
|
|
104
|
+
const stat = fs.statSync(fullpath);
|
|
105
|
+
if (stat.isFile() && fullpath.endsWith("Service.ts")) {
|
|
106
|
+
paths.push(fullpath);
|
|
107
|
+
} else if (stat.isDirectory()) {
|
|
108
|
+
const subdir = path.join(dir, file);
|
|
109
|
+
walk(subdir);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
const { replacement } = viteConfig.resolve.alias.find(
|
|
114
|
+
(alias) => alias.find === options.alias
|
|
115
|
+
);
|
|
116
|
+
if (fs.existsSync(path.resolve(options.dir))) {
|
|
117
|
+
walk(path.resolve(options.dir));
|
|
118
|
+
}
|
|
119
|
+
return paths.map(
|
|
120
|
+
(filepath) => filepath.replace(replacement.replace(/\\/g, "/"), options.alias)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
function getServiceItems(paths) {
|
|
124
|
+
const toCaseString = (str = "") => str.replace(/-(\w)/g, (_, $1) => $1.toUpperCase()).replace(/^\S/, (s) => s.toUpperCase());
|
|
125
|
+
return paths.map((filePath) => {
|
|
126
|
+
const [name] = /[^\\]+(?=\.ts$)/.exec(toCaseString(path.basename(filePath))) || [];
|
|
127
|
+
const [group] = /(?<=^.\/http\/)(.*?)(?=\/.*?Service\.ts$)/.exec(filePath) || [];
|
|
128
|
+
return {
|
|
129
|
+
name: toCaseString(name),
|
|
130
|
+
group: group !== "services" ? toCaseString(group) : void 0,
|
|
131
|
+
path: filePath.replace(/\.ts$/g, "")
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function getServiceGroups(services) {
|
|
136
|
+
return services.reduce((r, s) => {
|
|
137
|
+
if (!s.group) {
|
|
138
|
+
return r;
|
|
139
|
+
}
|
|
140
|
+
const group = r.find((x) => x.name === s.group) || {
|
|
141
|
+
name: s.group,
|
|
142
|
+
services: []
|
|
143
|
+
};
|
|
144
|
+
group.services.push(s);
|
|
145
|
+
if (!r.find((g) => g === group)) {
|
|
146
|
+
r.push(group);
|
|
147
|
+
}
|
|
148
|
+
return r;
|
|
149
|
+
}, []);
|
|
150
|
+
}
|
|
151
|
+
function generateCode(services, groups) {
|
|
152
|
+
const template = Handlebars.compile(generateCodeTemplate);
|
|
153
|
+
return template({
|
|
154
|
+
groups: groups.length ? groups : void 0,
|
|
155
|
+
services
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function generateDeclare(services, groups, options) {
|
|
159
|
+
const template = Handlebars.compile(generateDeclareTemplate);
|
|
160
|
+
const content = template({
|
|
161
|
+
groups: groups.length ? groups : void 0,
|
|
162
|
+
services,
|
|
163
|
+
MODULE_ID
|
|
164
|
+
});
|
|
165
|
+
fs.writeFileSync(
|
|
166
|
+
path.resolve(viteConfig.root, options.dts ?? DECLARATION_FILE),
|
|
167
|
+
content.replace(/\r\n/g, "\n"),
|
|
168
|
+
"utf-8"
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
export {
|
|
172
|
+
vite_plugin_default as default
|
|
173
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gopowerteam/request-generate",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -23,6 +23,11 @@
|
|
|
23
23
|
"require": "./dist/index.js",
|
|
24
24
|
"import": "./dist/index.mjs",
|
|
25
25
|
"types": "./dist/index.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"./vite-plugin": {
|
|
28
|
+
"require": "./dist/vite-plugin/index.js",
|
|
29
|
+
"import": "./dist/vite-plugin/index.mjs",
|
|
30
|
+
"types": "./dist/vite-plugin/index.d.ts"
|
|
26
31
|
}
|
|
27
32
|
},
|
|
28
33
|
"devDependencies": {
|
|
@@ -38,7 +43,8 @@
|
|
|
38
43
|
"ts-jest": "^29.0.3",
|
|
39
44
|
"ts-node": "^10.9.1",
|
|
40
45
|
"tsup": "^6.2.3",
|
|
41
|
-
"typescript": "^4.8.4"
|
|
46
|
+
"typescript": "^4.8.4",
|
|
47
|
+
"vite": "^3.1.8"
|
|
42
48
|
},
|
|
43
49
|
"dependencies": {
|
|
44
50
|
"@apidevtools/swagger-parser": "^10.1.0",
|