open-api-typescript-request-generator 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -13
- package/es/Generator.js +29 -195
- package/es/cli.d.ts +3 -1
- package/es/cli.js +63 -23
- package/es/constants.d.ts +0 -1
- package/es/constants.js +1 -2
- package/es/genIndex.js +4 -6
- package/es/genRequest.js +4 -10
- package/es/helpers.d.ts +1 -1
- package/es/helpers.js +1 -8
- package/es/responseDataJsonSchemaHandler.js +1 -1
- package/es/types.d.ts +170 -372
- package/es/types.js +24 -24
- package/es/utils.d.ts +4 -10
- package/es/utils.js +8 -44
- package/lib/Generator.js +29 -195
- package/lib/cli.d.ts +3 -1
- package/lib/cli.js +63 -23
- package/lib/constants.d.ts +0 -1
- package/lib/constants.js +1 -2
- package/lib/genIndex.js +4 -6
- package/lib/genRequest.js +4 -10
- package/lib/helpers.d.ts +1 -1
- package/lib/helpers.js +1 -8
- package/lib/responseDataJsonSchemaHandler.js +1 -1
- package/lib/types.d.ts +170 -372
- package/lib/types.js +24 -24
- package/lib/utils.d.ts +4 -10
- package/lib/utils.js +8 -44
- package/package.json +3 -2
- package/es/requestYapiData.d.ts +0 -157
- package/es/requestYapiData.js +0 -1010
- package/lib/requestYapiData.d.ts +0 -157
- package/lib/requestYapiData.js +0 -1010
package/es/types.js
CHANGED
|
@@ -31,7 +31,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
31
31
|
exports.ResponseBodyType = exports.RequestFormItemType = exports.RequestQueryType = exports.RequestParamType = exports.RequestBodyType = exports.Required = exports.Method = void 0;
|
|
32
32
|
|
|
33
33
|
__exportStar(require("json-schema"), exports);
|
|
34
|
-
/**
|
|
34
|
+
/** Request method */
|
|
35
35
|
|
|
36
36
|
|
|
37
37
|
var Method;
|
|
@@ -45,82 +45,82 @@ var Method;
|
|
|
45
45
|
Method["OPTIONS"] = "OPTIONS";
|
|
46
46
|
Method["PATCH"] = "PATCH";
|
|
47
47
|
})(Method = exports.Method || (exports.Method = {}));
|
|
48
|
-
/**
|
|
48
|
+
/** Required */
|
|
49
49
|
|
|
50
50
|
|
|
51
51
|
var Required;
|
|
52
52
|
|
|
53
53
|
(function (Required) {
|
|
54
|
-
/**
|
|
54
|
+
/** Not required */
|
|
55
55
|
Required["false"] = "0";
|
|
56
|
-
/**
|
|
56
|
+
/** Required */
|
|
57
57
|
|
|
58
58
|
Required["true"] = "1";
|
|
59
59
|
})(Required = exports.Required || (exports.Required = {}));
|
|
60
|
-
/**
|
|
60
|
+
/** Request body type */
|
|
61
61
|
|
|
62
62
|
|
|
63
63
|
var RequestBodyType;
|
|
64
64
|
|
|
65
65
|
(function (RequestBodyType) {
|
|
66
|
-
/**
|
|
66
|
+
/** Query string */
|
|
67
67
|
RequestBodyType["query"] = "query";
|
|
68
|
-
/**
|
|
68
|
+
/** Form */
|
|
69
69
|
|
|
70
70
|
RequestBodyType["form"] = "form";
|
|
71
71
|
/** JSON */
|
|
72
72
|
|
|
73
73
|
RequestBodyType["json"] = "json";
|
|
74
|
-
/**
|
|
74
|
+
/** Plain text */
|
|
75
75
|
|
|
76
76
|
RequestBodyType["text"] = "text";
|
|
77
|
-
/**
|
|
77
|
+
/** File */
|
|
78
78
|
|
|
79
79
|
RequestBodyType["file"] = "file";
|
|
80
|
-
/**
|
|
80
|
+
/** Raw data */
|
|
81
81
|
|
|
82
82
|
RequestBodyType["raw"] = "raw";
|
|
83
|
-
/**
|
|
83
|
+
/** No request data */
|
|
84
84
|
|
|
85
85
|
RequestBodyType["none"] = "none";
|
|
86
86
|
})(RequestBodyType = exports.RequestBodyType || (exports.RequestBodyType = {}));
|
|
87
|
-
/**
|
|
87
|
+
/** Request path parameter type */
|
|
88
88
|
|
|
89
89
|
|
|
90
90
|
var RequestParamType;
|
|
91
91
|
|
|
92
92
|
(function (RequestParamType) {
|
|
93
|
-
/**
|
|
93
|
+
/** String */
|
|
94
94
|
RequestParamType["string"] = "string";
|
|
95
|
-
/**
|
|
95
|
+
/** Number */
|
|
96
96
|
|
|
97
97
|
RequestParamType["number"] = "number";
|
|
98
98
|
})(RequestParamType = exports.RequestParamType || (exports.RequestParamType = {}));
|
|
99
|
-
/**
|
|
99
|
+
/** Request query parameter type */
|
|
100
100
|
|
|
101
101
|
|
|
102
102
|
var RequestQueryType;
|
|
103
103
|
|
|
104
104
|
(function (RequestQueryType) {
|
|
105
|
-
/**
|
|
105
|
+
/** String */
|
|
106
106
|
RequestQueryType["string"] = "string";
|
|
107
|
-
/**
|
|
107
|
+
/** Number */
|
|
108
108
|
|
|
109
109
|
RequestQueryType["number"] = "number";
|
|
110
110
|
})(RequestQueryType = exports.RequestQueryType || (exports.RequestQueryType = {}));
|
|
111
|
-
/**
|
|
111
|
+
/** Request form item type */
|
|
112
112
|
|
|
113
113
|
|
|
114
114
|
var RequestFormItemType;
|
|
115
115
|
|
|
116
116
|
(function (RequestFormItemType) {
|
|
117
|
-
/**
|
|
117
|
+
/** Plain text */
|
|
118
118
|
RequestFormItemType["text"] = "text";
|
|
119
|
-
/**
|
|
119
|
+
/** File */
|
|
120
120
|
|
|
121
121
|
RequestFormItemType["file"] = "file";
|
|
122
122
|
})(RequestFormItemType = exports.RequestFormItemType || (exports.RequestFormItemType = {}));
|
|
123
|
-
/**
|
|
123
|
+
/** Response body type */
|
|
124
124
|
|
|
125
125
|
|
|
126
126
|
var ResponseBodyType;
|
|
@@ -128,15 +128,15 @@ var ResponseBodyType;
|
|
|
128
128
|
(function (ResponseBodyType) {
|
|
129
129
|
/** JSON */
|
|
130
130
|
ResponseBodyType["json"] = "json";
|
|
131
|
-
/**
|
|
131
|
+
/** Plain text */
|
|
132
132
|
|
|
133
133
|
ResponseBodyType["text"] = "text";
|
|
134
134
|
/** XML */
|
|
135
135
|
|
|
136
136
|
ResponseBodyType["xml"] = "xml";
|
|
137
|
-
/**
|
|
137
|
+
/** Raw data */
|
|
138
138
|
|
|
139
|
-
ResponseBodyType["raw"] = "raw"; // yapi
|
|
139
|
+
ResponseBodyType["raw"] = "raw"; // yapi actually returns json, with another field indicating whether it is json schema
|
|
140
140
|
|
|
141
141
|
/** JSON Schema */
|
|
142
142
|
// jsonSchema = 'json-schema',
|
package/es/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import prettier from 'prettier';
|
|
2
|
-
import { Interface, PropDefinitions
|
|
2
|
+
import { Interface, PropDefinitions } from './types';
|
|
3
3
|
import { JSONSchema4 } from 'json-schema';
|
|
4
4
|
/**
|
|
5
5
|
* 抛出错误。
|
|
@@ -61,7 +61,7 @@ export declare function propDefinitionsToJsonSchema(propDefinitions: PropDefinit
|
|
|
61
61
|
* 获取prettier配置
|
|
62
62
|
* @returns
|
|
63
63
|
*/
|
|
64
|
-
export declare function getPrettier(
|
|
64
|
+
export declare function getPrettier(): prettier.Options;
|
|
65
65
|
export declare function preprocessSchema(schema: JSONSchema4): JSONSchema4;
|
|
66
66
|
/**
|
|
67
67
|
* 根据 JSONSchema 对象生产 TypeScript 类型定义。
|
|
@@ -72,7 +72,7 @@ export declare function preprocessSchema(schema: JSONSchema4): JSONSchema4;
|
|
|
72
72
|
*/
|
|
73
73
|
export declare function jsonSchemaToTsCode(jsonSchema: JSONSchema4, typeName: string): Promise<string>;
|
|
74
74
|
export declare function getRequestDataJsonSchema(interfaceInfo: Interface): JSONSchema4;
|
|
75
|
-
export declare function getResponseDataJsonSchema(interfaceInfo: Interface
|
|
75
|
+
export declare function getResponseDataJsonSchema(interfaceInfo: Interface): JSONSchema4;
|
|
76
76
|
export declare function sortByWeights<T extends {
|
|
77
77
|
weights: number[];
|
|
78
78
|
}>(list: T[]): T[];
|
|
@@ -83,15 +83,9 @@ export declare function sortByWeights<T extends {
|
|
|
83
83
|
* @returns
|
|
84
84
|
* https://prettier.io/docs/en/options.html
|
|
85
85
|
*/
|
|
86
|
-
export declare function formatContent(content: string
|
|
86
|
+
export declare function formatContent(content: string): string;
|
|
87
87
|
/**
|
|
88
88
|
* 通用生成文件顶部注释
|
|
89
89
|
* @returns
|
|
90
90
|
*/
|
|
91
91
|
export declare function topNotesContent(): string;
|
|
92
|
-
/**
|
|
93
|
-
* filter处理函数
|
|
94
|
-
* @param interfaceFilter
|
|
95
|
-
* @returns
|
|
96
|
-
*/
|
|
97
|
-
export declare const filterHandler: (interfaceFilter?: Config['filter']) => (path: string, id?: number) => boolean;
|
package/es/utils.js
CHANGED
|
@@ -180,7 +180,7 @@ var __importDefault = this && this.__importDefault || function (mod) {
|
|
|
180
180
|
Object.defineProperty(exports, "__esModule", {
|
|
181
181
|
value: true
|
|
182
182
|
});
|
|
183
|
-
exports.
|
|
183
|
+
exports.topNotesContent = exports.formatContent = exports.sortByWeights = exports.getResponseDataJsonSchema = exports.getRequestDataJsonSchema = exports.jsonSchemaToTsCode = exports.preprocessSchema = exports.getPrettier = exports.propDefinitionsToJsonSchema = exports.mockjsTemplateToJsonSchema = exports.jsonToJsonSchema = exports.jsonSchemaStringToJsonSchema = exports.processJsonSchema = exports.getNormalizedRelativePath = exports.toUnixPath = exports.throwError = void 0;
|
|
184
184
|
|
|
185
185
|
var json5_1 = __importDefault(require("json5"));
|
|
186
186
|
|
|
@@ -427,13 +427,8 @@ exports.propDefinitionsToJsonSchema = propDefinitionsToJsonSchema;
|
|
|
427
427
|
* @returns
|
|
428
428
|
*/
|
|
429
429
|
|
|
430
|
-
function getPrettier(
|
|
431
|
-
|
|
432
|
-
var configPath = prettier_1.default.resolveConfigFile.sync(filePath && path_1.default.resolve(filePath) || process.cwd());
|
|
433
|
-
var config = configPath && prettier_1.default.resolveConfig.sync(configPath) || {};
|
|
434
|
-
return config ? __assign({
|
|
435
|
-
parser: 'babel-ts'
|
|
436
|
-
}, config) : {
|
|
430
|
+
function getPrettier() {
|
|
431
|
+
return {
|
|
437
432
|
printWidth: 120,
|
|
438
433
|
tabWidth: 2,
|
|
439
434
|
singleQuote: true,
|
|
@@ -678,7 +673,7 @@ function getRequestDataJsonSchema(interfaceInfo) {
|
|
|
678
673
|
|
|
679
674
|
exports.getRequestDataJsonSchema = getRequestDataJsonSchema;
|
|
680
675
|
|
|
681
|
-
function getResponseDataJsonSchema(interfaceInfo
|
|
676
|
+
function getResponseDataJsonSchema(interfaceInfo) {
|
|
682
677
|
var jsonSchema = {};
|
|
683
678
|
|
|
684
679
|
switch (interfaceInfo.res_body_type) {
|
|
@@ -695,12 +690,6 @@ function getResponseDataJsonSchema(interfaceInfo, dataKey) {
|
|
|
695
690
|
};
|
|
696
691
|
break;
|
|
697
692
|
}
|
|
698
|
-
/* istanbul ignore if */
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
if (dataKey && jsonSchema && jsonSchema.properties && jsonSchema.properties[dataKey]) {
|
|
702
|
-
jsonSchema = jsonSchema.properties[dataKey];
|
|
703
|
-
}
|
|
704
693
|
|
|
705
694
|
return jsonSchema;
|
|
706
695
|
}
|
|
@@ -738,9 +727,9 @@ exports.sortByWeights = sortByWeights;
|
|
|
738
727
|
* https://prettier.io/docs/en/options.html
|
|
739
728
|
*/
|
|
740
729
|
|
|
741
|
-
function formatContent(content
|
|
730
|
+
function formatContent(content) {
|
|
742
731
|
// 从项目中获取prettier配置文件
|
|
743
|
-
var config = getPrettier(
|
|
732
|
+
var config = getPrettier();
|
|
744
733
|
var prettyOutputContent = prettier_1.default.format(content, config);
|
|
745
734
|
return prettyOutputContent;
|
|
746
735
|
}
|
|
@@ -752,32 +741,7 @@ exports.formatContent = formatContent;
|
|
|
752
741
|
*/
|
|
753
742
|
|
|
754
743
|
function topNotesContent() {
|
|
755
|
-
return "\n /**\n * Created By api-
|
|
744
|
+
return "\n /**\n * Created By open-api-typescript-request-generator\n */\n\n ";
|
|
756
745
|
}
|
|
757
746
|
|
|
758
|
-
exports.topNotesContent = topNotesContent;
|
|
759
|
-
/**
|
|
760
|
-
* filter处理函数
|
|
761
|
-
* @param interfaceFilter
|
|
762
|
-
* @returns
|
|
763
|
-
*/
|
|
764
|
-
|
|
765
|
-
var filterHandler = function filterHandler(interfaceFilter) {
|
|
766
|
-
return function (path, id) {
|
|
767
|
-
if (interfaceFilter instanceof RegExp && !interfaceFilter.test(path)) {
|
|
768
|
-
return false;
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
if (interfaceFilter instanceof Array && !interfaceFilter.includes(path)) {
|
|
772
|
-
return false;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
if (interfaceFilter instanceof Function && !interfaceFilter(path, id)) {
|
|
776
|
-
return false;
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
return true;
|
|
780
|
-
};
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
exports.filterHandler = filterHandler;
|
|
747
|
+
exports.topNotesContent = topNotesContent;
|
package/lib/Generator.js
CHANGED
|
@@ -254,8 +254,6 @@ var child_process_1 = require("child_process");
|
|
|
254
254
|
|
|
255
255
|
var utils_1 = require("./utils");
|
|
256
256
|
|
|
257
|
-
var genIndex_1 = __importDefault(require("./genIndex"));
|
|
258
|
-
|
|
259
257
|
var responseDataJsonSchemaHandler_1 = require("./responseDataJsonSchemaHandler");
|
|
260
258
|
|
|
261
259
|
var getOutputPath_1 = require("./getOutputPath");
|
|
@@ -263,8 +261,8 @@ var getOutputPath_1 = require("./getOutputPath");
|
|
|
263
261
|
var genRequest_1 = __importDefault(require("./genRequest")); // 默认顶部依赖生成模板
|
|
264
262
|
|
|
265
263
|
|
|
266
|
-
function
|
|
267
|
-
return "import request from '
|
|
264
|
+
function defaultTopImportTemplate(config) {
|
|
265
|
+
return "import request from './request'";
|
|
268
266
|
}
|
|
269
267
|
|
|
270
268
|
var getDataKeySetStr = function getDataKeySetStr(method) {
|
|
@@ -273,7 +271,7 @@ var getDataKeySetStr = function getDataKeySetStr(method) {
|
|
|
273
271
|
}
|
|
274
272
|
|
|
275
273
|
return 'data';
|
|
276
|
-
}; //
|
|
274
|
+
}; // 处理路径参数
|
|
277
275
|
|
|
278
276
|
|
|
279
277
|
function handlePathParam(path) {
|
|
@@ -287,7 +285,6 @@ function handlePathParam(path) {
|
|
|
287
285
|
|
|
288
286
|
|
|
289
287
|
function defaultRequestFunctionTemplate(props, config) {
|
|
290
|
-
var requestFunctionExtraParams = (config || {}).requestFunctionExtraParams;
|
|
291
288
|
var baseURL = props.baseURL,
|
|
292
289
|
requestFunctionName = props.requestFunctionName,
|
|
293
290
|
requestDataTypeName = props.requestDataTypeName,
|
|
@@ -306,32 +303,7 @@ function defaultRequestFunctionTemplate(props, config) {
|
|
|
306
303
|
finalBaseUrl = "\"".concat(baseURL, "\"");
|
|
307
304
|
}
|
|
308
305
|
|
|
309
|
-
return "export const ".concat(requestFunctionName, " = (data").concat(hasData ? '' : '?', ": ").concat(requestDataTypeName).concat(
|
|
310
|
-
} // 后台统一网关函数体生成模板
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
function adminRequestFunctionTemplate(props, config) {
|
|
314
|
-
var _a;
|
|
315
|
-
|
|
316
|
-
var baseURL = props.baseURL,
|
|
317
|
-
requestFunctionName = props.requestFunctionName,
|
|
318
|
-
requestDataTypeName = props.requestDataTypeName,
|
|
319
|
-
responseDataTypeName = props.responseDataTypeName,
|
|
320
|
-
extendedInterfaceInfo = props.extendedInterfaceInfo;
|
|
321
|
-
var req_params = extendedInterfaceInfo.req_params,
|
|
322
|
-
req_query = extendedInterfaceInfo.req_query;
|
|
323
|
-
var hasData = req_params.length || req_query.length;
|
|
324
|
-
var finalBaseUrl = '';
|
|
325
|
-
|
|
326
|
-
if (baseURL === null || baseURL === void 0 ? void 0 : baseURL.match(/^\[code\]:/)) {
|
|
327
|
-
// 如果使用[code]开头则表示,作为代码段执行,否则仅作为字符串
|
|
328
|
-
finalBaseUrl = baseURL.replace(/^\[code\]:/, '');
|
|
329
|
-
} else {
|
|
330
|
-
finalBaseUrl = "\"".concat(baseURL, "\"");
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
var url = ((_a = config === null || config === void 0 ? void 0 : config.proxyInterface) === null || _a === void 0 ? void 0 : _a.path) || '/proxy';
|
|
334
|
-
return "export const ".concat(requestFunctionName, " = (data").concat(hasData ? '' : '?', ": ").concat(requestDataTypeName, ") => {\n return request.post<").concat(requestDataTypeName, ",").concat(responseDataTypeName, ">( '").concat(url, "', {\n data:{\n real_url: '").concat(extendedInterfaceInfo.path, "',\n params: data\n },\n ").concat(baseURL ? "baseURL: ".concat(finalBaseUrl) : '', "\n })\n }");
|
|
306
|
+
return "export const ".concat(requestFunctionName, " = (data").concat(hasData ? '' : '?', ": ").concat(requestDataTypeName).concat(",extra?:Record<string,any>", ") => {\n return request.").concat(method, "<").concat(requestDataTypeName, ",").concat(responseDataTypeName, ">(").concat(handlePathParam(extendedInterfaceInfo.path), ", {\n ").concat(getDataKeySetStr(method), ",\n ").concat(baseURL ? "baseURL: ".concat(finalBaseUrl, ",") : '', "\n ").concat("...extra", "\n })\n }");
|
|
335
307
|
}
|
|
336
308
|
|
|
337
309
|
var Generator =
|
|
@@ -379,7 +351,7 @@ function () {
|
|
|
379
351
|
|
|
380
352
|
Generator.prototype.generate = function () {
|
|
381
353
|
return __awaiter(this, void 0, void 0, function () {
|
|
382
|
-
var outputFileList, _a,
|
|
354
|
+
var outputFileList, _a, serverUrl, configIndex, name, typesName, openApiV3Json, componentsCode, allApi, interfaceList, categoryCode, categoryResponseDataJsonSchemaContent, _i, interfaceList_1, interfaceInfo, _b, code, responseDataJsonSchema, catOutputFilePath;
|
|
383
355
|
|
|
384
356
|
var _this = this;
|
|
385
357
|
|
|
@@ -387,7 +359,7 @@ function () {
|
|
|
387
359
|
switch (_c.label) {
|
|
388
360
|
case 0:
|
|
389
361
|
outputFileList = Object.create(null);
|
|
390
|
-
_a = this.config,
|
|
362
|
+
_a = this.config, serverUrl = _a.serverUrl, configIndex = _a.configIndex, name = _a.name;
|
|
391
363
|
typesName = name || '_types_' + (configIndex + 1);
|
|
392
364
|
return [4
|
|
393
365
|
/*yield*/
|
|
@@ -443,7 +415,7 @@ function () {
|
|
|
443
415
|
interfaceInfo = interfaceList_1[_i];
|
|
444
416
|
return [4
|
|
445
417
|
/*yield*/
|
|
446
|
-
, this.generateInterfaceCode(__assign(__assign(
|
|
418
|
+
, this.generateInterfaceCode(__assign(__assign({}, this.config), {
|
|
447
419
|
components: openApiV3Json.components
|
|
448
420
|
}), interfaceInfo)];
|
|
449
421
|
|
|
@@ -470,7 +442,7 @@ function () {
|
|
|
470
442
|
content: categoryCode,
|
|
471
443
|
outputResponseDataJsonSchemaFilePath: (0, getOutputPath_1.getOutputFilePath)(this.config, "/".concat(typesName, "/responseDataJsonSchema.ts")),
|
|
472
444
|
responseDataJsonSchemaContent: categoryResponseDataJsonSchemaContent,
|
|
473
|
-
requestFunctionFilePath:
|
|
445
|
+
requestFunctionFilePath: path_1.default.join(path_1.default.dirname(catOutputFilePath), 'request.ts'),
|
|
474
446
|
requestHookMakerFilePath: ''
|
|
475
447
|
};
|
|
476
448
|
}
|
|
@@ -519,15 +491,6 @@ function () {
|
|
|
519
491
|
// config.getRequestFunctionName;
|
|
520
492
|
// this.requestFunctionNameGen;
|
|
521
493
|
// 生成 request.ts
|
|
522
|
-
_a.sent(); // 生成入口 index.ts
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
return [4
|
|
526
|
-
/*yield*/
|
|
527
|
-
, (0, genIndex_1.default)(config, projects)];
|
|
528
|
-
|
|
529
|
-
case 2:
|
|
530
|
-
// 生成入口 index.ts
|
|
531
494
|
_a.sent();
|
|
532
495
|
|
|
533
496
|
outputContent = '';
|
|
@@ -535,7 +498,7 @@ function () {
|
|
|
535
498
|
/*return*/
|
|
536
499
|
, Promise.all(Object.keys(outputFileList).map(function (outputFilePath, index) {
|
|
537
500
|
return __awaiter(_this, void 0, void 0, function () {
|
|
538
|
-
var _a, content, requestFunctionFilePath, requestHookMakerFilePath, syntheticalConfig, outputResponseDataJsonSchemaFilePath, responseDataJsonSchemaContent,
|
|
501
|
+
var _a, content, requestFunctionFilePath, requestHookMakerFilePath, syntheticalConfig, outputResponseDataJsonSchemaFilePath, responseDataJsonSchemaContent, topImportTemplate, rawOutputContent;
|
|
539
502
|
|
|
540
503
|
return __generator(this, function (_b) {
|
|
541
504
|
switch (_b.label) {
|
|
@@ -545,9 +508,9 @@ function () {
|
|
|
545
508
|
outputFilePath = outputFilePath.replace(/\.js(x)?$/, '.ts$1');
|
|
546
509
|
requestFunctionFilePath = requestFunctionFilePath.replace(/\.js(x)?$/, '.ts$1');
|
|
547
510
|
requestHookMakerFilePath = requestHookMakerFilePath.replace(/\.js(x)?$/, '.ts$1');
|
|
548
|
-
|
|
549
|
-
rawOutputContent = (0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n\n ", "\n "])), (0, utils_1.topNotesContent)(),
|
|
550
|
-
outputContent += (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["", ""], ["", ""])), rawOutputContent)
|
|
511
|
+
topImportTemplate = syntheticalConfig.topImportTemplate || defaultTopImportTemplate;
|
|
512
|
+
rawOutputContent = (0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n\n ", "\n "])), (0, utils_1.topNotesContent)(), topImportTemplate(config), content.join('\n\n').trim());
|
|
513
|
+
outputContent += (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["", ""], ["", ""])), rawOutputContent));
|
|
551
514
|
if (!(Object.keys(outputFileList).length - 1 === index)) return [3
|
|
552
515
|
/*break*/
|
|
553
516
|
, 2];
|
|
@@ -561,26 +524,6 @@ function () {
|
|
|
561
524
|
_b.label = 2;
|
|
562
525
|
|
|
563
526
|
case 2:
|
|
564
|
-
if (!(syntheticalConfig.target === 'javascript')) return [3
|
|
565
|
-
/*break*/
|
|
566
|
-
, 5];
|
|
567
|
-
return [4
|
|
568
|
-
/*yield*/
|
|
569
|
-
, this.tsc(outputFilePath)];
|
|
570
|
-
|
|
571
|
-
case 3:
|
|
572
|
-
_b.sent();
|
|
573
|
-
|
|
574
|
-
return [4
|
|
575
|
-
/*yield*/
|
|
576
|
-
, Promise.all([fs_extra_1.default.remove(requestFunctionFilePath).catch(vtils_1.noop), fs_extra_1.default.remove(requestHookMakerFilePath).catch(vtils_1.noop), fs_extra_1.default.remove(outputFilePath).catch(vtils_1.noop)])];
|
|
577
|
-
|
|
578
|
-
case 4:
|
|
579
|
-
_b.sent();
|
|
580
|
-
|
|
581
|
-
_b.label = 5;
|
|
582
|
-
|
|
583
|
-
case 5:
|
|
584
527
|
return [2
|
|
585
528
|
/*return*/
|
|
586
529
|
];
|
|
@@ -629,73 +572,18 @@ function () {
|
|
|
629
572
|
|
|
630
573
|
Generator.prototype.generateInterfaceCode = function (syntheticalConfig, interfaceInfo) {
|
|
631
574
|
return __awaiter(this, void 0, void 0, function () {
|
|
632
|
-
var extendedInterfaceInfo, requestFunctionName,
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
switch (_f.label) {
|
|
575
|
+
var extendedInterfaceInfo, requestFunctionName, requestConfigName, requestConfigTypeName, requestDataTypeName, responseDataTypeName, requestDataJsonSchema, requestDataType, responseDataJsonSchema, responseDataType, genComment, requestFunctionTemplate, baseURL, baseUrl, code;
|
|
576
|
+
return __generator(this, function (_a) {
|
|
577
|
+
switch (_a.label) {
|
|
636
578
|
case 0:
|
|
637
579
|
extendedInterfaceInfo = __assign(__assign({}, interfaceInfo), {
|
|
638
580
|
parsedPath: path_1.default.parse(interfaceInfo.path)
|
|
639
581
|
});
|
|
640
|
-
|
|
641
|
-
/*break*/
|
|
642
|
-
, 2];
|
|
643
|
-
return [4
|
|
644
|
-
/*yield*/
|
|
645
|
-
, syntheticalConfig.getRequestFunctionName(extendedInterfaceInfo, changeCase)];
|
|
646
|
-
|
|
647
|
-
case 1:
|
|
648
|
-
_a = _f.sent();
|
|
649
|
-
return [3
|
|
650
|
-
/*break*/
|
|
651
|
-
, 3];
|
|
652
|
-
|
|
653
|
-
case 2:
|
|
654
|
-
_a = this.requestFunctionNameGen(extendedInterfaceInfo);
|
|
655
|
-
_f.label = 3;
|
|
656
|
-
|
|
657
|
-
case 3:
|
|
658
|
-
requestFunctionName = _a;
|
|
582
|
+
requestFunctionName = this.requestFunctionNameGen(extendedInterfaceInfo);
|
|
659
583
|
requestConfigName = changeCase.camelCase("".concat(requestFunctionName, "RequestConfig"));
|
|
660
584
|
requestConfigTypeName = changeCase.pascalCase(requestConfigName);
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
, 5];
|
|
664
|
-
return [4
|
|
665
|
-
/*yield*/
|
|
666
|
-
, syntheticalConfig.getRequestDataTypeName(extendedInterfaceInfo, changeCase)];
|
|
667
|
-
|
|
668
|
-
case 4:
|
|
669
|
-
_b = _f.sent();
|
|
670
|
-
return [3
|
|
671
|
-
/*break*/
|
|
672
|
-
, 6];
|
|
673
|
-
|
|
674
|
-
case 5:
|
|
675
|
-
_b = changeCase.pascalCase("".concat(requestFunctionName, "Request"));
|
|
676
|
-
_f.label = 6;
|
|
677
|
-
|
|
678
|
-
case 6:
|
|
679
|
-
requestDataTypeName = _b;
|
|
680
|
-
if (!(0, vtils_1.isFunction)(syntheticalConfig.getResponseDataTypeName)) return [3
|
|
681
|
-
/*break*/
|
|
682
|
-
, 8];
|
|
683
|
-
return [4
|
|
684
|
-
/*yield*/
|
|
685
|
-
, syntheticalConfig.getResponseDataTypeName(extendedInterfaceInfo, changeCase)];
|
|
686
|
-
|
|
687
|
-
case 7:
|
|
688
|
-
_c = _f.sent();
|
|
689
|
-
return [3
|
|
690
|
-
/*break*/
|
|
691
|
-
, 9];
|
|
692
|
-
|
|
693
|
-
case 8:
|
|
694
|
-
_c = changeCase.pascalCase("".concat(requestFunctionName, "Response"));
|
|
695
|
-
_f.label = 9;
|
|
696
|
-
|
|
697
|
-
case 9:
|
|
698
|
-
responseDataTypeName = _c;
|
|
585
|
+
requestDataTypeName = changeCase.pascalCase("".concat(requestFunctionName, "Request"));
|
|
586
|
+
responseDataTypeName = changeCase.pascalCase("".concat(requestFunctionName, "Response"));
|
|
699
587
|
requestDataJsonSchema = (0, utils_1.getRequestDataJsonSchema)(extendedInterfaceInfo);
|
|
700
588
|
return [4
|
|
701
589
|
/*yield*/
|
|
@@ -703,82 +591,34 @@ function () {
|
|
|
703
591
|
components: syntheticalConfig.components
|
|
704
592
|
}), requestDataTypeName)];
|
|
705
593
|
|
|
706
|
-
case
|
|
707
|
-
requestDataType =
|
|
594
|
+
case 1:
|
|
595
|
+
requestDataType = _a.sent();
|
|
708
596
|
|
|
709
597
|
if (interfaceInfo.path.includes('/path')) {
|
|
710
598
|
console.log(requestDataType);
|
|
711
599
|
}
|
|
712
600
|
|
|
713
|
-
responseDataJsonSchema = (0, utils_1.getResponseDataJsonSchema)(extendedInterfaceInfo
|
|
601
|
+
responseDataJsonSchema = (0, utils_1.getResponseDataJsonSchema)(extendedInterfaceInfo);
|
|
714
602
|
return [4
|
|
715
603
|
/*yield*/
|
|
716
604
|
, (0, utils_1.jsonSchemaToTsCode)(__assign(__assign({}, responseDataJsonSchema), {
|
|
717
605
|
components: syntheticalConfig.components
|
|
718
606
|
}), responseDataTypeName)];
|
|
719
607
|
|
|
720
|
-
case
|
|
721
|
-
responseDataType =
|
|
608
|
+
case 2:
|
|
609
|
+
responseDataType = _a.sent();
|
|
722
610
|
|
|
723
611
|
if (interfaceInfo.path.includes('/path')) {
|
|
724
612
|
console.log(requestDataType);
|
|
725
613
|
}
|
|
726
614
|
|
|
727
|
-
isRequestDataOptional = /(\{\}|any)$/.test(requestDataType);
|
|
728
|
-
if (!(syntheticalConfig.reactHooks && syntheticalConfig.reactHooks.enabled)) return [3
|
|
729
|
-
/*break*/
|
|
730
|
-
, 15];
|
|
731
|
-
if (!(0, vtils_1.isFunction)(syntheticalConfig.reactHooks.getRequestHookName)) return [3
|
|
732
|
-
/*break*/
|
|
733
|
-
, 13];
|
|
734
|
-
return [4
|
|
735
|
-
/*yield*/
|
|
736
|
-
, syntheticalConfig.reactHooks.getRequestHookName(extendedInterfaceInfo, changeCase)];
|
|
737
|
-
|
|
738
|
-
case 12:
|
|
739
|
-
_e = _f.sent();
|
|
740
|
-
return [3
|
|
741
|
-
/*break*/
|
|
742
|
-
, 14];
|
|
743
|
-
|
|
744
|
-
case 13:
|
|
745
|
-
_e = "use".concat(changeCase.pascalCase(requestFunctionName));
|
|
746
|
-
_f.label = 14;
|
|
747
|
-
|
|
748
|
-
case 14:
|
|
749
|
-
_d = _e;
|
|
750
|
-
return [3
|
|
751
|
-
/*break*/
|
|
752
|
-
, 16];
|
|
753
|
-
|
|
754
|
-
case 15:
|
|
755
|
-
_d = '';
|
|
756
|
-
_f.label = 16;
|
|
757
|
-
|
|
758
|
-
case 16:
|
|
759
|
-
requestHookName = _d;
|
|
760
|
-
paramNames = (extendedInterfaceInfo.req_params
|
|
761
|
-
/* istanbul ignore next */
|
|
762
|
-
|| []).map(function (item) {
|
|
763
|
-
return item.name;
|
|
764
|
-
});
|
|
765
|
-
paramNamesLiteral = JSON.stringify(paramNames);
|
|
766
|
-
paramNameType = paramNames.length === 0 ? 'string' : "'".concat(paramNames.join("' | '"), "'");
|
|
767
|
-
queryNames = (extendedInterfaceInfo.req_query
|
|
768
|
-
/* istanbul ignore next */
|
|
769
|
-
|| []).map(function (item) {
|
|
770
|
-
return item.name;
|
|
771
|
-
});
|
|
772
|
-
queryNamesLiteral = JSON.stringify(queryNames);
|
|
773
|
-
queryNameType = queryNames.length === 0 ? 'string' : "'".concat(queryNames.join("' | '"), "'");
|
|
774
|
-
|
|
775
615
|
genComment = function genComment(genTitle) {
|
|
776
|
-
var _a =
|
|
616
|
+
var _a = {
|
|
777
617
|
// Swagger 时总是禁用标签、更新时间、链接
|
|
778
618
|
tag: false,
|
|
779
619
|
updateTime: false,
|
|
780
620
|
link: false
|
|
781
|
-
}
|
|
621
|
+
},
|
|
782
622
|
_b = _a.enabled,
|
|
783
623
|
isEnabled = _b === void 0 ? true : _b,
|
|
784
624
|
_c = _a.title,
|
|
@@ -823,16 +663,10 @@ function () {
|
|
|
823
663
|
"`".concat((0, dayjs_1.default)(extendedInterfaceInfo.up_time * 1000).format('YYYY-MM-DD HH:mm:ss'), "`")
|
|
824
664
|
}];
|
|
825
665
|
var titleComment = hasTitle ? (0, vtils_1.dedent)(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n * ", "\n *\n "], ["\n * ", "\n *\n "])), genTitle(description)) : '';
|
|
826
|
-
var extraComment = summary.filter(function (item) {
|
|
827
|
-
return typeof item !== 'boolean' && !(0, vtils_1.isEmpty)(item.value);
|
|
828
|
-
}).map(function (item) {
|
|
829
|
-
var _item = item;
|
|
830
|
-
return "* @".concat(_item.label, " ").concat((0, vtils_1.castArray)(_item.value).join(', '));
|
|
831
|
-
}).join('\n');
|
|
832
666
|
return (0, vtils_1.dedent)(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n /**\n ", "\n */\n "], ["\n /**\n ", "\n */\n "])), [titleComment].filter(Boolean).join('\n'));
|
|
833
667
|
};
|
|
834
668
|
|
|
835
|
-
requestFunctionTemplate =
|
|
669
|
+
requestFunctionTemplate = defaultRequestFunctionTemplate;
|
|
836
670
|
baseURL = syntheticalConfig.baseURL;
|
|
837
671
|
|
|
838
672
|
try {
|
|
@@ -842,10 +676,10 @@ function () {
|
|
|
842
676
|
}
|
|
843
677
|
|
|
844
678
|
code = (0, vtils_1.dedent)(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n "])), genComment(function (title) {
|
|
845
|
-
return "".concat(title, "
|
|
679
|
+
return "".concat(title, " request parameters");
|
|
846
680
|
}), requestDataType.trim(), genComment(function (title) {
|
|
847
|
-
return "".concat(title, "
|
|
848
|
-
}), responseDataType.trim(),
|
|
681
|
+
return "".concat(title, " response data");
|
|
682
|
+
}), responseDataType.trim(), (0, vtils_1.dedent)(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n ", "\n ", "\n "], ["\n ", "\n ", "\n "])), genComment(function (title) {
|
|
849
683
|
return "".concat(title);
|
|
850
684
|
}), requestFunctionTemplate({
|
|
851
685
|
baseURL: baseUrl,
|
package/lib/cli.d.ts
CHANGED
|
@@ -9,7 +9,9 @@ export declare function getConfig(): Promise<{
|
|
|
9
9
|
configTSFileExist: boolean;
|
|
10
10
|
}>;
|
|
11
11
|
export declare function genConfig(): Promise<void>;
|
|
12
|
-
export declare function start(
|
|
12
|
+
export declare function start(options?: {
|
|
13
|
+
name?: string;
|
|
14
|
+
}): Promise<void>;
|
|
13
15
|
export default class CLI {
|
|
14
16
|
argvs: any;
|
|
15
17
|
run(args: any, callback?: yargs.ParseCallback): {
|