@gopowerteam/request-generate 0.1.25 → 0.2.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/download.mts +67 -0
- package/bin/generate.mts +68 -0
- package/bin/index.mts +0 -0
- package/dist/{chunk-3T6NC7NX.mjs → chunk-WF3XBEPN.mjs} +1 -1
- package/dist/index.d.ts +67 -60
- package/dist/index.js +170 -72
- package/dist/index.mjs +178 -80
- package/dist/templates/partials/export-service-namespace.hbs +2 -0
- package/dist/vite-plugin/index.d.ts +2 -2
- package/dist/vite-plugin/index.js +24 -13
- package/dist/vite-plugin/index.mjs +25 -14
- package/package.json +4 -2
package/bin/download.mts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import fs from 'node:fs'
|
|
5
|
+
import { program } from 'commander'
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = path.dirname(__filename);
|
|
10
|
+
const RequestGenerate = await import(`file://${path.resolve(
|
|
11
|
+
__dirname,
|
|
12
|
+
'..',
|
|
13
|
+
'dist',
|
|
14
|
+
'index.mjs'
|
|
15
|
+
)}`)
|
|
16
|
+
|
|
17
|
+
const params = program
|
|
18
|
+
.name('@gopowerteam/request-generate')
|
|
19
|
+
.usage('[options]')
|
|
20
|
+
.option('--config <value>', '指定配置文件位置')
|
|
21
|
+
.parse(process.argv)
|
|
22
|
+
.opts()
|
|
23
|
+
|
|
24
|
+
const configFilePaths = [
|
|
25
|
+
'request.config.ts',
|
|
26
|
+
'request-generate.config.cjs',
|
|
27
|
+
'request-generate.config.js'
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 加载配置文件
|
|
32
|
+
* @param {*} filePath
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
async function loadConfigFile(filePath) {
|
|
36
|
+
if (filePath) {
|
|
37
|
+
configFilePaths.unshift(filePath)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const configFilePath = configFilePaths.find((file) =>
|
|
41
|
+
fs.existsSync(path.resolve(process.cwd(), file))
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
if(!configFilePath){
|
|
45
|
+
throw new Error("Not Found Config File.")
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (configFilePath.endsWith('.js') || configFilePath.endsWith('.ts')) {
|
|
49
|
+
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
50
|
+
} else {
|
|
51
|
+
throw new Error('无法找到RequestGenerate配置文件')
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (RequestGenerate) {
|
|
56
|
+
const { default: config } = await loadConfigFile(params.config)
|
|
57
|
+
|
|
58
|
+
RequestGenerate.download(config)
|
|
59
|
+
.then(() => {
|
|
60
|
+
console.log('接口文件更新完成')
|
|
61
|
+
process.exit(0)
|
|
62
|
+
})
|
|
63
|
+
.catch((error) => {
|
|
64
|
+
console.error(error)
|
|
65
|
+
process.exit(1)
|
|
66
|
+
})
|
|
67
|
+
}
|
package/bin/generate.mts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import fs from 'node:fs'
|
|
5
|
+
import { program } from 'commander'
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = path.dirname(__filename);
|
|
10
|
+
const RequestGenerate = await import(`file://${path.resolve(
|
|
11
|
+
__dirname,
|
|
12
|
+
'..',
|
|
13
|
+
'dist',
|
|
14
|
+
'index.mjs'
|
|
15
|
+
)}`)
|
|
16
|
+
|
|
17
|
+
const params = program
|
|
18
|
+
.name('@gopowerteam/request-generate')
|
|
19
|
+
.usage('[options]')
|
|
20
|
+
.option('--config <value>', '指定配置文件位置')
|
|
21
|
+
.parse(process.argv)
|
|
22
|
+
.opts()
|
|
23
|
+
|
|
24
|
+
const configFilePaths = [
|
|
25
|
+
'request.config.ts',
|
|
26
|
+
'request-generate.config.cjs',
|
|
27
|
+
'request-generate.config.js'
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 加载配置文件
|
|
32
|
+
* @param {*} filePath
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
async function loadConfigFile(filePath) {
|
|
36
|
+
if (filePath) {
|
|
37
|
+
configFilePaths.unshift(filePath)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const configFilePath = configFilePaths.find((file) =>
|
|
41
|
+
fs.existsSync(path.resolve(process.cwd(), file))
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
if(!configFilePath){
|
|
45
|
+
throw new Error("Not Find Config File.")
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (configFilePath.endsWith('js')) {
|
|
49
|
+
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
50
|
+
} else if (configFilePath.endsWith('ts')) {
|
|
51
|
+
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
52
|
+
} else {
|
|
53
|
+
throw new Error('无法找到RequestGenerate配置文件')
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (RequestGenerate) {
|
|
58
|
+
const { default: config } = await loadConfigFile(params.config)
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
await RequestGenerate.generate(config)
|
|
62
|
+
console.log('接口文件生成完成')
|
|
63
|
+
process.exit(0)
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error(error)
|
|
66
|
+
process.exit(1)
|
|
67
|
+
}
|
|
68
|
+
}
|
package/bin/index.mts
CHANGED
|
File without changes
|
|
@@ -5,7 +5,7 @@ var __publicField = (obj, key, value) => {
|
|
|
5
5
|
return value;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
-
// ../../node_modules/.pnpm/tsup@6.6.3_postcss@8.
|
|
8
|
+
// ../../node_modules/.pnpm/tsup@6.6.3_postcss@8.5.3_ts-node@10.9.1_@types+node@20.16.5_typescript@4.9.5__typescript@4.9.5/node_modules/tsup/assets/esm_shims.js
|
|
9
9
|
import { fileURLToPath } from "url";
|
|
10
10
|
import path from "path";
|
|
11
11
|
var getFilename = () => fileURLToPath(import.meta.url);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
1
|
import { OpenAPIV2, OpenAPIV3 } from 'openapi-types';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* 生成全局选项
|
|
5
|
+
*/
|
|
6
|
+
interface 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, ApplicationConfig>
|
|
35
|
+
// 追加service
|
|
36
|
+
appendService?: boolean
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type ApplicationConfig =
|
|
40
|
+
| {
|
|
41
|
+
key: string
|
|
42
|
+
openapi: string
|
|
43
|
+
}
|
|
44
|
+
| string
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 生成应用选项
|
|
48
|
+
*/
|
|
49
|
+
type GenerateApplicationOptions = Pick<
|
|
50
|
+
GenerateOptions,
|
|
51
|
+
'exportModels' | 'output'
|
|
52
|
+
> & {
|
|
53
|
+
// 服务名称
|
|
54
|
+
name?: string
|
|
55
|
+
// 应用名称
|
|
56
|
+
application?: string
|
|
57
|
+
// OPENAPI地址
|
|
58
|
+
input: string
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
declare class Download {
|
|
62
|
+
static options: GenerateOptions;
|
|
63
|
+
static startup(options: GenerateOptions): Promise<void>;
|
|
64
|
+
static downloadOpenAPIFile(option: GenerateApplicationOptions): Promise<unknown>;
|
|
65
|
+
}
|
|
66
|
+
|
|
3
67
|
declare enum OpenAPIVersion {
|
|
4
68
|
V2 = 2,
|
|
5
69
|
V3 = 3
|
|
@@ -64,64 +128,6 @@ interface GenerateClient {
|
|
|
64
128
|
services: Service[]
|
|
65
129
|
}
|
|
66
130
|
|
|
67
|
-
/**
|
|
68
|
-
* 生成全局选项
|
|
69
|
-
*/
|
|
70
|
-
interface GenerateOptions {
|
|
71
|
-
// 网关地址
|
|
72
|
-
gateway: string
|
|
73
|
-
// OpenAPI地址
|
|
74
|
-
openapi: string
|
|
75
|
-
// 输出目录
|
|
76
|
-
output: string
|
|
77
|
-
// 输出Model路径
|
|
78
|
-
exportModels: boolean
|
|
79
|
-
// 开启日志输出
|
|
80
|
-
logger?: boolean
|
|
81
|
-
// 输出Model路径
|
|
82
|
-
exportServices?: {
|
|
83
|
-
serviceResolve?: (options: {
|
|
84
|
-
path: string
|
|
85
|
-
method: string
|
|
86
|
-
object: OpenAPIV2.OperationObject | OpenAPIV3.OperationObject
|
|
87
|
-
tags: OpenAPIV2.TagObject[]
|
|
88
|
-
}) => string
|
|
89
|
-
operationResolve?: (options: {
|
|
90
|
-
path: string
|
|
91
|
-
method: string
|
|
92
|
-
object: OpenAPIV2.OperationObject | OpenAPIV3.OperationObject
|
|
93
|
-
}) => string
|
|
94
|
-
excludeQueryParams?: string[]
|
|
95
|
-
responseType?: 'promise' | 'observable'
|
|
96
|
-
}
|
|
97
|
-
// 多应用列表
|
|
98
|
-
applications?: Record<string, ApplicationConfig>
|
|
99
|
-
// 追加service
|
|
100
|
-
appendService?: boolean
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
type ApplicationConfig =
|
|
104
|
-
| {
|
|
105
|
-
key: string
|
|
106
|
-
openapi: string
|
|
107
|
-
}
|
|
108
|
-
| string
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 生成应用选项
|
|
112
|
-
*/
|
|
113
|
-
type GenerateApplicationOptions = Pick<
|
|
114
|
-
GenerateOptions,
|
|
115
|
-
'exportModels' | 'output'
|
|
116
|
-
> & {
|
|
117
|
-
// 服务名称
|
|
118
|
-
name?: string
|
|
119
|
-
// 应用名称
|
|
120
|
-
application?: string
|
|
121
|
-
// OPENAPI地址
|
|
122
|
-
input: string
|
|
123
|
-
}
|
|
124
|
-
|
|
125
131
|
type UnkownVersionDocument = OpenAPIV3.Document & OpenAPIV2.Document;
|
|
126
132
|
declare class Generate {
|
|
127
133
|
static options: GenerateOptions;
|
|
@@ -153,6 +159,7 @@ declare class Generate {
|
|
|
153
159
|
|
|
154
160
|
declare function defineConfig(config: GenerateOptions): GenerateOptions;
|
|
155
161
|
|
|
156
|
-
declare const
|
|
162
|
+
declare const generate: typeof Generate.startup;
|
|
163
|
+
declare const download: typeof Download.startup;
|
|
157
164
|
|
|
158
|
-
export { GenerateOptions,
|
|
165
|
+
export { GenerateOptions, defineConfig, download, generate };
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,73 @@
|
|
|
2
2
|
|
|
3
3
|
var _chunkXXPGZHWZjs = require('./chunk-XXPGZHWZ.js');
|
|
4
4
|
|
|
5
|
+
// src/download/index.ts
|
|
6
|
+
var _fs = require('fs'); var fs = _interopRequireWildcard(_fs); var fs2 = _interopRequireWildcard(_fs); var fs5 = _interopRequireWildcard(_fs); var fs4 = _interopRequireWildcard(_fs); var fs6 = _interopRequireWildcard(_fs);
|
|
7
|
+
var _path = require('path'); var path2 = _interopRequireWildcard(_path); var path3 = _interopRequireWildcard(_path); var path5 = _interopRequireWildcard(_path); var path6 = _interopRequireWildcard(_path);
|
|
8
|
+
|
|
9
|
+
// src/utils/get-services-options.ts
|
|
10
|
+
|
|
11
|
+
function createOptions(options, name, application) {
|
|
12
|
+
const { service, openapi } = (() => {
|
|
13
|
+
if (!application) {
|
|
14
|
+
return {
|
|
15
|
+
service: "",
|
|
16
|
+
openapi: options.openapi
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
if (typeof application === "string") {
|
|
20
|
+
return {
|
|
21
|
+
service: application,
|
|
22
|
+
openapi: options.openapi
|
|
23
|
+
};
|
|
24
|
+
} else {
|
|
25
|
+
return {
|
|
26
|
+
service: application.key,
|
|
27
|
+
openapi: application.openapi
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
})();
|
|
31
|
+
return {
|
|
32
|
+
name,
|
|
33
|
+
application: service,
|
|
34
|
+
input: `${options.gateway}/${service}/${openapi}`.replace(/\/{2,3}/g, "/"),
|
|
35
|
+
output: name ? path2.default.join(options.output, name) : options.output,
|
|
36
|
+
exportModels: options.exportModels
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function generateServiceOptions(options) {
|
|
40
|
+
if (options.applications && Object.keys(options.applications).length) {
|
|
41
|
+
return Object.entries(options.applications).map(
|
|
42
|
+
([name, application]) => createOptions(options, name, application)
|
|
43
|
+
);
|
|
44
|
+
} else {
|
|
45
|
+
return [createOptions(options)];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/download/index.ts
|
|
50
|
+
var DefaultDownloadDir = ".request";
|
|
51
|
+
var _Download = class {
|
|
52
|
+
static async startup(options) {
|
|
53
|
+
const applicationOptions = generateServiceOptions(options);
|
|
54
|
+
if (!fs.existsSync(DefaultDownloadDir)) {
|
|
55
|
+
fs.mkdirSync(DefaultDownloadDir);
|
|
56
|
+
}
|
|
57
|
+
await Promise.all(
|
|
58
|
+
applicationOptions.map((options2) => _Download.downloadOpenAPIFile(options2))
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
static async downloadOpenAPIFile(option) {
|
|
62
|
+
const response = await fetch(option.input);
|
|
63
|
+
const data = await response.json();
|
|
64
|
+
const filePath = path2.join(".request", `${option.name}.json`);
|
|
65
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
|
66
|
+
return data;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var Download = _Download;
|
|
70
|
+
_chunkXXPGZHWZjs.__publicField.call(void 0, Download, "options");
|
|
71
|
+
|
|
5
72
|
// src/generate/index.ts
|
|
6
73
|
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
7
74
|
|
|
@@ -206,11 +273,11 @@ var Service = (_class = class {
|
|
|
206
273
|
}, _class);
|
|
207
274
|
|
|
208
275
|
// src/utils/get-service-name.ts
|
|
209
|
-
function getServiceName(
|
|
276
|
+
function getServiceName(path7, method, operationObject, tags) {
|
|
210
277
|
var _a, _b, _c;
|
|
211
278
|
const resolve2 = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.serviceResolve;
|
|
212
279
|
if (resolve2) {
|
|
213
|
-
return resolve2({ path:
|
|
280
|
+
return resolve2({ path: path7, method, object: operationObject, tags });
|
|
214
281
|
} else {
|
|
215
282
|
return ((_c = operationObject.tags) == null ? void 0 : _c.map((tag) => getCamelName(tag))) || "Default";
|
|
216
283
|
}
|
|
@@ -238,21 +305,21 @@ var Operation = (_class2 = class {
|
|
|
238
305
|
|
|
239
306
|
// 导入操作
|
|
240
307
|
__init5() {this.imports = []}
|
|
241
|
-
constructor(name, method,
|
|
308
|
+
constructor(name, method, path7) {;_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);_class2.prototype.__init5.call(this);
|
|
242
309
|
var _a, _b;
|
|
243
310
|
this.name = name;
|
|
244
311
|
this.method = method;
|
|
245
|
-
this.path =
|
|
312
|
+
this.path = path7;
|
|
246
313
|
this.responseType = ((_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.responseType) || "promise";
|
|
247
314
|
}
|
|
248
315
|
}, _class2);
|
|
249
316
|
|
|
250
317
|
// src/utils/get-operation-name.ts
|
|
251
|
-
function getOperationName(
|
|
318
|
+
function getOperationName(path7, method, operationObject) {
|
|
252
319
|
var _a, _b;
|
|
253
320
|
const resolve2 = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.operationResolve;
|
|
254
321
|
if (resolve2) {
|
|
255
|
-
return resolve2({ path:
|
|
322
|
+
return resolve2({ path: path7, method, object: operationObject });
|
|
256
323
|
} else {
|
|
257
324
|
return operationObject.operationId || "";
|
|
258
325
|
}
|
|
@@ -345,10 +412,10 @@ function parseParametersQuery(parameters) {
|
|
|
345
412
|
}
|
|
346
413
|
|
|
347
414
|
// src/parse/v2/parse-operation.ts
|
|
348
|
-
function parseOperation(
|
|
415
|
+
function parseOperation(path7, method, operationObject) {
|
|
349
416
|
var _a;
|
|
350
|
-
const name = getOperationName(
|
|
351
|
-
const operation = new Operation(name, method,
|
|
417
|
+
const name = getOperationName(path7, method, operationObject);
|
|
418
|
+
const operation = new Operation(name, method, path7);
|
|
352
419
|
operation.description = operationObject.summary || operation.description;
|
|
353
420
|
if (operationObject.parameters) {
|
|
354
421
|
operation.parametersBody = parseParametersBody(operationObject.parameters);
|
|
@@ -384,17 +451,17 @@ function parseResponseType(responses) {
|
|
|
384
451
|
}
|
|
385
452
|
|
|
386
453
|
// src/parse/v2/parse-service.ts
|
|
387
|
-
function parseService(
|
|
454
|
+
function parseService(path7, method, operationObject, tags, services) {
|
|
388
455
|
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
389
456
|
const names = toNames(
|
|
390
457
|
getServiceName(
|
|
391
|
-
|
|
458
|
+
path7,
|
|
392
459
|
method,
|
|
393
460
|
operationObject,
|
|
394
461
|
tags || []
|
|
395
462
|
)
|
|
396
463
|
);
|
|
397
|
-
const operation = parseOperation(
|
|
464
|
+
const operation = parseOperation(path7, method, operationObject);
|
|
398
465
|
names.forEach((name) => {
|
|
399
466
|
let service = services.find((service2) => service2.name === name);
|
|
400
467
|
if (!service) {
|
|
@@ -415,11 +482,11 @@ function parseService(path5, method, operationObject, tags, services) {
|
|
|
415
482
|
// src/parse/v2/parse-services.ts
|
|
416
483
|
function parseServices(document) {
|
|
417
484
|
const services = [];
|
|
418
|
-
Object.entries(document.paths).forEach(([
|
|
485
|
+
Object.entries(document.paths).forEach(([path7, pathObject]) => {
|
|
419
486
|
if (pathObject) {
|
|
420
487
|
Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
421
488
|
parseService(
|
|
422
|
-
|
|
489
|
+
path7,
|
|
423
490
|
method,
|
|
424
491
|
operationObject,
|
|
425
492
|
document.tags || [],
|
|
@@ -616,10 +683,10 @@ function parseParametersQuery2(parameters) {
|
|
|
616
683
|
}
|
|
617
684
|
|
|
618
685
|
// src/parse/v3/parse-operation.ts
|
|
619
|
-
function parseOperation2(
|
|
686
|
+
function parseOperation2(path7, method, operationObject) {
|
|
620
687
|
var _a;
|
|
621
|
-
const name = getOperationName(
|
|
622
|
-
const operation = new Operation(name, method,
|
|
688
|
+
const name = getOperationName(path7, method, operationObject);
|
|
689
|
+
const operation = new Operation(name, method, path7);
|
|
623
690
|
operation.description = operationObject.summary;
|
|
624
691
|
if (operationObject.requestBody) {
|
|
625
692
|
operation.parametersBody = parseParametersBody2(operationObject.requestBody);
|
|
@@ -637,7 +704,7 @@ function parseOperation2(path5, method, operationObject) {
|
|
|
637
704
|
...(responseSchema == null ? void 0 : responseSchema.imports) || []
|
|
638
705
|
])
|
|
639
706
|
);
|
|
640
|
-
operation.responseRef = (responseSchema == null ? void 0 : responseSchema.ref) || "void";
|
|
707
|
+
operation.responseRef = (responseSchema == null ? void 0 : responseSchema.ref) || (responseSchema == null ? void 0 : responseSchema.type) || "void";
|
|
641
708
|
return operation;
|
|
642
709
|
}
|
|
643
710
|
function parseResponseType2(responses) {
|
|
@@ -669,17 +736,17 @@ function parseResponseType2(responses) {
|
|
|
669
736
|
}
|
|
670
737
|
|
|
671
738
|
// src/parse/v3/parse-service.ts
|
|
672
|
-
function parseService2(
|
|
739
|
+
function parseService2(path7, method, operationObject, tags, services) {
|
|
673
740
|
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
674
741
|
const names = toNames(
|
|
675
742
|
getServiceName(
|
|
676
|
-
|
|
743
|
+
path7,
|
|
677
744
|
method,
|
|
678
745
|
operationObject,
|
|
679
746
|
tags || []
|
|
680
747
|
)
|
|
681
748
|
);
|
|
682
|
-
const operation = parseOperation2(
|
|
749
|
+
const operation = parseOperation2(path7, method, operationObject);
|
|
683
750
|
names.forEach((name) => {
|
|
684
751
|
let service = services.find((service2) => service2.name === name);
|
|
685
752
|
if (!service) {
|
|
@@ -700,11 +767,11 @@ function parseService2(path5, method, operationObject, tags, services) {
|
|
|
700
767
|
// src/parse/v3/parse-services.ts
|
|
701
768
|
function parseServices2(document) {
|
|
702
769
|
const services = [];
|
|
703
|
-
Object.entries(document.paths).forEach(([
|
|
770
|
+
Object.entries(document.paths).forEach(([path7, pathObject]) => {
|
|
704
771
|
if (pathObject) {
|
|
705
772
|
Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
706
773
|
parseService2(
|
|
707
|
-
|
|
774
|
+
path7,
|
|
708
775
|
method,
|
|
709
776
|
operationObject,
|
|
710
777
|
document.tags || [],
|
|
@@ -796,8 +863,8 @@ function updateProgress(name, type) {
|
|
|
796
863
|
}
|
|
797
864
|
|
|
798
865
|
// src/template.ts
|
|
799
|
-
|
|
800
|
-
|
|
866
|
+
|
|
867
|
+
|
|
801
868
|
var _handlebars = require('handlebars'); var _handlebars2 = _interopRequireDefault(_handlebars);
|
|
802
869
|
|
|
803
870
|
// src/template-helpers/equal.helper.ts
|
|
@@ -812,6 +879,18 @@ var equalHelper = {
|
|
|
812
879
|
}
|
|
813
880
|
};
|
|
814
881
|
|
|
882
|
+
// src/template-helpers/include-query-params.ts
|
|
883
|
+
var includeQueryParams = {
|
|
884
|
+
name: "include-query-params",
|
|
885
|
+
fn(operations, options) {
|
|
886
|
+
if (operations.some((x) => x.parametersQuery.length > 0)) {
|
|
887
|
+
return options.fn(this);
|
|
888
|
+
} else {
|
|
889
|
+
return options.inverse(this);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
|
|
815
894
|
// src/template-helpers/is-array.helper.ts
|
|
816
895
|
var isArrayHelper = {
|
|
817
896
|
name: "is-array",
|
|
@@ -865,6 +944,7 @@ function registerHandlebarHelpers() {
|
|
|
865
944
|
registerHandlebarHelper(equalHelper);
|
|
866
945
|
registerHandlebarHelper(isArrayHelper);
|
|
867
946
|
registerHandlebarHelper(toUpperHelper);
|
|
947
|
+
registerHandlebarHelper(includeQueryParams);
|
|
868
948
|
}
|
|
869
949
|
function registerHandlebarHelper(helper) {
|
|
870
950
|
_handlebars2.default.registerHelper(helper.name, helper.fn);
|
|
@@ -874,8 +954,8 @@ function registerHandlebarPartial(input) {
|
|
|
874
954
|
_handlebars2.default.registerPartial(input, template);
|
|
875
955
|
}
|
|
876
956
|
function loadHandlebarTemplate(input) {
|
|
877
|
-
const templatePath =
|
|
878
|
-
return
|
|
957
|
+
const templatePath = path3.resolve(__dirname, "templates", `${input}.hbs`);
|
|
958
|
+
return fs2.readFileSync(templatePath, "utf-8");
|
|
879
959
|
}
|
|
880
960
|
|
|
881
961
|
// src/utils/get-openapi-document.ts
|
|
@@ -896,44 +976,59 @@ function getOpenAPIVersion(document) {
|
|
|
896
976
|
throw new Error(`\u65E0\u6CD5\u8BC6\u522B\u7684OPENAPI\u7248\u672C: "${String(version)}"`);
|
|
897
977
|
}
|
|
898
978
|
|
|
899
|
-
// src/
|
|
979
|
+
// src/generate/write-config.ts
|
|
980
|
+
var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto);
|
|
900
981
|
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
if (
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
openapi: options.openapi
|
|
913
|
-
};
|
|
982
|
+
|
|
983
|
+
var ConfigFileName = ".config.json";
|
|
984
|
+
var ConfigFilePath = path2.default.resolve(".request", ConfigFileName);
|
|
985
|
+
function updateOptionsFromLocalConfig(options) {
|
|
986
|
+
const config = readLocalConfig();
|
|
987
|
+
const toUpdateOptions = /* @__PURE__ */ new Map();
|
|
988
|
+
options = options.filter((item) => {
|
|
989
|
+
const md5 = isFindNewMd5(item.name, config);
|
|
990
|
+
if (md5) {
|
|
991
|
+
toUpdateOptions.set(item.name, md5);
|
|
992
|
+
return true;
|
|
914
993
|
} else {
|
|
915
|
-
return
|
|
916
|
-
service: application.key,
|
|
917
|
-
openapi: application.openapi
|
|
918
|
-
};
|
|
994
|
+
return false;
|
|
919
995
|
}
|
|
920
|
-
})
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
application: service,
|
|
924
|
-
input: `${options.gateway}/${service}/${openapi}`.replace(/\/{2,3}/g, "/"),
|
|
925
|
-
output: name ? path.default.join(options.output, name) : options.output,
|
|
926
|
-
exportModels: options.exportModels
|
|
927
|
-
};
|
|
996
|
+
});
|
|
997
|
+
writeLocalConfig(toUpdateOptions, config);
|
|
998
|
+
return options;
|
|
928
999
|
}
|
|
929
|
-
function
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
);
|
|
934
|
-
} else {
|
|
935
|
-
return [createOptions(options)];
|
|
1000
|
+
function isFindNewMd5(name, config) {
|
|
1001
|
+
const file = path2.default.resolve(".request", `${name}.json`);
|
|
1002
|
+
if (!fs.default.existsSync(file)) {
|
|
1003
|
+
throw new Error(`\u672A\u627E\u5230\u76F8\u5E94\u7684\u914D\u7F6E\u6587\u4EF6: ${name}.json`);
|
|
936
1004
|
}
|
|
1005
|
+
const data = fs.default.readFileSync(file);
|
|
1006
|
+
const md5 = _crypto2.default.createHash("md5").update(data.toString()).digest("hex");
|
|
1007
|
+
const configItem = config.find((x) => x.name === name);
|
|
1008
|
+
return !configItem || configItem.md5 !== md5 ? md5 : "";
|
|
1009
|
+
}
|
|
1010
|
+
function readLocalConfig() {
|
|
1011
|
+
if (!fs.default.existsSync(ConfigFilePath)) {
|
|
1012
|
+
return [];
|
|
1013
|
+
}
|
|
1014
|
+
const data = fs.default.readFileSync(ConfigFilePath, "utf-8");
|
|
1015
|
+
return JSON.parse(data);
|
|
1016
|
+
}
|
|
1017
|
+
function writeLocalConfig(toUpdateOptions, config) {
|
|
1018
|
+
toUpdateOptions.forEach((md5, name) => {
|
|
1019
|
+
const configItem = config.find((x) => x.name === name);
|
|
1020
|
+
if (configItem) {
|
|
1021
|
+
configItem.md5 = md5;
|
|
1022
|
+
configItem.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1023
|
+
} else {
|
|
1024
|
+
config.push({
|
|
1025
|
+
name,
|
|
1026
|
+
md5,
|
|
1027
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
});
|
|
1031
|
+
fs.default.writeFileSync(ConfigFilePath, JSON.stringify(config, null, 2));
|
|
937
1032
|
}
|
|
938
1033
|
|
|
939
1034
|
// src/generate/write-models.ts
|
|
@@ -947,7 +1042,7 @@ var _rimraf = require('rimraf'); var _rimraf2 = _interopRequireDefault(_rimraf);
|
|
|
947
1042
|
// src/generate/write-file.ts
|
|
948
1043
|
|
|
949
1044
|
function writeFile(output, content) {
|
|
950
|
-
|
|
1045
|
+
fs4.writeFileSync(output, content, "utf-8");
|
|
951
1046
|
}
|
|
952
1047
|
|
|
953
1048
|
// src/generate/write-model.ts
|
|
@@ -963,14 +1058,14 @@ function writeModels(client, options) {
|
|
|
963
1058
|
if (!options.exportModels || !client.models) {
|
|
964
1059
|
return;
|
|
965
1060
|
}
|
|
966
|
-
const output =
|
|
967
|
-
if (
|
|
1061
|
+
const output = path5.join(options.output, "models");
|
|
1062
|
+
if (fs5.existsSync(output)) {
|
|
968
1063
|
_rimraf2.default.sync(output);
|
|
969
1064
|
}
|
|
970
|
-
|
|
1065
|
+
fs5.mkdirSync(output, { recursive: true });
|
|
971
1066
|
client.models.forEach((model) => {
|
|
972
1067
|
const filename = `${model.name}.ts`;
|
|
973
|
-
writeModel(model,
|
|
1068
|
+
writeModel(model, path5.join(output, filename));
|
|
974
1069
|
updateProgress(options.name || "default", "model");
|
|
975
1070
|
});
|
|
976
1071
|
}
|
|
@@ -994,15 +1089,15 @@ function writeServices(client, options) {
|
|
|
994
1089
|
if (!client.services) {
|
|
995
1090
|
return;
|
|
996
1091
|
}
|
|
997
|
-
const output =
|
|
998
|
-
if (
|
|
1092
|
+
const output = path6.join(options.output, "services");
|
|
1093
|
+
if (fs6.existsSync(output)) {
|
|
999
1094
|
_rimraf2.default.sync(output);
|
|
1000
1095
|
}
|
|
1001
|
-
|
|
1096
|
+
fs6.mkdirSync(output, { recursive: true });
|
|
1002
1097
|
client.services.forEach((service) => {
|
|
1003
1098
|
const filename = `${service.name}Service.ts`;
|
|
1004
1099
|
service.application = Generate.options.appendService === false ? "" : options.application;
|
|
1005
|
-
writeService(service,
|
|
1100
|
+
writeService(service, path6.join(output, filename));
|
|
1006
1101
|
updateProgress(options.name || "default", "service");
|
|
1007
1102
|
});
|
|
1008
1103
|
}
|
|
@@ -1017,7 +1112,8 @@ var _Generate = class {
|
|
|
1017
1112
|
static async startup(options) {
|
|
1018
1113
|
_Generate.options = options;
|
|
1019
1114
|
registerHandlebarTemplates();
|
|
1020
|
-
|
|
1115
|
+
let applicationOptions = generateServiceOptions(options);
|
|
1116
|
+
applicationOptions = updateOptionsFromLocalConfig(applicationOptions);
|
|
1021
1117
|
const applications = [];
|
|
1022
1118
|
for (const applicationOption of applicationOptions) {
|
|
1023
1119
|
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
@@ -1090,8 +1186,10 @@ function defineConfig(config) {
|
|
|
1090
1186
|
}
|
|
1091
1187
|
|
|
1092
1188
|
// src/index.ts
|
|
1093
|
-
var
|
|
1189
|
+
var generate = Generate.startup;
|
|
1190
|
+
var download = Download.startup;
|
|
1191
|
+
|
|
1094
1192
|
|
|
1095
1193
|
|
|
1096
1194
|
|
|
1097
|
-
exports.
|
|
1195
|
+
exports.defineConfig = defineConfig; exports.download = download; exports.generate = generate;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,74 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__dirname,
|
|
3
3
|
__publicField
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-WF3XBEPN.mjs";
|
|
5
|
+
|
|
6
|
+
// src/download/index.ts
|
|
7
|
+
import * as fs from "node:fs";
|
|
8
|
+
import * as path2 from "node:path";
|
|
9
|
+
|
|
10
|
+
// src/utils/get-services-options.ts
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
function createOptions(options, name, application) {
|
|
13
|
+
const { service, openapi } = (() => {
|
|
14
|
+
if (!application) {
|
|
15
|
+
return {
|
|
16
|
+
service: "",
|
|
17
|
+
openapi: options.openapi
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (typeof application === "string") {
|
|
21
|
+
return {
|
|
22
|
+
service: application,
|
|
23
|
+
openapi: options.openapi
|
|
24
|
+
};
|
|
25
|
+
} else {
|
|
26
|
+
return {
|
|
27
|
+
service: application.key,
|
|
28
|
+
openapi: application.openapi
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
})();
|
|
32
|
+
return {
|
|
33
|
+
name,
|
|
34
|
+
application: service,
|
|
35
|
+
input: `${options.gateway}/${service}/${openapi}`.replace(/\/{2,3}/g, "/"),
|
|
36
|
+
output: name ? path.join(options.output, name) : options.output,
|
|
37
|
+
exportModels: options.exportModels
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function generateServiceOptions(options) {
|
|
41
|
+
if (options.applications && Object.keys(options.applications).length) {
|
|
42
|
+
return Object.entries(options.applications).map(
|
|
43
|
+
([name, application]) => createOptions(options, name, application)
|
|
44
|
+
);
|
|
45
|
+
} else {
|
|
46
|
+
return [createOptions(options)];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/download/index.ts
|
|
51
|
+
var DefaultDownloadDir = ".request";
|
|
52
|
+
var _Download = class {
|
|
53
|
+
static async startup(options) {
|
|
54
|
+
const applicationOptions = generateServiceOptions(options);
|
|
55
|
+
if (!fs.existsSync(DefaultDownloadDir)) {
|
|
56
|
+
fs.mkdirSync(DefaultDownloadDir);
|
|
57
|
+
}
|
|
58
|
+
await Promise.all(
|
|
59
|
+
applicationOptions.map((options2) => _Download.downloadOpenAPIFile(options2))
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
static async downloadOpenAPIFile(option) {
|
|
63
|
+
const response = await fetch(option.input);
|
|
64
|
+
const data = await response.json();
|
|
65
|
+
const filePath = path2.join(".request", `${option.name}.json`);
|
|
66
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
|
67
|
+
return data;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var Download = _Download;
|
|
71
|
+
__publicField(Download, "options");
|
|
5
72
|
|
|
6
73
|
// src/generate/index.ts
|
|
7
74
|
import process from "node:process";
|
|
@@ -207,11 +274,11 @@ var Service = class {
|
|
|
207
274
|
};
|
|
208
275
|
|
|
209
276
|
// src/utils/get-service-name.ts
|
|
210
|
-
function getServiceName(
|
|
277
|
+
function getServiceName(path7, method, operationObject, tags) {
|
|
211
278
|
var _a, _b, _c;
|
|
212
279
|
const resolve2 = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.serviceResolve;
|
|
213
280
|
if (resolve2) {
|
|
214
|
-
return resolve2({ path:
|
|
281
|
+
return resolve2({ path: path7, method, object: operationObject, tags });
|
|
215
282
|
} else {
|
|
216
283
|
return ((_c = operationObject.tags) == null ? void 0 : _c.map((tag) => getCamelName(tag))) || "Default";
|
|
217
284
|
}
|
|
@@ -239,21 +306,21 @@ var Operation = class {
|
|
|
239
306
|
parametersBody;
|
|
240
307
|
// 导入操作
|
|
241
308
|
imports = [];
|
|
242
|
-
constructor(name, method,
|
|
309
|
+
constructor(name, method, path7) {
|
|
243
310
|
var _a, _b;
|
|
244
311
|
this.name = name;
|
|
245
312
|
this.method = method;
|
|
246
|
-
this.path =
|
|
313
|
+
this.path = path7;
|
|
247
314
|
this.responseType = ((_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.responseType) || "promise";
|
|
248
315
|
}
|
|
249
316
|
};
|
|
250
317
|
|
|
251
318
|
// src/utils/get-operation-name.ts
|
|
252
|
-
function getOperationName(
|
|
319
|
+
function getOperationName(path7, method, operationObject) {
|
|
253
320
|
var _a, _b;
|
|
254
321
|
const resolve2 = (_b = (_a = Generate.options) == null ? void 0 : _a.exportServices) == null ? void 0 : _b.operationResolve;
|
|
255
322
|
if (resolve2) {
|
|
256
|
-
return resolve2({ path:
|
|
323
|
+
return resolve2({ path: path7, method, object: operationObject });
|
|
257
324
|
} else {
|
|
258
325
|
return operationObject.operationId || "";
|
|
259
326
|
}
|
|
@@ -346,10 +413,10 @@ function parseParametersQuery(parameters) {
|
|
|
346
413
|
}
|
|
347
414
|
|
|
348
415
|
// src/parse/v2/parse-operation.ts
|
|
349
|
-
function parseOperation(
|
|
416
|
+
function parseOperation(path7, method, operationObject) {
|
|
350
417
|
var _a;
|
|
351
|
-
const name = getOperationName(
|
|
352
|
-
const operation = new Operation(name, method,
|
|
418
|
+
const name = getOperationName(path7, method, operationObject);
|
|
419
|
+
const operation = new Operation(name, method, path7);
|
|
353
420
|
operation.description = operationObject.summary || operation.description;
|
|
354
421
|
if (operationObject.parameters) {
|
|
355
422
|
operation.parametersBody = parseParametersBody(operationObject.parameters);
|
|
@@ -385,17 +452,17 @@ function parseResponseType(responses) {
|
|
|
385
452
|
}
|
|
386
453
|
|
|
387
454
|
// src/parse/v2/parse-service.ts
|
|
388
|
-
function parseService(
|
|
455
|
+
function parseService(path7, method, operationObject, tags, services) {
|
|
389
456
|
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
390
457
|
const names = toNames(
|
|
391
458
|
getServiceName(
|
|
392
|
-
|
|
459
|
+
path7,
|
|
393
460
|
method,
|
|
394
461
|
operationObject,
|
|
395
462
|
tags || []
|
|
396
463
|
)
|
|
397
464
|
);
|
|
398
|
-
const operation = parseOperation(
|
|
465
|
+
const operation = parseOperation(path7, method, operationObject);
|
|
399
466
|
names.forEach((name) => {
|
|
400
467
|
let service = services.find((service2) => service2.name === name);
|
|
401
468
|
if (!service) {
|
|
@@ -416,11 +483,11 @@ function parseService(path5, method, operationObject, tags, services) {
|
|
|
416
483
|
// src/parse/v2/parse-services.ts
|
|
417
484
|
function parseServices(document) {
|
|
418
485
|
const services = [];
|
|
419
|
-
Object.entries(document.paths).forEach(([
|
|
486
|
+
Object.entries(document.paths).forEach(([path7, pathObject]) => {
|
|
420
487
|
if (pathObject) {
|
|
421
488
|
Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
422
489
|
parseService(
|
|
423
|
-
|
|
490
|
+
path7,
|
|
424
491
|
method,
|
|
425
492
|
operationObject,
|
|
426
493
|
document.tags || [],
|
|
@@ -617,10 +684,10 @@ function parseParametersQuery2(parameters) {
|
|
|
617
684
|
}
|
|
618
685
|
|
|
619
686
|
// src/parse/v3/parse-operation.ts
|
|
620
|
-
function parseOperation2(
|
|
687
|
+
function parseOperation2(path7, method, operationObject) {
|
|
621
688
|
var _a;
|
|
622
|
-
const name = getOperationName(
|
|
623
|
-
const operation = new Operation(name, method,
|
|
689
|
+
const name = getOperationName(path7, method, operationObject);
|
|
690
|
+
const operation = new Operation(name, method, path7);
|
|
624
691
|
operation.description = operationObject.summary;
|
|
625
692
|
if (operationObject.requestBody) {
|
|
626
693
|
operation.parametersBody = parseParametersBody2(operationObject.requestBody);
|
|
@@ -638,7 +705,7 @@ function parseOperation2(path5, method, operationObject) {
|
|
|
638
705
|
...(responseSchema == null ? void 0 : responseSchema.imports) || []
|
|
639
706
|
])
|
|
640
707
|
);
|
|
641
|
-
operation.responseRef = (responseSchema == null ? void 0 : responseSchema.ref) || "void";
|
|
708
|
+
operation.responseRef = (responseSchema == null ? void 0 : responseSchema.ref) || (responseSchema == null ? void 0 : responseSchema.type) || "void";
|
|
642
709
|
return operation;
|
|
643
710
|
}
|
|
644
711
|
function parseResponseType2(responses) {
|
|
@@ -670,17 +737,17 @@ function parseResponseType2(responses) {
|
|
|
670
737
|
}
|
|
671
738
|
|
|
672
739
|
// src/parse/v3/parse-service.ts
|
|
673
|
-
function parseService2(
|
|
740
|
+
function parseService2(path7, method, operationObject, tags, services) {
|
|
674
741
|
const toNames = (name) => Array.isArray(name) ? name : [name];
|
|
675
742
|
const names = toNames(
|
|
676
743
|
getServiceName(
|
|
677
|
-
|
|
744
|
+
path7,
|
|
678
745
|
method,
|
|
679
746
|
operationObject,
|
|
680
747
|
tags || []
|
|
681
748
|
)
|
|
682
749
|
);
|
|
683
|
-
const operation = parseOperation2(
|
|
750
|
+
const operation = parseOperation2(path7, method, operationObject);
|
|
684
751
|
names.forEach((name) => {
|
|
685
752
|
let service = services.find((service2) => service2.name === name);
|
|
686
753
|
if (!service) {
|
|
@@ -701,11 +768,11 @@ function parseService2(path5, method, operationObject, tags, services) {
|
|
|
701
768
|
// src/parse/v3/parse-services.ts
|
|
702
769
|
function parseServices2(document) {
|
|
703
770
|
const services = [];
|
|
704
|
-
Object.entries(document.paths).forEach(([
|
|
771
|
+
Object.entries(document.paths).forEach(([path7, pathObject]) => {
|
|
705
772
|
if (pathObject) {
|
|
706
773
|
Object.entries(pathObject).forEach(([method, operationObject]) => {
|
|
707
774
|
parseService2(
|
|
708
|
-
|
|
775
|
+
path7,
|
|
709
776
|
method,
|
|
710
777
|
operationObject,
|
|
711
778
|
document.tags || [],
|
|
@@ -797,8 +864,8 @@ function updateProgress(name, type) {
|
|
|
797
864
|
}
|
|
798
865
|
|
|
799
866
|
// src/template.ts
|
|
800
|
-
import * as
|
|
801
|
-
import * as
|
|
867
|
+
import * as fs2 from "node:fs";
|
|
868
|
+
import * as path3 from "node:path";
|
|
802
869
|
import Handlebars from "handlebars";
|
|
803
870
|
|
|
804
871
|
// src/template-helpers/equal.helper.ts
|
|
@@ -813,6 +880,18 @@ var equalHelper = {
|
|
|
813
880
|
}
|
|
814
881
|
};
|
|
815
882
|
|
|
883
|
+
// src/template-helpers/include-query-params.ts
|
|
884
|
+
var includeQueryParams = {
|
|
885
|
+
name: "include-query-params",
|
|
886
|
+
fn(operations, options) {
|
|
887
|
+
if (operations.some((x) => x.parametersQuery.length > 0)) {
|
|
888
|
+
return options.fn(this);
|
|
889
|
+
} else {
|
|
890
|
+
return options.inverse(this);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
|
|
816
895
|
// src/template-helpers/is-array.helper.ts
|
|
817
896
|
var isArrayHelper = {
|
|
818
897
|
name: "is-array",
|
|
@@ -866,6 +945,7 @@ function registerHandlebarHelpers() {
|
|
|
866
945
|
registerHandlebarHelper(equalHelper);
|
|
867
946
|
registerHandlebarHelper(isArrayHelper);
|
|
868
947
|
registerHandlebarHelper(toUpperHelper);
|
|
948
|
+
registerHandlebarHelper(includeQueryParams);
|
|
869
949
|
}
|
|
870
950
|
function registerHandlebarHelper(helper) {
|
|
871
951
|
Handlebars.registerHelper(helper.name, helper.fn);
|
|
@@ -875,8 +955,8 @@ function registerHandlebarPartial(input) {
|
|
|
875
955
|
Handlebars.registerPartial(input, template);
|
|
876
956
|
}
|
|
877
957
|
function loadHandlebarTemplate(input) {
|
|
878
|
-
const templatePath =
|
|
879
|
-
return
|
|
958
|
+
const templatePath = path3.resolve(__dirname, "templates", `${input}.hbs`);
|
|
959
|
+
return fs2.readFileSync(templatePath, "utf-8");
|
|
880
960
|
}
|
|
881
961
|
|
|
882
962
|
// src/utils/get-openapi-document.ts
|
|
@@ -897,58 +977,73 @@ function getOpenAPIVersion(document) {
|
|
|
897
977
|
throw new Error(`\u65E0\u6CD5\u8BC6\u522B\u7684OPENAPI\u7248\u672C: "${String(version)}"`);
|
|
898
978
|
}
|
|
899
979
|
|
|
900
|
-
// src/
|
|
901
|
-
import
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
};
|
|
980
|
+
// src/generate/write-config.ts
|
|
981
|
+
import crypto from "node:crypto";
|
|
982
|
+
import fs3 from "node:fs";
|
|
983
|
+
import path4 from "node:path";
|
|
984
|
+
var ConfigFileName = ".config.json";
|
|
985
|
+
var ConfigFilePath = path4.resolve(".request", ConfigFileName);
|
|
986
|
+
function updateOptionsFromLocalConfig(options) {
|
|
987
|
+
const config = readLocalConfig();
|
|
988
|
+
const toUpdateOptions = /* @__PURE__ */ new Map();
|
|
989
|
+
options = options.filter((item) => {
|
|
990
|
+
const md5 = isFindNewMd5(item.name, config);
|
|
991
|
+
if (md5) {
|
|
992
|
+
toUpdateOptions.set(item.name, md5);
|
|
993
|
+
return true;
|
|
915
994
|
} else {
|
|
916
|
-
return
|
|
917
|
-
service: application.key,
|
|
918
|
-
openapi: application.openapi
|
|
919
|
-
};
|
|
995
|
+
return false;
|
|
920
996
|
}
|
|
921
|
-
})
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
application: service,
|
|
925
|
-
input: `${options.gateway}/${service}/${openapi}`.replace(/\/{2,3}/g, "/"),
|
|
926
|
-
output: name ? path2.join(options.output, name) : options.output,
|
|
927
|
-
exportModels: options.exportModels
|
|
928
|
-
};
|
|
997
|
+
});
|
|
998
|
+
writeLocalConfig(toUpdateOptions, config);
|
|
999
|
+
return options;
|
|
929
1000
|
}
|
|
930
|
-
function
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
1001
|
+
function isFindNewMd5(name, config) {
|
|
1002
|
+
const file = path4.resolve(".request", `${name}.json`);
|
|
1003
|
+
if (!fs3.existsSync(file)) {
|
|
1004
|
+
throw new Error(`\u672A\u627E\u5230\u76F8\u5E94\u7684\u914D\u7F6E\u6587\u4EF6: ${name}.json`);
|
|
1005
|
+
}
|
|
1006
|
+
const data = fs3.readFileSync(file);
|
|
1007
|
+
const md5 = crypto.createHash("md5").update(data.toString()).digest("hex");
|
|
1008
|
+
const configItem = config.find((x) => x.name === name);
|
|
1009
|
+
return !configItem || configItem.md5 !== md5 ? md5 : "";
|
|
1010
|
+
}
|
|
1011
|
+
function readLocalConfig() {
|
|
1012
|
+
if (!fs3.existsSync(ConfigFilePath)) {
|
|
1013
|
+
return [];
|
|
937
1014
|
}
|
|
1015
|
+
const data = fs3.readFileSync(ConfigFilePath, "utf-8");
|
|
1016
|
+
return JSON.parse(data);
|
|
1017
|
+
}
|
|
1018
|
+
function writeLocalConfig(toUpdateOptions, config) {
|
|
1019
|
+
toUpdateOptions.forEach((md5, name) => {
|
|
1020
|
+
const configItem = config.find((x) => x.name === name);
|
|
1021
|
+
if (configItem) {
|
|
1022
|
+
configItem.md5 = md5;
|
|
1023
|
+
configItem.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1024
|
+
} else {
|
|
1025
|
+
config.push({
|
|
1026
|
+
name,
|
|
1027
|
+
md5,
|
|
1028
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
});
|
|
1032
|
+
fs3.writeFileSync(ConfigFilePath, JSON.stringify(config, null, 2));
|
|
938
1033
|
}
|
|
939
1034
|
|
|
940
1035
|
// src/generate/write-models.ts
|
|
941
|
-
import * as
|
|
942
|
-
import * as
|
|
1036
|
+
import * as fs5 from "node:fs";
|
|
1037
|
+
import * as path5 from "node:path";
|
|
943
1038
|
import rimraf from "rimraf";
|
|
944
1039
|
|
|
945
1040
|
// src/generate/write-model.ts
|
|
946
1041
|
import Handlebars2 from "handlebars";
|
|
947
1042
|
|
|
948
1043
|
// src/generate/write-file.ts
|
|
949
|
-
import * as
|
|
1044
|
+
import * as fs4 from "node:fs";
|
|
950
1045
|
function writeFile(output, content) {
|
|
951
|
-
|
|
1046
|
+
fs4.writeFileSync(output, content, "utf-8");
|
|
952
1047
|
}
|
|
953
1048
|
|
|
954
1049
|
// src/generate/write-model.ts
|
|
@@ -964,21 +1059,21 @@ function writeModels(client, options) {
|
|
|
964
1059
|
if (!options.exportModels || !client.models) {
|
|
965
1060
|
return;
|
|
966
1061
|
}
|
|
967
|
-
const output =
|
|
968
|
-
if (
|
|
1062
|
+
const output = path5.join(options.output, "models");
|
|
1063
|
+
if (fs5.existsSync(output)) {
|
|
969
1064
|
rimraf.sync(output);
|
|
970
1065
|
}
|
|
971
|
-
|
|
1066
|
+
fs5.mkdirSync(output, { recursive: true });
|
|
972
1067
|
client.models.forEach((model) => {
|
|
973
1068
|
const filename = `${model.name}.ts`;
|
|
974
|
-
writeModel(model,
|
|
1069
|
+
writeModel(model, path5.join(output, filename));
|
|
975
1070
|
updateProgress(options.name || "default", "model");
|
|
976
1071
|
});
|
|
977
1072
|
}
|
|
978
1073
|
|
|
979
1074
|
// src/generate/write-services.ts
|
|
980
|
-
import * as
|
|
981
|
-
import * as
|
|
1075
|
+
import * as fs6 from "node:fs";
|
|
1076
|
+
import * as path6 from "node:path";
|
|
982
1077
|
import rimraf2 from "rimraf";
|
|
983
1078
|
|
|
984
1079
|
// src/generate/write-service.ts
|
|
@@ -995,15 +1090,15 @@ function writeServices(client, options) {
|
|
|
995
1090
|
if (!client.services) {
|
|
996
1091
|
return;
|
|
997
1092
|
}
|
|
998
|
-
const output =
|
|
999
|
-
if (
|
|
1093
|
+
const output = path6.join(options.output, "services");
|
|
1094
|
+
if (fs6.existsSync(output)) {
|
|
1000
1095
|
rimraf2.sync(output);
|
|
1001
1096
|
}
|
|
1002
|
-
|
|
1097
|
+
fs6.mkdirSync(output, { recursive: true });
|
|
1003
1098
|
client.services.forEach((service) => {
|
|
1004
1099
|
const filename = `${service.name}Service.ts`;
|
|
1005
1100
|
service.application = Generate.options.appendService === false ? "" : options.application;
|
|
1006
|
-
writeService(service,
|
|
1101
|
+
writeService(service, path6.join(output, filename));
|
|
1007
1102
|
updateProgress(options.name || "default", "service");
|
|
1008
1103
|
});
|
|
1009
1104
|
}
|
|
@@ -1018,7 +1113,8 @@ var _Generate = class {
|
|
|
1018
1113
|
static async startup(options) {
|
|
1019
1114
|
_Generate.options = options;
|
|
1020
1115
|
registerHandlebarTemplates();
|
|
1021
|
-
|
|
1116
|
+
let applicationOptions = generateServiceOptions(options);
|
|
1117
|
+
applicationOptions = updateOptionsFromLocalConfig(applicationOptions);
|
|
1022
1118
|
const applications = [];
|
|
1023
1119
|
for (const applicationOption of applicationOptions) {
|
|
1024
1120
|
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
@@ -1091,8 +1187,10 @@ function defineConfig(config) {
|
|
|
1091
1187
|
}
|
|
1092
1188
|
|
|
1093
1189
|
// src/index.ts
|
|
1094
|
-
var
|
|
1190
|
+
var generate = Generate.startup;
|
|
1191
|
+
var download = Download.startup;
|
|
1095
1192
|
export {
|
|
1096
|
-
|
|
1097
|
-
|
|
1193
|
+
defineConfig,
|
|
1194
|
+
download,
|
|
1195
|
+
generate
|
|
1098
1196
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PluginOption } from 'vite';
|
|
2
2
|
|
|
3
3
|
interface PluginOptions {
|
|
4
4
|
alias: string;
|
|
@@ -8,6 +8,6 @@ interface PluginOptions {
|
|
|
8
8
|
/**
|
|
9
9
|
* Request插件
|
|
10
10
|
*/
|
|
11
|
-
declare const _default: (options: PluginOptions) =>
|
|
11
|
+
declare const _default: (options: PluginOptions) => PluginOption;
|
|
12
12
|
|
|
13
13
|
export { _default as default };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
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 }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }require('../chunk-XXPGZHWZ.js');
|
|
2
2
|
|
|
3
3
|
// src/vite-plugin/index.ts
|
|
4
|
+
var _child_process = require('child_process');
|
|
4
5
|
var _fs = require('fs'); var fs = _interopRequireWildcard(_fs);
|
|
5
6
|
var _path = require('path'); var path = _interopRequireWildcard(_path);
|
|
6
7
|
var _handlebars = require('handlebars'); var _handlebars2 = _interopRequireDefault(_handlebars);
|
|
@@ -112,28 +113,38 @@ var generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
|
|
|
112
113
|
var MODULE_ID = "virtual:request";
|
|
113
114
|
var DECLARATION_FILE = "request.d.ts";
|
|
114
115
|
var viteConfig;
|
|
116
|
+
var GerneratedCodeStr = "";
|
|
117
|
+
function genretateDeclareAndCode(options) {
|
|
118
|
+
const paths = getServicePaths(options);
|
|
119
|
+
const services = getServiceItems(paths);
|
|
120
|
+
const groups = getServiceGroups(services);
|
|
121
|
+
if (services && services.length) {
|
|
122
|
+
generateDeclare(services, groups, options);
|
|
123
|
+
generateCode(services, groups);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function generateRequestCode() {
|
|
127
|
+
const generateScript = path.resolve(viteConfig.root, "node_modules", ".bin", "request-generate");
|
|
128
|
+
_child_process.execSync.call(void 0, `${generateScript}`);
|
|
129
|
+
}
|
|
115
130
|
var vite_plugin_default = (options) => {
|
|
116
131
|
return {
|
|
117
|
-
name: "vite-plugin-request",
|
|
132
|
+
name: "vite-plugin-vue-request",
|
|
118
133
|
enforce: "pre",
|
|
134
|
+
configResolved(config) {
|
|
135
|
+
viteConfig = config;
|
|
136
|
+
},
|
|
119
137
|
resolveId(id) {
|
|
120
138
|
return id === MODULE_ID ? MODULE_ID : void 0;
|
|
121
139
|
},
|
|
122
|
-
|
|
123
|
-
|
|
140
|
+
async buildStart() {
|
|
141
|
+
await generateRequestCode();
|
|
142
|
+
await genretateDeclareAndCode(options);
|
|
124
143
|
},
|
|
125
144
|
load(id) {
|
|
126
145
|
if (id !== MODULE_ID)
|
|
127
146
|
return;
|
|
128
|
-
|
|
129
|
-
const services = getServiceItems(paths);
|
|
130
|
-
const groups = getServiceGroups(services);
|
|
131
|
-
if (services && services.length) {
|
|
132
|
-
generateDeclare(services, groups, options);
|
|
133
|
-
return generateCode(services, groups);
|
|
134
|
-
} else {
|
|
135
|
-
return void 0;
|
|
136
|
-
}
|
|
147
|
+
return GerneratedCodeStr;
|
|
137
148
|
}
|
|
138
149
|
};
|
|
139
150
|
};
|
|
@@ -191,7 +202,7 @@ function getServiceGroups(services) {
|
|
|
191
202
|
}
|
|
192
203
|
function generateCode(services, groups) {
|
|
193
204
|
const template = _handlebars2.default.compile(generateCodeTemplate);
|
|
194
|
-
|
|
205
|
+
GerneratedCodeStr = template({
|
|
195
206
|
groups: groups.length ? groups : void 0,
|
|
196
207
|
services
|
|
197
208
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import "../chunk-
|
|
1
|
+
import "../chunk-WF3XBEPN.mjs";
|
|
2
2
|
|
|
3
3
|
// src/vite-plugin/index.ts
|
|
4
|
+
import { execSync } from "node:child_process";
|
|
4
5
|
import * as fs from "node:fs";
|
|
5
6
|
import * as path from "node:path";
|
|
6
7
|
import Handlebars from "handlebars";
|
|
@@ -112,28 +113,38 @@ var generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
|
|
|
112
113
|
var MODULE_ID = "virtual:request";
|
|
113
114
|
var DECLARATION_FILE = "request.d.ts";
|
|
114
115
|
var viteConfig;
|
|
116
|
+
var GerneratedCodeStr = "";
|
|
117
|
+
function genretateDeclareAndCode(options) {
|
|
118
|
+
const paths = getServicePaths(options);
|
|
119
|
+
const services = getServiceItems(paths);
|
|
120
|
+
const groups = getServiceGroups(services);
|
|
121
|
+
if (services && services.length) {
|
|
122
|
+
generateDeclare(services, groups, options);
|
|
123
|
+
generateCode(services, groups);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function generateRequestCode() {
|
|
127
|
+
const generateScript = path.resolve(viteConfig.root, "node_modules", ".bin", "request-generate");
|
|
128
|
+
execSync(`${generateScript}`);
|
|
129
|
+
}
|
|
115
130
|
var vite_plugin_default = (options) => {
|
|
116
131
|
return {
|
|
117
|
-
name: "vite-plugin-request",
|
|
132
|
+
name: "vite-plugin-vue-request",
|
|
118
133
|
enforce: "pre",
|
|
134
|
+
configResolved(config) {
|
|
135
|
+
viteConfig = config;
|
|
136
|
+
},
|
|
119
137
|
resolveId(id) {
|
|
120
138
|
return id === MODULE_ID ? MODULE_ID : void 0;
|
|
121
139
|
},
|
|
122
|
-
|
|
123
|
-
|
|
140
|
+
async buildStart() {
|
|
141
|
+
await generateRequestCode();
|
|
142
|
+
await genretateDeclareAndCode(options);
|
|
124
143
|
},
|
|
125
144
|
load(id) {
|
|
126
145
|
if (id !== MODULE_ID)
|
|
127
146
|
return;
|
|
128
|
-
|
|
129
|
-
const services = getServiceItems(paths);
|
|
130
|
-
const groups = getServiceGroups(services);
|
|
131
|
-
if (services && services.length) {
|
|
132
|
-
generateDeclare(services, groups, options);
|
|
133
|
-
return generateCode(services, groups);
|
|
134
|
-
} else {
|
|
135
|
-
return void 0;
|
|
136
|
-
}
|
|
147
|
+
return GerneratedCodeStr;
|
|
137
148
|
}
|
|
138
149
|
};
|
|
139
150
|
};
|
|
@@ -191,7 +202,7 @@ function getServiceGroups(services) {
|
|
|
191
202
|
}
|
|
192
203
|
function generateCode(services, groups) {
|
|
193
204
|
const template = Handlebars.compile(generateCodeTemplate);
|
|
194
|
-
|
|
205
|
+
GerneratedCodeStr = template({
|
|
195
206
|
groups: groups.length ? groups : void 0,
|
|
196
207
|
services
|
|
197
208
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gopowerteam/request-generate",
|
|
3
3
|
"type": "commonjs",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
7
7
|
"gopowerteam",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"module": "./dist/index.mjs",
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
25
25
|
"bin": {
|
|
26
|
-
"request-
|
|
26
|
+
"request-download": "bin/download.mts",
|
|
27
|
+
"request-generate": "bin/generate.mts"
|
|
27
28
|
},
|
|
28
29
|
"files": [
|
|
29
30
|
"README.md",
|
|
@@ -61,6 +62,7 @@
|
|
|
61
62
|
"@gopowerteam/request": "0.1.20"
|
|
62
63
|
},
|
|
63
64
|
"scripts": {
|
|
65
|
+
"dev": "tsup --watch",
|
|
64
66
|
"build": "tsup",
|
|
65
67
|
"lint": "eslint . --fix"
|
|
66
68
|
}
|