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