@gopowerteam/request-generate 0.1.26 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/download.mts +69 -0
- package/bin/generate.mts +72 -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 +175 -71
- package/dist/index.mjs +183 -79
- 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,69 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
import { program } from 'commander'
|
|
8
|
+
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
10
|
+
const __dirname = path.dirname(__filename)
|
|
11
|
+
const RequestGenerate = await import(`file://${path.resolve(
|
|
12
|
+
__dirname,
|
|
13
|
+
'..',
|
|
14
|
+
'dist',
|
|
15
|
+
'index.mjs',
|
|
16
|
+
)}`)
|
|
17
|
+
|
|
18
|
+
const params = program
|
|
19
|
+
.name('@gopowerteam/request-generate')
|
|
20
|
+
.usage('[options]')
|
|
21
|
+
.option('--config <value>', '指定配置文件位置')
|
|
22
|
+
.parse(process.argv)
|
|
23
|
+
.opts()
|
|
24
|
+
|
|
25
|
+
const configFilePaths = [
|
|
26
|
+
'request.config.ts',
|
|
27
|
+
'request-generate.config.cjs',
|
|
28
|
+
'request-generate.config.js',
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 加载配置文件
|
|
33
|
+
* @param {*} filePath
|
|
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
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
throw new Error('无法找到RequestGenerate配置文件')
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (RequestGenerate) {
|
|
57
|
+
const { default: config } = await loadConfigFile(params.config)
|
|
58
|
+
|
|
59
|
+
RequestGenerate.download(config)
|
|
60
|
+
.then(() => {
|
|
61
|
+
// eslint-disable-next-line no-console
|
|
62
|
+
console.log('接口文件更新完成')
|
|
63
|
+
process.exit(0)
|
|
64
|
+
})
|
|
65
|
+
.catch((error) => {
|
|
66
|
+
console.error(error)
|
|
67
|
+
process.exit(1)
|
|
68
|
+
})
|
|
69
|
+
}
|
package/bin/generate.mts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
import { program } from 'commander'
|
|
8
|
+
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
10
|
+
const __dirname = path.dirname(__filename)
|
|
11
|
+
const RequestGenerate = await import(`file://${path.resolve(
|
|
12
|
+
__dirname,
|
|
13
|
+
'..',
|
|
14
|
+
'dist',
|
|
15
|
+
'index.mjs',
|
|
16
|
+
)}`)
|
|
17
|
+
|
|
18
|
+
const params = program
|
|
19
|
+
.name('@gopowerteam/request-generate')
|
|
20
|
+
.usage('[options]')
|
|
21
|
+
.option('--config <value>', '指定配置文件位置')
|
|
22
|
+
.parse(process.argv)
|
|
23
|
+
.opts()
|
|
24
|
+
|
|
25
|
+
const configFilePaths = [
|
|
26
|
+
'request.config.ts',
|
|
27
|
+
'request-generate.config.cjs',
|
|
28
|
+
'request-generate.config.js',
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 加载配置文件
|
|
33
|
+
* @param {*} filePath
|
|
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
|
+
}
|
|
51
|
+
else if (configFilePath.endsWith('ts')) {
|
|
52
|
+
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
throw new Error('无法找到RequestGenerate配置文件')
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (RequestGenerate) {
|
|
60
|
+
const { default: config } = await loadConfigFile(params.config)
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
await RequestGenerate.generate(config)
|
|
64
|
+
// eslint-disable-next-line no-console
|
|
65
|
+
console.log('接口文件生成完成')
|
|
66
|
+
process.exit(0)
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.error(error)
|
|
70
|
+
process.exit(1)
|
|
71
|
+
}
|
|
72
|
+
}
|
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);
|
|
@@ -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,45 +976,66 @@ 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, output) {
|
|
986
|
+
const config = readLocalConfig();
|
|
987
|
+
const toUpdateOptions = /* @__PURE__ */ new Map();
|
|
988
|
+
options = options.filter((item) => {
|
|
989
|
+
const md5 = isNeedUpdate(item.name, config, output);
|
|
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
|
-
}
|
|
935
|
-
|
|
1000
|
+
function isNeedUpdate(name, config, output) {
|
|
1001
|
+
const file = path2.default.resolve(".request", `${name}.json`);
|
|
1002
|
+
const outputDir = path2.default.resolve(output);
|
|
1003
|
+
if (!fs.default.existsSync(file)) {
|
|
1004
|
+
throw new Error(`\u672A\u627E\u5230\u76F8\u5E94\u7684\u914D\u7F6E\u6587\u4EF6: ${name}.json`);
|
|
1005
|
+
}
|
|
1006
|
+
const data = fs.default.readFileSync(file);
|
|
1007
|
+
const md5 = _crypto2.default.createHash("md5").update(data.toString()).digest("hex");
|
|
1008
|
+
if (!fs.default.existsSync(path2.default.join(outputDir, name))) {
|
|
1009
|
+
return md5;
|
|
1010
|
+
}
|
|
1011
|
+
const configItem = config.find((x) => x.name === name);
|
|
1012
|
+
if (!configItem || configItem.md5 !== md5) {
|
|
1013
|
+
return md5;
|
|
936
1014
|
}
|
|
937
1015
|
}
|
|
1016
|
+
function readLocalConfig() {
|
|
1017
|
+
if (!fs.default.existsSync(ConfigFilePath)) {
|
|
1018
|
+
return [];
|
|
1019
|
+
}
|
|
1020
|
+
const data = fs.default.readFileSync(ConfigFilePath, "utf-8");
|
|
1021
|
+
return JSON.parse(data);
|
|
1022
|
+
}
|
|
1023
|
+
function writeLocalConfig(toUpdateOptions, config) {
|
|
1024
|
+
toUpdateOptions.forEach((md5, name) => {
|
|
1025
|
+
const configItem = config.find((x) => x.name === name);
|
|
1026
|
+
if (configItem) {
|
|
1027
|
+
configItem.md5 = md5;
|
|
1028
|
+
configItem.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1029
|
+
} else {
|
|
1030
|
+
config.push({
|
|
1031
|
+
name,
|
|
1032
|
+
md5,
|
|
1033
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
fs.default.writeFileSync(ConfigFilePath, JSON.stringify(config, null, 2));
|
|
1038
|
+
}
|
|
938
1039
|
|
|
939
1040
|
// src/generate/write-models.ts
|
|
940
1041
|
|
|
@@ -947,7 +1048,7 @@ var _rimraf = require('rimraf'); var _rimraf2 = _interopRequireDefault(_rimraf);
|
|
|
947
1048
|
// src/generate/write-file.ts
|
|
948
1049
|
|
|
949
1050
|
function writeFile(output, content) {
|
|
950
|
-
|
|
1051
|
+
fs4.writeFileSync(output, content, "utf-8");
|
|
951
1052
|
}
|
|
952
1053
|
|
|
953
1054
|
// src/generate/write-model.ts
|
|
@@ -963,14 +1064,14 @@ function writeModels(client, options) {
|
|
|
963
1064
|
if (!options.exportModels || !client.models) {
|
|
964
1065
|
return;
|
|
965
1066
|
}
|
|
966
|
-
const output =
|
|
967
|
-
if (
|
|
1067
|
+
const output = path5.join(options.output, "models");
|
|
1068
|
+
if (fs5.existsSync(output)) {
|
|
968
1069
|
_rimraf2.default.sync(output);
|
|
969
1070
|
}
|
|
970
|
-
|
|
1071
|
+
fs5.mkdirSync(output, { recursive: true });
|
|
971
1072
|
client.models.forEach((model) => {
|
|
972
1073
|
const filename = `${model.name}.ts`;
|
|
973
|
-
writeModel(model,
|
|
1074
|
+
writeModel(model, path5.join(output, filename));
|
|
974
1075
|
updateProgress(options.name || "default", "model");
|
|
975
1076
|
});
|
|
976
1077
|
}
|
|
@@ -994,15 +1095,15 @@ function writeServices(client, options) {
|
|
|
994
1095
|
if (!client.services) {
|
|
995
1096
|
return;
|
|
996
1097
|
}
|
|
997
|
-
const output =
|
|
998
|
-
if (
|
|
1098
|
+
const output = path6.join(options.output, "services");
|
|
1099
|
+
if (fs6.existsSync(output)) {
|
|
999
1100
|
_rimraf2.default.sync(output);
|
|
1000
1101
|
}
|
|
1001
|
-
|
|
1102
|
+
fs6.mkdirSync(output, { recursive: true });
|
|
1002
1103
|
client.services.forEach((service) => {
|
|
1003
1104
|
const filename = `${service.name}Service.ts`;
|
|
1004
1105
|
service.application = Generate.options.appendService === false ? "" : options.application;
|
|
1005
|
-
writeService(service,
|
|
1106
|
+
writeService(service, path6.join(output, filename));
|
|
1006
1107
|
updateProgress(options.name || "default", "service");
|
|
1007
1108
|
});
|
|
1008
1109
|
}
|
|
@@ -1017,7 +1118,8 @@ var _Generate = class {
|
|
|
1017
1118
|
static async startup(options) {
|
|
1018
1119
|
_Generate.options = options;
|
|
1019
1120
|
registerHandlebarTemplates();
|
|
1020
|
-
|
|
1121
|
+
let applicationOptions = generateServiceOptions(options);
|
|
1122
|
+
applicationOptions = updateOptionsFromLocalConfig(applicationOptions, options.output);
|
|
1021
1123
|
const applications = [];
|
|
1022
1124
|
for (const applicationOption of applicationOptions) {
|
|
1023
1125
|
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
@@ -1090,8 +1192,10 @@ function defineConfig(config) {
|
|
|
1090
1192
|
}
|
|
1091
1193
|
|
|
1092
1194
|
// src/index.ts
|
|
1093
|
-
var
|
|
1195
|
+
var generate = Generate.startup;
|
|
1196
|
+
var download = Download.startup;
|
|
1197
|
+
|
|
1094
1198
|
|
|
1095
1199
|
|
|
1096
1200
|
|
|
1097
|
-
exports.
|
|
1201
|
+
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);
|
|
@@ -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,79 @@ 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, output) {
|
|
987
|
+
const config = readLocalConfig();
|
|
988
|
+
const toUpdateOptions = /* @__PURE__ */ new Map();
|
|
989
|
+
options = options.filter((item) => {
|
|
990
|
+
const md5 = isNeedUpdate(item.name, config, output);
|
|
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 isNeedUpdate(name, config, output) {
|
|
1002
|
+
const file = path4.resolve(".request", `${name}.json`);
|
|
1003
|
+
const outputDir = path4.resolve(output);
|
|
1004
|
+
if (!fs3.existsSync(file)) {
|
|
1005
|
+
throw new Error(`\u672A\u627E\u5230\u76F8\u5E94\u7684\u914D\u7F6E\u6587\u4EF6: ${name}.json`);
|
|
1006
|
+
}
|
|
1007
|
+
const data = fs3.readFileSync(file);
|
|
1008
|
+
const md5 = crypto.createHash("md5").update(data.toString()).digest("hex");
|
|
1009
|
+
if (!fs3.existsSync(path4.join(outputDir, name))) {
|
|
1010
|
+
return md5;
|
|
937
1011
|
}
|
|
1012
|
+
const configItem = config.find((x) => x.name === name);
|
|
1013
|
+
if (!configItem || configItem.md5 !== md5) {
|
|
1014
|
+
return md5;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
function readLocalConfig() {
|
|
1018
|
+
if (!fs3.existsSync(ConfigFilePath)) {
|
|
1019
|
+
return [];
|
|
1020
|
+
}
|
|
1021
|
+
const data = fs3.readFileSync(ConfigFilePath, "utf-8");
|
|
1022
|
+
return JSON.parse(data);
|
|
1023
|
+
}
|
|
1024
|
+
function writeLocalConfig(toUpdateOptions, config) {
|
|
1025
|
+
toUpdateOptions.forEach((md5, name) => {
|
|
1026
|
+
const configItem = config.find((x) => x.name === name);
|
|
1027
|
+
if (configItem) {
|
|
1028
|
+
configItem.md5 = md5;
|
|
1029
|
+
configItem.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1030
|
+
} else {
|
|
1031
|
+
config.push({
|
|
1032
|
+
name,
|
|
1033
|
+
md5,
|
|
1034
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
1038
|
+
fs3.writeFileSync(ConfigFilePath, JSON.stringify(config, null, 2));
|
|
938
1039
|
}
|
|
939
1040
|
|
|
940
1041
|
// src/generate/write-models.ts
|
|
941
|
-
import * as
|
|
942
|
-
import * as
|
|
1042
|
+
import * as fs5 from "node:fs";
|
|
1043
|
+
import * as path5 from "node:path";
|
|
943
1044
|
import rimraf from "rimraf";
|
|
944
1045
|
|
|
945
1046
|
// src/generate/write-model.ts
|
|
946
1047
|
import Handlebars2 from "handlebars";
|
|
947
1048
|
|
|
948
1049
|
// src/generate/write-file.ts
|
|
949
|
-
import * as
|
|
1050
|
+
import * as fs4 from "node:fs";
|
|
950
1051
|
function writeFile(output, content) {
|
|
951
|
-
|
|
1052
|
+
fs4.writeFileSync(output, content, "utf-8");
|
|
952
1053
|
}
|
|
953
1054
|
|
|
954
1055
|
// src/generate/write-model.ts
|
|
@@ -964,21 +1065,21 @@ function writeModels(client, options) {
|
|
|
964
1065
|
if (!options.exportModels || !client.models) {
|
|
965
1066
|
return;
|
|
966
1067
|
}
|
|
967
|
-
const output =
|
|
968
|
-
if (
|
|
1068
|
+
const output = path5.join(options.output, "models");
|
|
1069
|
+
if (fs5.existsSync(output)) {
|
|
969
1070
|
rimraf.sync(output);
|
|
970
1071
|
}
|
|
971
|
-
|
|
1072
|
+
fs5.mkdirSync(output, { recursive: true });
|
|
972
1073
|
client.models.forEach((model) => {
|
|
973
1074
|
const filename = `${model.name}.ts`;
|
|
974
|
-
writeModel(model,
|
|
1075
|
+
writeModel(model, path5.join(output, filename));
|
|
975
1076
|
updateProgress(options.name || "default", "model");
|
|
976
1077
|
});
|
|
977
1078
|
}
|
|
978
1079
|
|
|
979
1080
|
// src/generate/write-services.ts
|
|
980
|
-
import * as
|
|
981
|
-
import * as
|
|
1081
|
+
import * as fs6 from "node:fs";
|
|
1082
|
+
import * as path6 from "node:path";
|
|
982
1083
|
import rimraf2 from "rimraf";
|
|
983
1084
|
|
|
984
1085
|
// src/generate/write-service.ts
|
|
@@ -995,15 +1096,15 @@ function writeServices(client, options) {
|
|
|
995
1096
|
if (!client.services) {
|
|
996
1097
|
return;
|
|
997
1098
|
}
|
|
998
|
-
const output =
|
|
999
|
-
if (
|
|
1099
|
+
const output = path6.join(options.output, "services");
|
|
1100
|
+
if (fs6.existsSync(output)) {
|
|
1000
1101
|
rimraf2.sync(output);
|
|
1001
1102
|
}
|
|
1002
|
-
|
|
1103
|
+
fs6.mkdirSync(output, { recursive: true });
|
|
1003
1104
|
client.services.forEach((service) => {
|
|
1004
1105
|
const filename = `${service.name}Service.ts`;
|
|
1005
1106
|
service.application = Generate.options.appendService === false ? "" : options.application;
|
|
1006
|
-
writeService(service,
|
|
1107
|
+
writeService(service, path6.join(output, filename));
|
|
1007
1108
|
updateProgress(options.name || "default", "service");
|
|
1008
1109
|
});
|
|
1009
1110
|
}
|
|
@@ -1018,7 +1119,8 @@ var _Generate = class {
|
|
|
1018
1119
|
static async startup(options) {
|
|
1019
1120
|
_Generate.options = options;
|
|
1020
1121
|
registerHandlebarTemplates();
|
|
1021
|
-
|
|
1122
|
+
let applicationOptions = generateServiceOptions(options);
|
|
1123
|
+
applicationOptions = updateOptionsFromLocalConfig(applicationOptions, options.output);
|
|
1022
1124
|
const applications = [];
|
|
1023
1125
|
for (const applicationOption of applicationOptions) {
|
|
1024
1126
|
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
@@ -1091,8 +1193,10 @@ function defineConfig(config) {
|
|
|
1091
1193
|
}
|
|
1092
1194
|
|
|
1093
1195
|
// src/index.ts
|
|
1094
|
-
var
|
|
1196
|
+
var generate = Generate.startup;
|
|
1197
|
+
var download = Download.startup;
|
|
1095
1198
|
export {
|
|
1096
|
-
|
|
1097
|
-
|
|
1199
|
+
defineConfig,
|
|
1200
|
+
download,
|
|
1201
|
+
generate
|
|
1098
1202
|
};
|
|
@@ -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.1
|
|
4
|
+
"version": "0.2.1",
|
|
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
|
}
|