@gopowerteam/request-generate 0.1.24 → 0.1.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.mts +16 -14
- package/dist/{chunk-4OXX5WFT.mjs → chunk-3T6NC7NX.mjs} +1 -1
- package/dist/index.d.ts +64 -63
- package/dist/index.js +184 -99
- package/dist/index.mjs +186 -101
- package/dist/vite-plugin/index.d.ts +2 -2
- package/dist/vite-plugin/index.js +1 -1
- package/dist/vite-plugin/index.mjs +4 -4
- package/package.json +37 -34
package/bin/index.mts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env tsx
|
|
2
2
|
|
|
3
|
-
import path from 'node:path'
|
|
4
3
|
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
5
7
|
import { program } from 'commander'
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
8
|
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
9
|
-
const __dirname = path.dirname(__filename)
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
10
|
+
const __dirname = path.dirname(__filename)
|
|
10
11
|
const RequestGenerate = await import(`file://${path.resolve(
|
|
11
12
|
__dirname,
|
|
12
13
|
'..',
|
|
13
14
|
'dist',
|
|
14
|
-
'index.mjs'
|
|
15
|
+
'index.mjs',
|
|
15
16
|
)}`)
|
|
16
17
|
|
|
17
18
|
const params = program
|
|
@@ -24,32 +25,34 @@ const params = program
|
|
|
24
25
|
const configFilePaths = [
|
|
25
26
|
'request.config.ts',
|
|
26
27
|
'request-generate.config.cjs',
|
|
27
|
-
'request-generate.config.js'
|
|
28
|
+
'request-generate.config.js',
|
|
28
29
|
]
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* 加载配置文件
|
|
32
33
|
* @param {*} filePath
|
|
33
|
-
* @returns
|
|
34
|
+
* @returns Promise
|
|
34
35
|
*/
|
|
35
36
|
async function loadConfigFile(filePath) {
|
|
36
37
|
if (filePath) {
|
|
37
38
|
configFilePaths.unshift(filePath)
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
const configFilePath = configFilePaths.find(
|
|
41
|
-
fs.existsSync(path.resolve(process.cwd(), file))
|
|
41
|
+
const configFilePath = configFilePaths.find(file =>
|
|
42
|
+
fs.existsSync(path.resolve(process.cwd(), file)),
|
|
42
43
|
)
|
|
43
44
|
|
|
44
|
-
if(!configFilePath){
|
|
45
|
-
throw new Error(
|
|
45
|
+
if (!configFilePath) {
|
|
46
|
+
throw new Error('Not Find Config File.')
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
if (configFilePath.endsWith('js')) {
|
|
49
50
|
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
50
|
-
}
|
|
51
|
+
}
|
|
52
|
+
else if (configFilePath.endsWith('ts')) {
|
|
51
53
|
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
52
|
-
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
53
56
|
throw new Error('无法找到RequestGenerate配置文件')
|
|
54
57
|
}
|
|
55
58
|
}
|
|
@@ -59,7 +62,6 @@ if (RequestGenerate) {
|
|
|
59
62
|
|
|
60
63
|
RequestGenerate.default(config)
|
|
61
64
|
.then(() => {
|
|
62
|
-
console.log('接口文件生成完成')
|
|
63
65
|
process.exit(0)
|
|
64
66
|
})
|
|
65
67
|
.catch((error) => {
|
|
@@ -5,7 +5,7 @@ var __publicField = (obj, key, value) => {
|
|
|
5
5
|
return value;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
-
// ../../node_modules/.pnpm/tsup@6.6.
|
|
8
|
+
// ../../node_modules/.pnpm/tsup@6.6.3_postcss@8.4.45_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,63 +1,5 @@
|
|
|
1
1
|
import { OpenAPIV2, OpenAPIV3 } from 'openapi-types';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* 生成全局选项
|
|
5
|
-
*/
|
|
6
|
-
type GenerateOptions = {
|
|
7
|
-
// 网关地址
|
|
8
|
-
gateway: string
|
|
9
|
-
// OpenAPI地址
|
|
10
|
-
openapi: string
|
|
11
|
-
// 输出目录
|
|
12
|
-
output: string
|
|
13
|
-
// 输出Model路径
|
|
14
|
-
exportModels: boolean
|
|
15
|
-
// 开启日志输出
|
|
16
|
-
logger?: boolean
|
|
17
|
-
// 输出Model路径
|
|
18
|
-
exportServices?: {
|
|
19
|
-
serviceResolve?: (options: {
|
|
20
|
-
path: string
|
|
21
|
-
method: string
|
|
22
|
-
object: OpenAPIV2.OperationObject | OpenAPIV3.OperationObject
|
|
23
|
-
tags: OpenAPIV2.TagObject[]
|
|
24
|
-
}) => string
|
|
25
|
-
operationResolve?: (options: {
|
|
26
|
-
path: string
|
|
27
|
-
method: string
|
|
28
|
-
object: OpenAPIV2.OperationObject | OpenAPIV3.OperationObject
|
|
29
|
-
}) => string
|
|
30
|
-
excludeQueryParams?: string[]
|
|
31
|
-
responseType?: 'promise' | 'observable'
|
|
32
|
-
}
|
|
33
|
-
// 多应用列表
|
|
34
|
-
applications?: Record<string, 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
3
|
declare enum OpenAPIVersion {
|
|
62
4
|
V2 = 2,
|
|
63
5
|
V3 = 3
|
|
@@ -115,31 +57,90 @@ declare class Service {
|
|
|
115
57
|
responseType: 'promise' | 'observable';
|
|
116
58
|
}
|
|
117
59
|
|
|
118
|
-
|
|
60
|
+
interface GenerateClient {
|
|
119
61
|
// 模型列表
|
|
120
62
|
models: Model[]
|
|
121
63
|
// 服务列表
|
|
122
64
|
services: Service[]
|
|
123
65
|
}
|
|
124
66
|
|
|
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
125
|
type UnkownVersionDocument = OpenAPIV3.Document & OpenAPIV2.Document;
|
|
126
126
|
declare class Generate {
|
|
127
127
|
static options: GenerateOptions;
|
|
128
128
|
/**
|
|
129
129
|
* 生成入口
|
|
130
130
|
* @param options
|
|
131
|
-
* @returns
|
|
131
|
+
* @returns Promise<void>
|
|
132
132
|
*/
|
|
133
|
-
static startup(options: GenerateOptions): Promise<void
|
|
133
|
+
static startup(options: GenerateOptions): Promise<void>;
|
|
134
|
+
static getApiDocument(url: string, retry?: number): Promise<UnkownVersionDocument>;
|
|
134
135
|
/**
|
|
135
136
|
* 生成应用
|
|
136
137
|
*/
|
|
137
|
-
static
|
|
138
|
+
static generateApplicationClient(options: GenerateApplicationOptions): Promise<GenerateClient>;
|
|
138
139
|
/**
|
|
139
140
|
* 生成对象信息
|
|
140
141
|
* @param document
|
|
141
142
|
* @param version
|
|
142
|
-
* @returns
|
|
143
|
+
* @returns GenerateClient
|
|
143
144
|
*/
|
|
144
145
|
static generateClient(document: UnkownVersionDocument, version: OpenAPIVersion): GenerateClient;
|
|
145
146
|
/**
|
package/dist/index.js
CHANGED
|
@@ -2,11 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var _chunkXXPGZHWZjs = require('./chunk-XXPGZHWZ.js');
|
|
4
4
|
|
|
5
|
-
// src/
|
|
6
|
-
var
|
|
7
|
-
var getOpenApiDocument = async (input) => {
|
|
8
|
-
return await _swaggerparser2.default.parse(input);
|
|
9
|
-
};
|
|
5
|
+
// src/generate/index.ts
|
|
6
|
+
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
10
7
|
|
|
11
8
|
// src/config/enum.config.ts
|
|
12
9
|
var OpenAPIVersion = /* @__PURE__ */ ((OpenAPIVersion2) => {
|
|
@@ -15,61 +12,9 @@ var OpenAPIVersion = /* @__PURE__ */ ((OpenAPIVersion2) => {
|
|
|
15
12
|
return OpenAPIVersion2;
|
|
16
13
|
})(OpenAPIVersion || {});
|
|
17
14
|
|
|
18
|
-
// src/utils/get-openapi-version.ts
|
|
19
|
-
function getOpenAPIVersion(document) {
|
|
20
|
-
const version = (document == null ? void 0 : document.swagger) || (document == null ? void 0 : document.openapi);
|
|
21
|
-
if (typeof version === "string") {
|
|
22
|
-
const v = Number.parseInt(version.charAt(0));
|
|
23
|
-
if (Object.values(OpenAPIVersion).includes(v)) {
|
|
24
|
-
return v;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
throw new Error(`\u65E0\u6CD5\u8BC6\u522B\u7684OPENAPI\u7248\u672C: "${String(version)}"`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// src/utils/get-services-options.ts
|
|
31
|
-
var _path = require('path'); var path2 = _interopRequireWildcard(_path); var path3 = _interopRequireWildcard(_path); var path4 = _interopRequireWildcard(_path);
|
|
32
|
-
function createOptions(options, name, application) {
|
|
33
|
-
const { service, openapi } = (() => {
|
|
34
|
-
if (!application) {
|
|
35
|
-
return {
|
|
36
|
-
service: "",
|
|
37
|
-
openapi: options.openapi
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
if (typeof application === "string") {
|
|
41
|
-
return {
|
|
42
|
-
service: application,
|
|
43
|
-
openapi: options.openapi
|
|
44
|
-
};
|
|
45
|
-
} else {
|
|
46
|
-
return {
|
|
47
|
-
service: application.key,
|
|
48
|
-
openapi: application.openapi
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
})();
|
|
52
|
-
return {
|
|
53
|
-
name,
|
|
54
|
-
application: service,
|
|
55
|
-
input: `${options.gateway}/${service}/${openapi}`.replace(/\/{2,3}/g, "/"),
|
|
56
|
-
output: name ? path2.default.join(options.output, name) : options.output,
|
|
57
|
-
exportModels: options.exportModels
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
function generateServiceOptions(options) {
|
|
61
|
-
if (options.applications && Object.keys(options.applications).length) {
|
|
62
|
-
return Object.entries(options.applications).map(
|
|
63
|
-
([name, application]) => createOptions(options, name, application)
|
|
64
|
-
);
|
|
65
|
-
} else {
|
|
66
|
-
return [createOptions(options)];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
15
|
// src/utils/get-camel-name.ts
|
|
71
16
|
function getCamelName(value) {
|
|
72
|
-
return value.replace(/^[^a-
|
|
17
|
+
return value.replace(/^[^a-z]+/gi, "").replace(/\W+/g, "_").replace(/[^\w-]+/g, "_").replace(/^\S/, (s) => s.toUpperCase()).replace(/_[a-z]/gi, (s) => s.toUpperCase()).replace(/_/g, "").trim();
|
|
73
18
|
}
|
|
74
19
|
|
|
75
20
|
// src/entities/model.ts
|
|
@@ -111,9 +56,9 @@ var Field = class {
|
|
|
111
56
|
var TYPE_MAPPINGS = /* @__PURE__ */ new Map([
|
|
112
57
|
["MapStringObject", "Record<string, any>"]
|
|
113
58
|
]);
|
|
114
|
-
|
|
59
|
+
function getBuiltInType(ref) {
|
|
115
60
|
return TYPE_MAPPINGS.get(ref);
|
|
116
|
-
}
|
|
61
|
+
}
|
|
117
62
|
|
|
118
63
|
// src/utils/get-mapped-type.ts
|
|
119
64
|
var TYPE_MAPPINGS2 = /* @__PURE__ */ new Map([
|
|
@@ -138,17 +83,17 @@ var TYPE_MAPPINGS2 = /* @__PURE__ */ new Map([
|
|
|
138
83
|
["void", "void"],
|
|
139
84
|
["null", "null"]
|
|
140
85
|
]);
|
|
141
|
-
|
|
86
|
+
function getMappedType(type = "object", format) {
|
|
142
87
|
if (format === "binary") {
|
|
143
88
|
return "binary";
|
|
144
89
|
}
|
|
145
90
|
return TYPE_MAPPINGS2.get(type) || "any";
|
|
146
|
-
}
|
|
91
|
+
}
|
|
147
92
|
|
|
148
93
|
// src/parse/v2/strip-namespace.ts
|
|
149
|
-
|
|
94
|
+
function stripNamespace(value) {
|
|
150
95
|
return value.trim().replace(/^#\/definitions\//, "").replace(/^#\/parameters\//, "").replace(/^#\/responses\//, "").replace(/^#\/securityDefinitions\//, "");
|
|
151
|
-
}
|
|
96
|
+
}
|
|
152
97
|
|
|
153
98
|
// src/parse/v2/parse-schema-type.ts
|
|
154
99
|
function parseSchemaType(schema) {
|
|
@@ -347,9 +292,8 @@ function parseParametersBody(parameters) {
|
|
|
347
292
|
parameter.ref = ref;
|
|
348
293
|
parameter.imports = imports || [];
|
|
349
294
|
return parameter;
|
|
350
|
-
} else {
|
|
351
|
-
return;
|
|
352
295
|
}
|
|
296
|
+
return void 0;
|
|
353
297
|
}
|
|
354
298
|
|
|
355
299
|
// src/parse/v2/parse-parameters-path.ts
|
|
@@ -498,9 +442,9 @@ function parseV2(document) {
|
|
|
498
442
|
}
|
|
499
443
|
|
|
500
444
|
// src/parse/v3/strip-namespace.ts
|
|
501
|
-
|
|
445
|
+
function stripNamespace2(value) {
|
|
502
446
|
return value.trim().replace(/^#\/components\/schemas\//, "").replace(/^#\/components\/responses\//, "").replace(/^#\/components\/parameters\//, "").replace(/^#\/components\/examples\//, "").replace(/^#\/components\/requestBodies\//, "").replace(/^#\/components\/headers\//, "").replace(/^#\/components\/securitySchemes\//, "").replace(/^#\/components\/links\//, "").replace(/^#\/components\/callbacks\//, "");
|
|
503
|
-
}
|
|
447
|
+
}
|
|
504
448
|
|
|
505
449
|
// src/parse/v3/parse-schema-type.ts
|
|
506
450
|
function parseSchemaType2(schema) {
|
|
@@ -543,7 +487,10 @@ function parseSchemaType2(schema) {
|
|
|
543
487
|
type: hasRef ? "any" : ofSchemaArray.map((s) => s.type).join("|"),
|
|
544
488
|
ref: hasRef ? ofSchemaArray.map((s) => s.ref && getCamelName(s.ref) || s.type).join("|") : void 0,
|
|
545
489
|
imports: hasRef ? ofSchemaArray.reduce(
|
|
546
|
-
(r, s) => (
|
|
490
|
+
(r, s) => (
|
|
491
|
+
// eslint-disable-next-line no-sequences
|
|
492
|
+
(s.ref && !r.includes(s.ref) && r.push(getCamelName(s.ref)), r)
|
|
493
|
+
),
|
|
547
494
|
[]
|
|
548
495
|
) : void 0
|
|
549
496
|
};
|
|
@@ -779,9 +726,78 @@ function parseV3(document) {
|
|
|
779
726
|
};
|
|
780
727
|
}
|
|
781
728
|
|
|
729
|
+
// src/progress.ts
|
|
730
|
+
var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk);
|
|
731
|
+
var _ora = require('ora'); var _ora2 = _interopRequireDefault(_ora);
|
|
732
|
+
var progressMaps = /* @__PURE__ */ new Map();
|
|
733
|
+
function createProgress(name) {
|
|
734
|
+
const spinner = _ora2.default.call(void 0,
|
|
735
|
+
formatProgressText(name)
|
|
736
|
+
).start();
|
|
737
|
+
progressMaps.set(name, {
|
|
738
|
+
spinner,
|
|
739
|
+
model: {
|
|
740
|
+
total: 0,
|
|
741
|
+
value: 0
|
|
742
|
+
},
|
|
743
|
+
service: {
|
|
744
|
+
total: 0,
|
|
745
|
+
value: 0
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
function startProgress(name, options) {
|
|
750
|
+
const progress = progressMaps.get(name);
|
|
751
|
+
if (progress) {
|
|
752
|
+
progress.model = {
|
|
753
|
+
total: options.models,
|
|
754
|
+
value: 0
|
|
755
|
+
};
|
|
756
|
+
progress.service = {
|
|
757
|
+
total: options.services,
|
|
758
|
+
value: 0
|
|
759
|
+
};
|
|
760
|
+
updateProgressText(name, progress);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
function formatProgressText(name, progress) {
|
|
764
|
+
const toStateText = (text) => `${_chalk2.default.green(text)}`;
|
|
765
|
+
const toNameText = (text) => `${_chalk2.default.cyan(text).padEnd(20, " ")}`;
|
|
766
|
+
const toModelText = (text) => `${_chalk2.default.greenBright("Model")}:${_chalk2.default.gray(text).padEnd(10, " ")}`;
|
|
767
|
+
const toServiceText = (text) => `${_chalk2.default.greenBright("Service")}:${_chalk2.default.gray(text).padEnd(10, " ")}`;
|
|
768
|
+
const isFinish = (progress == null ? void 0 : progress.model.value) === (progress == null ? void 0 : progress.model.total) && (progress == null ? void 0 : progress.service.value) === (progress == null ? void 0 : progress.service.total);
|
|
769
|
+
if (progress) {
|
|
770
|
+
const stateText = toStateText(isFinish ? "\u751F\u6210\u5B8C\u6210" : "\u751F\u6210\u4E2D");
|
|
771
|
+
const nameText = toNameText(name);
|
|
772
|
+
const modelText = toModelText(`${progress.model.value} / ${progress.model.total}`);
|
|
773
|
+
const serviceText = toServiceText(`${progress.service.value} / ${progress.service.total}`);
|
|
774
|
+
return `${stateText} | ${nameText} | ${modelText} | ${serviceText}`;
|
|
775
|
+
} else {
|
|
776
|
+
const stateText = toStateText("\u5F00\u59CB\u8BF7\u6C42");
|
|
777
|
+
const nameText = toNameText(name);
|
|
778
|
+
return `${stateText} | ${nameText}`;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
function updateProgressText(name, progress) {
|
|
782
|
+
const isFinish = (progress == null ? void 0 : progress.model.value) === (progress == null ? void 0 : progress.model.total) && (progress == null ? void 0 : progress.service.value) === (progress == null ? void 0 : progress.service.total);
|
|
783
|
+
if (isFinish) {
|
|
784
|
+
progress.spinner.succeed(formatProgressText(name, progress));
|
|
785
|
+
} else {
|
|
786
|
+
progress.spinner.text = formatProgressText(name, progress);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
function updateProgress(name, type) {
|
|
790
|
+
const progress = progressMaps.get(name);
|
|
791
|
+
if (!progress) {
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
progress[type].value++;
|
|
795
|
+
updateProgressText(name, progress);
|
|
796
|
+
}
|
|
797
|
+
|
|
782
798
|
// src/template.ts
|
|
783
799
|
var _fs = require('fs'); var fs = _interopRequireWildcard(_fs); var fs3 = _interopRequireWildcard(_fs); var fs2 = _interopRequireWildcard(_fs); var fs4 = _interopRequireWildcard(_fs);
|
|
784
|
-
|
|
800
|
+
var _path = require('path'); var path = _interopRequireWildcard(_path); var path3 = _interopRequireWildcard(_path); var path4 = _interopRequireWildcard(_path);
|
|
785
801
|
var _handlebars = require('handlebars'); var _handlebars2 = _interopRequireDefault(_handlebars);
|
|
786
802
|
|
|
787
803
|
// src/template-helpers/equal.helper.ts
|
|
@@ -813,7 +829,7 @@ var toUpperHelper = {
|
|
|
813
829
|
name: "to-upper",
|
|
814
830
|
fn(v1, onlyFirst) {
|
|
815
831
|
if (onlyFirst) {
|
|
816
|
-
return v1.replace(/^\S/,
|
|
832
|
+
return v1.replace(/^\S/, (s) => {
|
|
817
833
|
return s.toUpperCase();
|
|
818
834
|
});
|
|
819
835
|
} else {
|
|
@@ -858,34 +874,80 @@ function registerHandlebarPartial(input) {
|
|
|
858
874
|
_handlebars2.default.registerPartial(input, template);
|
|
859
875
|
}
|
|
860
876
|
function loadHandlebarTemplate(input) {
|
|
861
|
-
const templatePath =
|
|
877
|
+
const templatePath = path.resolve(__dirname, "templates", `${input}.hbs`);
|
|
862
878
|
return fs.readFileSync(templatePath, "utf-8");
|
|
863
879
|
}
|
|
864
880
|
|
|
881
|
+
// src/utils/get-openapi-document.ts
|
|
882
|
+
var _swaggerparser = require('@apidevtools/swagger-parser'); var _swaggerparser2 = _interopRequireDefault(_swaggerparser);
|
|
883
|
+
async function getOpenApiDocument(input) {
|
|
884
|
+
return await _swaggerparser2.default.parse(input);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
// src/utils/get-openapi-version.ts
|
|
888
|
+
function getOpenAPIVersion(document) {
|
|
889
|
+
const version = (document == null ? void 0 : document.swagger) || (document == null ? void 0 : document.openapi);
|
|
890
|
+
if (typeof version === "string") {
|
|
891
|
+
const v = Number.parseInt(version.charAt(0));
|
|
892
|
+
if (Object.values(OpenAPIVersion).includes(v)) {
|
|
893
|
+
return v;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
throw new Error(`\u65E0\u6CD5\u8BC6\u522B\u7684OPENAPI\u7248\u672C: "${String(version)}"`);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// src/utils/get-services-options.ts
|
|
900
|
+
|
|
901
|
+
function createOptions(options, name, application) {
|
|
902
|
+
const { service, openapi } = (() => {
|
|
903
|
+
if (!application) {
|
|
904
|
+
return {
|
|
905
|
+
service: "",
|
|
906
|
+
openapi: options.openapi
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
if (typeof application === "string") {
|
|
910
|
+
return {
|
|
911
|
+
service: application,
|
|
912
|
+
openapi: options.openapi
|
|
913
|
+
};
|
|
914
|
+
} else {
|
|
915
|
+
return {
|
|
916
|
+
service: application.key,
|
|
917
|
+
openapi: application.openapi
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
})();
|
|
921
|
+
return {
|
|
922
|
+
name,
|
|
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
|
+
};
|
|
928
|
+
}
|
|
929
|
+
function generateServiceOptions(options) {
|
|
930
|
+
if (options.applications && Object.keys(options.applications).length) {
|
|
931
|
+
return Object.entries(options.applications).map(
|
|
932
|
+
([name, application]) => createOptions(options, name, application)
|
|
933
|
+
);
|
|
934
|
+
} else {
|
|
935
|
+
return [createOptions(options)];
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
865
939
|
// src/generate/write-models.ts
|
|
866
940
|
|
|
867
941
|
|
|
942
|
+
var _rimraf = require('rimraf'); var _rimraf2 = _interopRequireDefault(_rimraf);
|
|
868
943
|
|
|
869
944
|
// src/generate/write-model.ts
|
|
870
945
|
|
|
871
946
|
|
|
872
947
|
// src/generate/write-file.ts
|
|
873
948
|
|
|
874
|
-
var ConsoleLogBlue = "\x1B[34m";
|
|
875
|
-
var ConsoleLogGreen = "\x1B[32m";
|
|
876
|
-
var ConsoleLogReset = "\x1B[0m";
|
|
877
949
|
function writeFile(output, content) {
|
|
878
|
-
var _a;
|
|
879
950
|
fs2.writeFileSync(output, content, "utf-8");
|
|
880
|
-
if ((_a = Generate.options) == null ? void 0 : _a.logger) {
|
|
881
|
-
console.log(
|
|
882
|
-
ConsoleLogBlue,
|
|
883
|
-
"Generate File: ",
|
|
884
|
-
ConsoleLogGreen,
|
|
885
|
-
output,
|
|
886
|
-
ConsoleLogReset
|
|
887
|
-
);
|
|
888
|
-
}
|
|
889
951
|
}
|
|
890
952
|
|
|
891
953
|
// src/generate/write-model.ts
|
|
@@ -897,7 +959,6 @@ function writeModel(model, output) {
|
|
|
897
959
|
}
|
|
898
960
|
|
|
899
961
|
// src/generate/write-models.ts
|
|
900
|
-
var _rimraf = require('rimraf'); var _rimraf2 = _interopRequireDefault(_rimraf);
|
|
901
962
|
function writeModels(client, options) {
|
|
902
963
|
if (!options.exportModels || !client.models) {
|
|
903
964
|
return;
|
|
@@ -910,6 +971,7 @@ function writeModels(client, options) {
|
|
|
910
971
|
client.models.forEach((model) => {
|
|
911
972
|
const filename = `${model.name}.ts`;
|
|
912
973
|
writeModel(model, path3.join(output, filename));
|
|
974
|
+
updateProgress(options.name || "default", "model");
|
|
913
975
|
});
|
|
914
976
|
}
|
|
915
977
|
|
|
@@ -941,6 +1003,7 @@ function writeServices(client, options) {
|
|
|
941
1003
|
const filename = `${service.name}Service.ts`;
|
|
942
1004
|
service.application = Generate.options.appendService === false ? "" : options.application;
|
|
943
1005
|
writeService(service, path4.join(output, filename));
|
|
1006
|
+
updateProgress(options.name || "default", "service");
|
|
944
1007
|
});
|
|
945
1008
|
}
|
|
946
1009
|
|
|
@@ -949,34 +1012,52 @@ var _Generate = class {
|
|
|
949
1012
|
/**
|
|
950
1013
|
* 生成入口
|
|
951
1014
|
* @param options
|
|
952
|
-
* @returns
|
|
1015
|
+
* @returns Promise<void>
|
|
953
1016
|
*/
|
|
954
|
-
static startup(options) {
|
|
1017
|
+
static async startup(options) {
|
|
955
1018
|
_Generate.options = options;
|
|
956
1019
|
registerHandlebarTemplates();
|
|
957
1020
|
const applicationOptions = generateServiceOptions(options);
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
1021
|
+
const applications = [];
|
|
1022
|
+
for (const applicationOption of applicationOptions) {
|
|
1023
|
+
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
1024
|
+
applications.push({
|
|
1025
|
+
client,
|
|
1026
|
+
options: applicationOption
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
applications.forEach((application) => {
|
|
1030
|
+
createProgress(application.options.name || "default");
|
|
1031
|
+
_Generate.writeClient(application.client, application.options);
|
|
1032
|
+
});
|
|
1033
|
+
}
|
|
1034
|
+
static async getApiDocument(url, retry = 0) {
|
|
1035
|
+
if (retry >= 3) {
|
|
1036
|
+
console.error(`\u8BF7\u6C42[${url}]\u5931\u8D25,\u8BF7\u7A0D\u540E\u91CD\u8BD5.`);
|
|
1037
|
+
_process2.default.exit(0);
|
|
1038
|
+
}
|
|
1039
|
+
try {
|
|
1040
|
+
return await getOpenApiDocument(
|
|
1041
|
+
url
|
|
1042
|
+
);
|
|
1043
|
+
} catch (e) {
|
|
1044
|
+
return _Generate.getApiDocument(url, retry + 1);
|
|
1045
|
+
}
|
|
963
1046
|
}
|
|
964
1047
|
/**
|
|
965
1048
|
* 生成应用
|
|
966
1049
|
*/
|
|
967
|
-
static async
|
|
968
|
-
const document = await
|
|
969
|
-
options.input
|
|
970
|
-
);
|
|
1050
|
+
static async generateApplicationClient(options) {
|
|
1051
|
+
const document = await _Generate.getApiDocument(options.input);
|
|
971
1052
|
const version = getOpenAPIVersion(document);
|
|
972
1053
|
const client = _Generate.generateClient(document, version);
|
|
973
|
-
|
|
1054
|
+
return client;
|
|
974
1055
|
}
|
|
975
1056
|
/**
|
|
976
1057
|
* 生成对象信息
|
|
977
1058
|
* @param document
|
|
978
1059
|
* @param version
|
|
979
|
-
* @returns
|
|
1060
|
+
* @returns GenerateClient
|
|
980
1061
|
*/
|
|
981
1062
|
static generateClient(document, version) {
|
|
982
1063
|
switch (version) {
|
|
@@ -992,6 +1073,10 @@ var _Generate = class {
|
|
|
992
1073
|
* @param options
|
|
993
1074
|
*/
|
|
994
1075
|
static writeClient(client, options) {
|
|
1076
|
+
startProgress(options.name || "default", {
|
|
1077
|
+
models: client.models.length,
|
|
1078
|
+
services: client.services.length
|
|
1079
|
+
});
|
|
995
1080
|
writeModels(client, options);
|
|
996
1081
|
writeServices(client, options);
|
|
997
1082
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__dirname,
|
|
3
3
|
__publicField
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-3T6NC7NX.mjs";
|
|
5
5
|
|
|
6
|
-
// src/
|
|
7
|
-
import
|
|
8
|
-
var getOpenApiDocument = async (input) => {
|
|
9
|
-
return await swaggerParse.parse(input);
|
|
10
|
-
};
|
|
6
|
+
// src/generate/index.ts
|
|
7
|
+
import process from "node:process";
|
|
11
8
|
|
|
12
9
|
// src/config/enum.config.ts
|
|
13
10
|
var OpenAPIVersion = /* @__PURE__ */ ((OpenAPIVersion2) => {
|
|
@@ -16,61 +13,9 @@ var OpenAPIVersion = /* @__PURE__ */ ((OpenAPIVersion2) => {
|
|
|
16
13
|
return OpenAPIVersion2;
|
|
17
14
|
})(OpenAPIVersion || {});
|
|
18
15
|
|
|
19
|
-
// src/utils/get-openapi-version.ts
|
|
20
|
-
function getOpenAPIVersion(document) {
|
|
21
|
-
const version = (document == null ? void 0 : document.swagger) || (document == null ? void 0 : document.openapi);
|
|
22
|
-
if (typeof version === "string") {
|
|
23
|
-
const v = Number.parseInt(version.charAt(0));
|
|
24
|
-
if (Object.values(OpenAPIVersion).includes(v)) {
|
|
25
|
-
return v;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
throw new Error(`\u65E0\u6CD5\u8BC6\u522B\u7684OPENAPI\u7248\u672C: "${String(version)}"`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// src/utils/get-services-options.ts
|
|
32
|
-
import path from "node:path";
|
|
33
|
-
function createOptions(options, name, application) {
|
|
34
|
-
const { service, openapi } = (() => {
|
|
35
|
-
if (!application) {
|
|
36
|
-
return {
|
|
37
|
-
service: "",
|
|
38
|
-
openapi: options.openapi
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
if (typeof application === "string") {
|
|
42
|
-
return {
|
|
43
|
-
service: application,
|
|
44
|
-
openapi: options.openapi
|
|
45
|
-
};
|
|
46
|
-
} else {
|
|
47
|
-
return {
|
|
48
|
-
service: application.key,
|
|
49
|
-
openapi: application.openapi
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
})();
|
|
53
|
-
return {
|
|
54
|
-
name,
|
|
55
|
-
application: service,
|
|
56
|
-
input: `${options.gateway}/${service}/${openapi}`.replace(/\/{2,3}/g, "/"),
|
|
57
|
-
output: name ? path.join(options.output, name) : options.output,
|
|
58
|
-
exportModels: options.exportModels
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
function generateServiceOptions(options) {
|
|
62
|
-
if (options.applications && Object.keys(options.applications).length) {
|
|
63
|
-
return Object.entries(options.applications).map(
|
|
64
|
-
([name, application]) => createOptions(options, name, application)
|
|
65
|
-
);
|
|
66
|
-
} else {
|
|
67
|
-
return [createOptions(options)];
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
16
|
// src/utils/get-camel-name.ts
|
|
72
17
|
function getCamelName(value) {
|
|
73
|
-
return value.replace(/^[^a-
|
|
18
|
+
return value.replace(/^[^a-z]+/gi, "").replace(/\W+/g, "_").replace(/[^\w-]+/g, "_").replace(/^\S/, (s) => s.toUpperCase()).replace(/_[a-z]/gi, (s) => s.toUpperCase()).replace(/_/g, "").trim();
|
|
74
19
|
}
|
|
75
20
|
|
|
76
21
|
// src/entities/model.ts
|
|
@@ -112,9 +57,9 @@ var Field = class {
|
|
|
112
57
|
var TYPE_MAPPINGS = /* @__PURE__ */ new Map([
|
|
113
58
|
["MapStringObject", "Record<string, any>"]
|
|
114
59
|
]);
|
|
115
|
-
|
|
60
|
+
function getBuiltInType(ref) {
|
|
116
61
|
return TYPE_MAPPINGS.get(ref);
|
|
117
|
-
}
|
|
62
|
+
}
|
|
118
63
|
|
|
119
64
|
// src/utils/get-mapped-type.ts
|
|
120
65
|
var TYPE_MAPPINGS2 = /* @__PURE__ */ new Map([
|
|
@@ -139,17 +84,17 @@ var TYPE_MAPPINGS2 = /* @__PURE__ */ new Map([
|
|
|
139
84
|
["void", "void"],
|
|
140
85
|
["null", "null"]
|
|
141
86
|
]);
|
|
142
|
-
|
|
87
|
+
function getMappedType(type = "object", format) {
|
|
143
88
|
if (format === "binary") {
|
|
144
89
|
return "binary";
|
|
145
90
|
}
|
|
146
91
|
return TYPE_MAPPINGS2.get(type) || "any";
|
|
147
|
-
}
|
|
92
|
+
}
|
|
148
93
|
|
|
149
94
|
// src/parse/v2/strip-namespace.ts
|
|
150
|
-
|
|
95
|
+
function stripNamespace(value) {
|
|
151
96
|
return value.trim().replace(/^#\/definitions\//, "").replace(/^#\/parameters\//, "").replace(/^#\/responses\//, "").replace(/^#\/securityDefinitions\//, "");
|
|
152
|
-
}
|
|
97
|
+
}
|
|
153
98
|
|
|
154
99
|
// src/parse/v2/parse-schema-type.ts
|
|
155
100
|
function parseSchemaType(schema) {
|
|
@@ -348,9 +293,8 @@ function parseParametersBody(parameters) {
|
|
|
348
293
|
parameter.ref = ref;
|
|
349
294
|
parameter.imports = imports || [];
|
|
350
295
|
return parameter;
|
|
351
|
-
} else {
|
|
352
|
-
return;
|
|
353
296
|
}
|
|
297
|
+
return void 0;
|
|
354
298
|
}
|
|
355
299
|
|
|
356
300
|
// src/parse/v2/parse-parameters-path.ts
|
|
@@ -499,9 +443,9 @@ function parseV2(document) {
|
|
|
499
443
|
}
|
|
500
444
|
|
|
501
445
|
// src/parse/v3/strip-namespace.ts
|
|
502
|
-
|
|
446
|
+
function stripNamespace2(value) {
|
|
503
447
|
return value.trim().replace(/^#\/components\/schemas\//, "").replace(/^#\/components\/responses\//, "").replace(/^#\/components\/parameters\//, "").replace(/^#\/components\/examples\//, "").replace(/^#\/components\/requestBodies\//, "").replace(/^#\/components\/headers\//, "").replace(/^#\/components\/securitySchemes\//, "").replace(/^#\/components\/links\//, "").replace(/^#\/components\/callbacks\//, "");
|
|
504
|
-
}
|
|
448
|
+
}
|
|
505
449
|
|
|
506
450
|
// src/parse/v3/parse-schema-type.ts
|
|
507
451
|
function parseSchemaType2(schema) {
|
|
@@ -544,7 +488,10 @@ function parseSchemaType2(schema) {
|
|
|
544
488
|
type: hasRef ? "any" : ofSchemaArray.map((s) => s.type).join("|"),
|
|
545
489
|
ref: hasRef ? ofSchemaArray.map((s) => s.ref && getCamelName(s.ref) || s.type).join("|") : void 0,
|
|
546
490
|
imports: hasRef ? ofSchemaArray.reduce(
|
|
547
|
-
(r, s) => (
|
|
491
|
+
(r, s) => (
|
|
492
|
+
// eslint-disable-next-line no-sequences
|
|
493
|
+
(s.ref && !r.includes(s.ref) && r.push(getCamelName(s.ref)), r)
|
|
494
|
+
),
|
|
548
495
|
[]
|
|
549
496
|
) : void 0
|
|
550
497
|
};
|
|
@@ -780,9 +727,78 @@ function parseV3(document) {
|
|
|
780
727
|
};
|
|
781
728
|
}
|
|
782
729
|
|
|
730
|
+
// src/progress.ts
|
|
731
|
+
import chalk from "chalk";
|
|
732
|
+
import ora from "ora";
|
|
733
|
+
var progressMaps = /* @__PURE__ */ new Map();
|
|
734
|
+
function createProgress(name) {
|
|
735
|
+
const spinner = ora(
|
|
736
|
+
formatProgressText(name)
|
|
737
|
+
).start();
|
|
738
|
+
progressMaps.set(name, {
|
|
739
|
+
spinner,
|
|
740
|
+
model: {
|
|
741
|
+
total: 0,
|
|
742
|
+
value: 0
|
|
743
|
+
},
|
|
744
|
+
service: {
|
|
745
|
+
total: 0,
|
|
746
|
+
value: 0
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
function startProgress(name, options) {
|
|
751
|
+
const progress = progressMaps.get(name);
|
|
752
|
+
if (progress) {
|
|
753
|
+
progress.model = {
|
|
754
|
+
total: options.models,
|
|
755
|
+
value: 0
|
|
756
|
+
};
|
|
757
|
+
progress.service = {
|
|
758
|
+
total: options.services,
|
|
759
|
+
value: 0
|
|
760
|
+
};
|
|
761
|
+
updateProgressText(name, progress);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
function formatProgressText(name, progress) {
|
|
765
|
+
const toStateText = (text) => `${chalk.green(text)}`;
|
|
766
|
+
const toNameText = (text) => `${chalk.cyan(text).padEnd(20, " ")}`;
|
|
767
|
+
const toModelText = (text) => `${chalk.greenBright("Model")}:${chalk.gray(text).padEnd(10, " ")}`;
|
|
768
|
+
const toServiceText = (text) => `${chalk.greenBright("Service")}:${chalk.gray(text).padEnd(10, " ")}`;
|
|
769
|
+
const isFinish = (progress == null ? void 0 : progress.model.value) === (progress == null ? void 0 : progress.model.total) && (progress == null ? void 0 : progress.service.value) === (progress == null ? void 0 : progress.service.total);
|
|
770
|
+
if (progress) {
|
|
771
|
+
const stateText = toStateText(isFinish ? "\u751F\u6210\u5B8C\u6210" : "\u751F\u6210\u4E2D");
|
|
772
|
+
const nameText = toNameText(name);
|
|
773
|
+
const modelText = toModelText(`${progress.model.value} / ${progress.model.total}`);
|
|
774
|
+
const serviceText = toServiceText(`${progress.service.value} / ${progress.service.total}`);
|
|
775
|
+
return `${stateText} | ${nameText} | ${modelText} | ${serviceText}`;
|
|
776
|
+
} else {
|
|
777
|
+
const stateText = toStateText("\u5F00\u59CB\u8BF7\u6C42");
|
|
778
|
+
const nameText = toNameText(name);
|
|
779
|
+
return `${stateText} | ${nameText}`;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
function updateProgressText(name, progress) {
|
|
783
|
+
const isFinish = (progress == null ? void 0 : progress.model.value) === (progress == null ? void 0 : progress.model.total) && (progress == null ? void 0 : progress.service.value) === (progress == null ? void 0 : progress.service.total);
|
|
784
|
+
if (isFinish) {
|
|
785
|
+
progress.spinner.succeed(formatProgressText(name, progress));
|
|
786
|
+
} else {
|
|
787
|
+
progress.spinner.text = formatProgressText(name, progress);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
function updateProgress(name, type) {
|
|
791
|
+
const progress = progressMaps.get(name);
|
|
792
|
+
if (!progress) {
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
progress[type].value++;
|
|
796
|
+
updateProgressText(name, progress);
|
|
797
|
+
}
|
|
798
|
+
|
|
783
799
|
// src/template.ts
|
|
784
800
|
import * as fs from "node:fs";
|
|
785
|
-
import * as
|
|
801
|
+
import * as path from "node:path";
|
|
786
802
|
import Handlebars from "handlebars";
|
|
787
803
|
|
|
788
804
|
// src/template-helpers/equal.helper.ts
|
|
@@ -814,7 +830,7 @@ var toUpperHelper = {
|
|
|
814
830
|
name: "to-upper",
|
|
815
831
|
fn(v1, onlyFirst) {
|
|
816
832
|
if (onlyFirst) {
|
|
817
|
-
return v1.replace(/^\S/,
|
|
833
|
+
return v1.replace(/^\S/, (s) => {
|
|
818
834
|
return s.toUpperCase();
|
|
819
835
|
});
|
|
820
836
|
} else {
|
|
@@ -859,34 +875,80 @@ function registerHandlebarPartial(input) {
|
|
|
859
875
|
Handlebars.registerPartial(input, template);
|
|
860
876
|
}
|
|
861
877
|
function loadHandlebarTemplate(input) {
|
|
862
|
-
const templatePath =
|
|
878
|
+
const templatePath = path.resolve(__dirname, "templates", `${input}.hbs`);
|
|
863
879
|
return fs.readFileSync(templatePath, "utf-8");
|
|
864
880
|
}
|
|
865
881
|
|
|
882
|
+
// src/utils/get-openapi-document.ts
|
|
883
|
+
import swaggerParse from "@apidevtools/swagger-parser";
|
|
884
|
+
async function getOpenApiDocument(input) {
|
|
885
|
+
return await swaggerParse.parse(input);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// src/utils/get-openapi-version.ts
|
|
889
|
+
function getOpenAPIVersion(document) {
|
|
890
|
+
const version = (document == null ? void 0 : document.swagger) || (document == null ? void 0 : document.openapi);
|
|
891
|
+
if (typeof version === "string") {
|
|
892
|
+
const v = Number.parseInt(version.charAt(0));
|
|
893
|
+
if (Object.values(OpenAPIVersion).includes(v)) {
|
|
894
|
+
return v;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
throw new Error(`\u65E0\u6CD5\u8BC6\u522B\u7684OPENAPI\u7248\u672C: "${String(version)}"`);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// src/utils/get-services-options.ts
|
|
901
|
+
import path2 from "node:path";
|
|
902
|
+
function createOptions(options, name, application) {
|
|
903
|
+
const { service, openapi } = (() => {
|
|
904
|
+
if (!application) {
|
|
905
|
+
return {
|
|
906
|
+
service: "",
|
|
907
|
+
openapi: options.openapi
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
if (typeof application === "string") {
|
|
911
|
+
return {
|
|
912
|
+
service: application,
|
|
913
|
+
openapi: options.openapi
|
|
914
|
+
};
|
|
915
|
+
} else {
|
|
916
|
+
return {
|
|
917
|
+
service: application.key,
|
|
918
|
+
openapi: application.openapi
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
})();
|
|
922
|
+
return {
|
|
923
|
+
name,
|
|
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
|
+
};
|
|
929
|
+
}
|
|
930
|
+
function generateServiceOptions(options) {
|
|
931
|
+
if (options.applications && Object.keys(options.applications).length) {
|
|
932
|
+
return Object.entries(options.applications).map(
|
|
933
|
+
([name, application]) => createOptions(options, name, application)
|
|
934
|
+
);
|
|
935
|
+
} else {
|
|
936
|
+
return [createOptions(options)];
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
866
940
|
// src/generate/write-models.ts
|
|
867
941
|
import * as fs3 from "node:fs";
|
|
868
942
|
import * as path3 from "node:path";
|
|
943
|
+
import rimraf from "rimraf";
|
|
869
944
|
|
|
870
945
|
// src/generate/write-model.ts
|
|
871
946
|
import Handlebars2 from "handlebars";
|
|
872
947
|
|
|
873
948
|
// src/generate/write-file.ts
|
|
874
949
|
import * as fs2 from "node:fs";
|
|
875
|
-
var ConsoleLogBlue = "\x1B[34m";
|
|
876
|
-
var ConsoleLogGreen = "\x1B[32m";
|
|
877
|
-
var ConsoleLogReset = "\x1B[0m";
|
|
878
950
|
function writeFile(output, content) {
|
|
879
|
-
var _a;
|
|
880
951
|
fs2.writeFileSync(output, content, "utf-8");
|
|
881
|
-
if ((_a = Generate.options) == null ? void 0 : _a.logger) {
|
|
882
|
-
console.log(
|
|
883
|
-
ConsoleLogBlue,
|
|
884
|
-
"Generate File: ",
|
|
885
|
-
ConsoleLogGreen,
|
|
886
|
-
output,
|
|
887
|
-
ConsoleLogReset
|
|
888
|
-
);
|
|
889
|
-
}
|
|
890
952
|
}
|
|
891
953
|
|
|
892
954
|
// src/generate/write-model.ts
|
|
@@ -898,7 +960,6 @@ function writeModel(model, output) {
|
|
|
898
960
|
}
|
|
899
961
|
|
|
900
962
|
// src/generate/write-models.ts
|
|
901
|
-
import rimraf from "rimraf";
|
|
902
963
|
function writeModels(client, options) {
|
|
903
964
|
if (!options.exportModels || !client.models) {
|
|
904
965
|
return;
|
|
@@ -911,12 +972,13 @@ function writeModels(client, options) {
|
|
|
911
972
|
client.models.forEach((model) => {
|
|
912
973
|
const filename = `${model.name}.ts`;
|
|
913
974
|
writeModel(model, path3.join(output, filename));
|
|
975
|
+
updateProgress(options.name || "default", "model");
|
|
914
976
|
});
|
|
915
977
|
}
|
|
916
978
|
|
|
917
979
|
// src/generate/write-services.ts
|
|
918
|
-
import * as path4 from "node:path";
|
|
919
980
|
import * as fs4 from "node:fs";
|
|
981
|
+
import * as path4 from "node:path";
|
|
920
982
|
import rimraf2 from "rimraf";
|
|
921
983
|
|
|
922
984
|
// src/generate/write-service.ts
|
|
@@ -942,6 +1004,7 @@ function writeServices(client, options) {
|
|
|
942
1004
|
const filename = `${service.name}Service.ts`;
|
|
943
1005
|
service.application = Generate.options.appendService === false ? "" : options.application;
|
|
944
1006
|
writeService(service, path4.join(output, filename));
|
|
1007
|
+
updateProgress(options.name || "default", "service");
|
|
945
1008
|
});
|
|
946
1009
|
}
|
|
947
1010
|
|
|
@@ -950,34 +1013,52 @@ var _Generate = class {
|
|
|
950
1013
|
/**
|
|
951
1014
|
* 生成入口
|
|
952
1015
|
* @param options
|
|
953
|
-
* @returns
|
|
1016
|
+
* @returns Promise<void>
|
|
954
1017
|
*/
|
|
955
|
-
static startup(options) {
|
|
1018
|
+
static async startup(options) {
|
|
956
1019
|
_Generate.options = options;
|
|
957
1020
|
registerHandlebarTemplates();
|
|
958
1021
|
const applicationOptions = generateServiceOptions(options);
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
1022
|
+
const applications = [];
|
|
1023
|
+
for (const applicationOption of applicationOptions) {
|
|
1024
|
+
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
1025
|
+
applications.push({
|
|
1026
|
+
client,
|
|
1027
|
+
options: applicationOption
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
applications.forEach((application) => {
|
|
1031
|
+
createProgress(application.options.name || "default");
|
|
1032
|
+
_Generate.writeClient(application.client, application.options);
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
static async getApiDocument(url, retry = 0) {
|
|
1036
|
+
if (retry >= 3) {
|
|
1037
|
+
console.error(`\u8BF7\u6C42[${url}]\u5931\u8D25,\u8BF7\u7A0D\u540E\u91CD\u8BD5.`);
|
|
1038
|
+
process.exit(0);
|
|
1039
|
+
}
|
|
1040
|
+
try {
|
|
1041
|
+
return await getOpenApiDocument(
|
|
1042
|
+
url
|
|
1043
|
+
);
|
|
1044
|
+
} catch {
|
|
1045
|
+
return _Generate.getApiDocument(url, retry + 1);
|
|
1046
|
+
}
|
|
964
1047
|
}
|
|
965
1048
|
/**
|
|
966
1049
|
* 生成应用
|
|
967
1050
|
*/
|
|
968
|
-
static async
|
|
969
|
-
const document = await
|
|
970
|
-
options.input
|
|
971
|
-
);
|
|
1051
|
+
static async generateApplicationClient(options) {
|
|
1052
|
+
const document = await _Generate.getApiDocument(options.input);
|
|
972
1053
|
const version = getOpenAPIVersion(document);
|
|
973
1054
|
const client = _Generate.generateClient(document, version);
|
|
974
|
-
|
|
1055
|
+
return client;
|
|
975
1056
|
}
|
|
976
1057
|
/**
|
|
977
1058
|
* 生成对象信息
|
|
978
1059
|
* @param document
|
|
979
1060
|
* @param version
|
|
980
|
-
* @returns
|
|
1061
|
+
* @returns GenerateClient
|
|
981
1062
|
*/
|
|
982
1063
|
static generateClient(document, version) {
|
|
983
1064
|
switch (version) {
|
|
@@ -993,6 +1074,10 @@ var _Generate = class {
|
|
|
993
1074
|
* @param options
|
|
994
1075
|
*/
|
|
995
1076
|
static writeClient(client, options) {
|
|
1077
|
+
startProgress(options.name || "default", {
|
|
1078
|
+
models: client.models.length,
|
|
1079
|
+
services: client.services.length
|
|
1080
|
+
});
|
|
996
1081
|
writeModels(client, options);
|
|
997
1082
|
writeServices(client, options);
|
|
998
1083
|
}
|
|
@@ -140,7 +140,7 @@ var vite_plugin_default = (options) => {
|
|
|
140
140
|
function getServicePaths(options) {
|
|
141
141
|
const paths = [];
|
|
142
142
|
const walk = (dir) => {
|
|
143
|
-
fs.readdirSync(dir).forEach(
|
|
143
|
+
fs.readdirSync(dir).forEach((file) => {
|
|
144
144
|
const fullpath = path.join(dir, file).replace(/\\/g, "/");
|
|
145
145
|
const stat = fs.statSync(fullpath);
|
|
146
146
|
if (stat.isFile() && fullpath.endsWith("Service.ts")) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import "../chunk-
|
|
1
|
+
import "../chunk-3T6NC7NX.mjs";
|
|
2
2
|
|
|
3
3
|
// src/vite-plugin/index.ts
|
|
4
|
-
import * as fs from "fs";
|
|
5
|
-
import * as path from "path";
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import * as path from "node:path";
|
|
6
6
|
import Handlebars from "handlebars";
|
|
7
7
|
|
|
8
8
|
// src/vite-plugin/templates.ts
|
|
@@ -140,7 +140,7 @@ var vite_plugin_default = (options) => {
|
|
|
140
140
|
function getServicePaths(options) {
|
|
141
141
|
const paths = [];
|
|
142
142
|
const walk = (dir) => {
|
|
143
|
-
fs.readdirSync(dir).forEach(
|
|
143
|
+
fs.readdirSync(dir).forEach((file) => {
|
|
144
144
|
const fullpath = path.join(dir, file).replace(/\\/g, "/");
|
|
145
145
|
const stat = fs.statSync(fullpath);
|
|
146
146
|
if (stat.isFile() && fullpath.endsWith("Service.ts")) {
|
package/package.json
CHANGED
|
@@ -1,43 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gopowerteam/request-generate",
|
|
3
|
-
"private": false,
|
|
4
|
-
"version": "0.1.24",
|
|
5
3
|
"type": "commonjs",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"bin",
|
|
9
|
-
"vite-plugin.d.ts",
|
|
10
|
-
"README.md"
|
|
11
|
-
],
|
|
12
|
-
"bin": {
|
|
13
|
-
"request-generate": "bin/index.mts"
|
|
14
|
-
},
|
|
4
|
+
"version": "0.1.25",
|
|
5
|
+
"private": false,
|
|
15
6
|
"keywords": [
|
|
16
7
|
"gopowerteam",
|
|
17
8
|
"request-generate"
|
|
18
9
|
],
|
|
19
|
-
"publishConfig": {
|
|
20
|
-
"registry": "https://registry.npmjs.org",
|
|
21
|
-
"access": "public"
|
|
22
|
-
},
|
|
23
|
-
"main": "./dist/index.js",
|
|
24
|
-
"module": "./dist/index.mjs",
|
|
25
|
-
"types": "./dist/index.d.ts",
|
|
26
10
|
"exports": {
|
|
27
11
|
".": {
|
|
28
|
-
"
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
29
13
|
"import": "./dist/index.mjs",
|
|
30
|
-
"
|
|
14
|
+
"require": "./dist/index.js"
|
|
31
15
|
},
|
|
32
16
|
"./vite-plugin": {
|
|
33
|
-
"
|
|
17
|
+
"types": "./dist/vite-plugin/index.d.ts",
|
|
34
18
|
"import": "./dist/vite-plugin/index.mjs",
|
|
35
|
-
"
|
|
19
|
+
"require": "./dist/vite-plugin/index.js"
|
|
36
20
|
}
|
|
37
21
|
},
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"module": "./dist/index.mjs",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"bin": {
|
|
26
|
+
"request-generate": "bin/index.mts"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"README.md",
|
|
30
|
+
"bin",
|
|
31
|
+
"dist",
|
|
32
|
+
"vite-plugin.d.ts"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"registry": "https://registry.npmjs.org",
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@apidevtools/swagger-parser": "^10.1.0",
|
|
40
|
+
"chalk": "^5.3.0",
|
|
41
|
+
"commander": "^10.0.0",
|
|
42
|
+
"handlebars": "^4.7.8",
|
|
43
|
+
"ora": "^8.1.0",
|
|
44
|
+
"qs": "^6.11.0",
|
|
45
|
+
"rimraf": "^4.1.2",
|
|
46
|
+
"rxjs": "^7.8.0",
|
|
47
|
+
"tsx": "^4.7.0"
|
|
48
|
+
},
|
|
38
49
|
"devDependencies": {
|
|
39
50
|
"@types/jest": "^29.4.0",
|
|
40
|
-
"@types/node": "
|
|
51
|
+
"@types/node": "20",
|
|
41
52
|
"@types/qs": "^6.9.7",
|
|
42
53
|
"@types/rimraf": "^3.0.2",
|
|
43
54
|
"fs-extra": "^11.1.0",
|
|
@@ -47,18 +58,10 @@
|
|
|
47
58
|
"tsup": "^6.6.3",
|
|
48
59
|
"typescript": "^4.9.5",
|
|
49
60
|
"vite": "^4.1.4",
|
|
50
|
-
"@gopowerteam/request": "0.1.
|
|
51
|
-
},
|
|
52
|
-
"dependencies": {
|
|
53
|
-
"@apidevtools/swagger-parser": "^10.1.0",
|
|
54
|
-
"commander": "^10.0.0",
|
|
55
|
-
"handlebars": "^4.7.8",
|
|
56
|
-
"qs": "^6.11.0",
|
|
57
|
-
"rimraf": "^4.1.2",
|
|
58
|
-
"rxjs": "^7.8.0",
|
|
59
|
-
"tsx": "^4.7.0"
|
|
61
|
+
"@gopowerteam/request": "0.1.20"
|
|
60
62
|
},
|
|
61
63
|
"scripts": {
|
|
62
|
-
"build": "tsup"
|
|
64
|
+
"build": "tsup",
|
|
65
|
+
"lint": "eslint . --fix"
|
|
63
66
|
}
|
|
64
67
|
}
|