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/lib/cli.js
CHANGED
|
@@ -229,15 +229,11 @@ var conso = __importStar(require("./console"));
|
|
|
229
229
|
|
|
230
230
|
var utils_1 = require("./utils");
|
|
231
231
|
|
|
232
|
-
var genIndex_1 = require("./genIndex");
|
|
233
|
-
|
|
234
232
|
var spinner_1 = require("./spinner");
|
|
235
233
|
|
|
236
234
|
var helpers_1 = require("./helpers");
|
|
237
235
|
|
|
238
236
|
TSNode.register({
|
|
239
|
-
// 不加载本地的 tsconfig.json
|
|
240
|
-
// skipProject: true,
|
|
241
237
|
// 仅转译,不做类型检查
|
|
242
238
|
transpileOnly: true,
|
|
243
239
|
// 自定义编译选项
|
|
@@ -299,7 +295,7 @@ exports.getConfig = getConfig;
|
|
|
299
295
|
|
|
300
296
|
function genConfig() {
|
|
301
297
|
return __awaiter(this, void 0, void 0, function () {
|
|
302
|
-
var _a, configTSFile, configTSFileExist, answers,
|
|
298
|
+
var _a, configTSFile, configTSFileExist, answers, configAnswers;
|
|
303
299
|
|
|
304
300
|
return __generator(this, function (_b) {
|
|
305
301
|
switch (_b.label) {
|
|
@@ -333,6 +329,11 @@ function genConfig() {
|
|
|
333
329
|
return [4
|
|
334
330
|
/*yield*/
|
|
335
331
|
, (0, prompts_1.default)([{
|
|
332
|
+
message: '接口文件路径名称',
|
|
333
|
+
name: 'name',
|
|
334
|
+
type: 'text',
|
|
335
|
+
initial: ''
|
|
336
|
+
}, {
|
|
336
337
|
message: '接口信息服务地址',
|
|
337
338
|
name: 'url',
|
|
338
339
|
type: 'text',
|
|
@@ -340,10 +341,10 @@ function genConfig() {
|
|
|
340
341
|
}])];
|
|
341
342
|
|
|
342
343
|
case 4:
|
|
343
|
-
|
|
344
|
+
configAnswers = _b.sent();
|
|
344
345
|
return [4
|
|
345
346
|
/*yield*/
|
|
346
|
-
, fs_extra_1.default.outputFile(configTSFile, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n import { defineConfig } from 'api-
|
|
347
|
+
, fs_extra_1.default.outputFile(configTSFile, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n import { defineConfig } from 'open-api-typescript-request-generator'\n\n export default defineConfig([{\n name: '", "',\n serverUrl: '", "',\n outputFilePath: 'src/api',\n baseURL: '[code]:process.env.BASE_API_URL',\n topImportTemplate: () => \"", "\",\n defaultRequestLib: true,\n }])\n "], ["\n import { defineConfig } from 'open-api-typescript-request-generator'\n\n export default defineConfig([{\n name: '", "',\n serverUrl: '", "',\n outputFilePath: 'src/api',\n baseURL: '[code]:process.env.BASE_API_URL',\n topImportTemplate: () => \"", "\",\n defaultRequestLib: true,\n }])\n "])), (configAnswers === null || configAnswers === void 0 ? void 0 : configAnswers.name) || '', (configAnswers === null || configAnswers === void 0 ? void 0 : configAnswers.url) || '', "import request from './request'")))];
|
|
347
348
|
|
|
348
349
|
case 5:
|
|
349
350
|
_b.sent();
|
|
@@ -411,9 +412,13 @@ function startGenerate(config, cwd, index) {
|
|
|
411
412
|
});
|
|
412
413
|
}
|
|
413
414
|
|
|
414
|
-
function start() {
|
|
415
|
+
function start(options) {
|
|
416
|
+
if (options === void 0) {
|
|
417
|
+
options = {};
|
|
418
|
+
}
|
|
419
|
+
|
|
415
420
|
return __awaiter(this, void 0, void 0, function () {
|
|
416
|
-
var timeLabel, _a, cwd, configFileExist, configFile, configTSFile, config, err_1;
|
|
421
|
+
var timeLabel, _a, cwd, configFileExist, configFile, configTSFile, config, configToRun, err_1;
|
|
417
422
|
|
|
418
423
|
var _this = this;
|
|
419
424
|
|
|
@@ -442,28 +447,35 @@ function start() {
|
|
|
442
447
|
_b.trys.push([2, 4,, 5]);
|
|
443
448
|
|
|
444
449
|
config = require(configFile).default;
|
|
450
|
+
configToRun = config;
|
|
451
|
+
|
|
452
|
+
if (options.name) {
|
|
453
|
+
configToRun = config.filter(function (c) {
|
|
454
|
+
return c.name === options.name;
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
if (configToRun.length === 0) {
|
|
458
|
+
return [2
|
|
459
|
+
/*return*/
|
|
460
|
+
, conso.error("\u672A\u627E\u5230 name \u4E3A ".concat(options.name, " \u7684\u914D\u7F6E"))];
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
445
464
|
spinner_1.spinnerInstance.start('正在获取数据并生成代码... \n');
|
|
446
465
|
return [4
|
|
447
466
|
/*yield*/
|
|
448
|
-
, (0, helpers_1.asyncFnArrayOrderRun)(
|
|
467
|
+
, (0, helpers_1.asyncFnArrayOrderRun)(configToRun.map(function (configItem, index) {
|
|
449
468
|
return function () {
|
|
450
469
|
return __awaiter(_this, void 0, void 0, function () {
|
|
451
470
|
return __generator(this, function (_a) {
|
|
452
471
|
switch (_a.label) {
|
|
453
472
|
case 0:
|
|
454
|
-
return [4
|
|
455
|
-
/*yield*/
|
|
456
|
-
, (0, genIndex_1.prepareIndexFile)(configItem)];
|
|
457
|
-
|
|
458
|
-
case 1:
|
|
459
|
-
_a.sent();
|
|
460
|
-
|
|
461
473
|
configItem.configIndex = index;
|
|
462
474
|
return [4
|
|
463
475
|
/*yield*/
|
|
464
476
|
, startGenerate(configItem, cwd, index)];
|
|
465
477
|
|
|
466
|
-
case
|
|
478
|
+
case 1:
|
|
467
479
|
_a.sent();
|
|
468
480
|
|
|
469
481
|
return [2
|
|
@@ -531,12 +543,40 @@ function () {
|
|
|
531
543
|
};
|
|
532
544
|
|
|
533
545
|
CLI.prototype.init = function () {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
546
|
+
var _this = this;
|
|
547
|
+
|
|
548
|
+
return yargs_1.default.scriptName('apits-gener').usage('Usage: $0 <command> [options]').command('gen', '生成接口类型声明和方法', function (y) {
|
|
549
|
+
y.option('name', {
|
|
550
|
+
alias: 'n',
|
|
551
|
+
type: 'string',
|
|
552
|
+
description: '只生成指定 name 的配置'
|
|
553
|
+
});
|
|
554
|
+
}, function (argv) {
|
|
555
|
+
var name = argv.name;
|
|
556
|
+
start({
|
|
557
|
+
name: name
|
|
558
|
+
});
|
|
537
559
|
}).command('init', '生成配置文件', function (y) {}, function (argv) {
|
|
538
|
-
|
|
539
|
-
|
|
560
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
561
|
+
var _a;
|
|
562
|
+
|
|
563
|
+
return __generator(this, function (_b) {
|
|
564
|
+
switch (_b.label) {
|
|
565
|
+
case 0:
|
|
566
|
+
_a = argv;
|
|
567
|
+
return [4
|
|
568
|
+
/*yield*/
|
|
569
|
+
, genConfig()];
|
|
570
|
+
|
|
571
|
+
case 1:
|
|
572
|
+
_b.sent();
|
|
573
|
+
|
|
574
|
+
return [2
|
|
575
|
+
/*return*/
|
|
576
|
+
];
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
});
|
|
540
580
|
}).help();
|
|
541
581
|
};
|
|
542
582
|
|
package/lib/constants.d.ts
CHANGED
package/lib/constants.js
CHANGED
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.yapiApiToken = exports.yapiApiExport = exports.yapiApiProjectMenuList = exports.yapiApiProjectInterfaceList = exports.yapiApiCatInterfaceList = exports.yapiApiInterfaceDetail = exports.yapiApiGetProject = exports.yapiApiGetMenu = exports.ResponseErrorCode = exports.
|
|
6
|
+
exports.yapiApiToken = exports.yapiApiExport = exports.yapiApiProjectMenuList = exports.yapiApiProjectInterfaceList = exports.yapiApiCatInterfaceList = exports.yapiApiInterfaceDetail = exports.yapiApiGetProject = exports.yapiApiGetMenu = exports.ResponseErrorCode = exports.DefaultServerUrl = void 0;
|
|
7
7
|
exports.DefaultServerUrl = '';
|
|
8
|
-
exports.CookieStoreFile = '/Users/Shared/api-ts-generator/yapiCookies.json';
|
|
9
8
|
var ResponseErrorCode;
|
|
10
9
|
|
|
11
10
|
(function (ResponseErrorCode) {
|
package/lib/genIndex.js
CHANGED
|
@@ -252,11 +252,10 @@ exports.prepareIndexFile = prepareIndexFile;
|
|
|
252
252
|
|
|
253
253
|
exports.default = function (config, categoryList) {
|
|
254
254
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
255
|
-
var
|
|
255
|
+
var indexFilePath, originFileContent, exportAllInterface, content;
|
|
256
256
|
return __generator(this, function (_a) {
|
|
257
257
|
switch (_a.label) {
|
|
258
258
|
case 0:
|
|
259
|
-
prettierConfigPath = config.prettierConfigPath;
|
|
260
259
|
indexFilePath = (0, getOutputPath_1.getOutputFilePath)(config, 'index.ts');
|
|
261
260
|
originFileContent = '';
|
|
262
261
|
return [4
|
|
@@ -281,7 +280,7 @@ exports.default = function (config, categoryList) {
|
|
|
281
280
|
}, []);
|
|
282
281
|
content = "\n ".concat(exportAllInterface.join(';'), "\n "); // 输出index文件
|
|
283
282
|
|
|
284
|
-
fs_extra_1.default.appendFile(indexFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["", ""], ["", ""])), content)
|
|
283
|
+
fs_extra_1.default.appendFile(indexFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["", ""], ["", ""])), content)));
|
|
285
284
|
return [2
|
|
286
285
|
/*return*/
|
|
287
286
|
];
|
|
@@ -314,11 +313,10 @@ exports.getIndexGitInfo = getIndexGitInfo;
|
|
|
314
313
|
|
|
315
314
|
var genGitRepoIndex = function genGitRepoIndex(config, filePathList, notes) {
|
|
316
315
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
317
|
-
var
|
|
316
|
+
var indexFilePath, originFileContent, exportAllInterface, content;
|
|
318
317
|
return __generator(this, function (_a) {
|
|
319
318
|
switch (_a.label) {
|
|
320
319
|
case 0:
|
|
321
|
-
prettierConfigPath = config.prettierConfigPath;
|
|
322
320
|
indexFilePath = (0, getOutputPath_1.getOutputFilePath)(config, 'index.ts');
|
|
323
321
|
originFileContent = '';
|
|
324
322
|
return [4
|
|
@@ -341,7 +339,7 @@ var genGitRepoIndex = function genGitRepoIndex(config, filePathList, notes) {
|
|
|
341
339
|
}, []);
|
|
342
340
|
content = "\n ".concat(exportAllInterface.join(';'), "\n "); // 输出index文件
|
|
343
341
|
|
|
344
|
-
fs_extra_1.default.appendFile(indexFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_3 || (templateObject_3 = __makeTemplateObject(["", ""], ["", ""])), content)
|
|
342
|
+
fs_extra_1.default.appendFile(indexFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_3 || (templateObject_3 = __makeTemplateObject(["", ""], ["", ""])), content)));
|
|
345
343
|
return [2
|
|
346
344
|
/*return*/
|
|
347
345
|
];
|
package/lib/genRequest.js
CHANGED
|
@@ -176,18 +176,15 @@ var utils_1 = require("./utils");
|
|
|
176
176
|
|
|
177
177
|
exports.default = function (config) {
|
|
178
178
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
179
|
-
var
|
|
179
|
+
var defaultRequestLib, rawRequestFunctionFilePath, content;
|
|
180
180
|
return __generator(this, function (_a) {
|
|
181
181
|
switch (_a.label) {
|
|
182
182
|
case 0:
|
|
183
|
-
|
|
183
|
+
defaultRequestLib = config.defaultRequestLib;
|
|
184
184
|
if (defaultRequestLib === false) return [2
|
|
185
185
|
/*return*/
|
|
186
186
|
];
|
|
187
187
|
rawRequestFunctionFilePath = (0, getOutputPath_1.getOutputFilePath)(config, 'request.ts');
|
|
188
|
-
if (!!config.typesOnly) return [3
|
|
189
|
-
/*break*/
|
|
190
|
-
, 2];
|
|
191
188
|
return [4
|
|
192
189
|
/*yield*/
|
|
193
190
|
, fs_extra_1.default.pathExists(rawRequestFunctionFilePath)];
|
|
@@ -200,11 +197,8 @@ exports.default = function (config) {
|
|
|
200
197
|
];
|
|
201
198
|
}
|
|
202
199
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
case 2:
|
|
206
|
-
content = "\n ".concat((0, utils_1.topNotesContent)(), "\n\n import request,{ AxiosRequestConfig } from 'axios'; // axios\u7248\u672C>=0.18.1\n\n const instance = request.create({\n withCredentials: true,\n baseURL: process.env.BASE_URL,\n });\n\n // \u81EA\u5B9A\u4E49request\u62E6\u622A\u5668\n instance.interceptors.request.use((config) => {\n return {\n ...config\n }\n });\n\n // \u81EA\u5B9A\u4E49response\u62E6\u622A\u5668\uFF0C\n // \u6CE8\u610F\uFF1A\u5982\u679C\u4FEE\u6539\u63A5\u53E3\u6B63\u5E38\u8FD4\u56DE\u7684\u7ED3\u6784\uFF0C\u5BF9\u5E94\u7684response\u58F0\u660E\u9700\u8981\u4FEE\u6539\n instance.interceptors.response.use((r) => {\n const { data, config } = r;\n if (data.code === 0) {\n\n return data.data;\n }\n return Promise.reject(data);\n });\n\n export default {\n get: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.get<RP>(url, config);\n },\n post: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.post<RP>(url, config);\n }\n };\n");
|
|
207
|
-
fs_extra_1.default.outputFile(rawRequestFunctionFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["", ""], ["", ""])), content), prettierConfigPath));
|
|
200
|
+
content = "\n ".concat((0, utils_1.topNotesContent)(), "\n\n import request,{ AxiosRequestConfig } from 'axios'; // axios\u7248\u672C>=0.18.1\n\n const instance = request.create({\n withCredentials: true,\n baseURL: process.env.BASE_URL,\n });\n\n // \u81EA\u5B9A\u4E49request\u62E6\u622A\u5668\n instance.interceptors.request.use((config) => {\n return {\n ...config\n }\n });\n\n // \u81EA\u5B9A\u4E49response\u62E6\u622A\u5668\uFF0C\n // \u6CE8\u610F\uFF1A\u5982\u679C\u4FEE\u6539\u63A5\u53E3\u6B63\u5E38\u8FD4\u56DE\u7684\u7ED3\u6784\uFF0C\u5BF9\u5E94\u7684response\u58F0\u660E\u9700\u8981\u4FEE\u6539\n instance.interceptors.response.use((res) => {\n const { status } = res;\n if (status >= 200 && status < 300) {\n return res;\n }\n return Promise.reject(res);\n });\n\n export default {\n get: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.get<RP>(url, config);\n },\n post: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.post<RP>(url, config);\n },\n head: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.head<RP>(url, config);\n },\n put: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.put<RP>(url, config);\n },\n patch: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.patch<RP>(url, config);\n },\n delete: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.delete<RP>(url, config);\n },\n };\n");
|
|
201
|
+
fs_extra_1.default.outputFile(rawRequestFunctionFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["", ""], ["", ""])), content)));
|
|
208
202
|
return [2
|
|
209
203
|
/*return*/
|
|
210
204
|
];
|
package/lib/helpers.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { Config, RequestConfig, RequestFunctionParams } from './types';
|
|
|
5
5
|
*
|
|
6
6
|
* @param config 配置
|
|
7
7
|
*/
|
|
8
|
-
export declare function defineConfig(config:
|
|
8
|
+
export declare function defineConfig(config: Config | Config[]): Config[];
|
|
9
9
|
export declare class FileData<T = any> {
|
|
10
10
|
/**
|
|
11
11
|
* 原始文件数据。
|
package/lib/helpers.js
CHANGED
|
@@ -186,14 +186,7 @@ function defineConfig(config) {
|
|
|
186
186
|
var final = configs.map(function (item) {
|
|
187
187
|
return __assign({
|
|
188
188
|
serverUrl: '',
|
|
189
|
-
|
|
190
|
-
target: 'typescript',
|
|
191
|
-
prodEnvName: 'local',
|
|
192
|
-
outputFilePath: 'src/api',
|
|
193
|
-
dataKey: 'data',
|
|
194
|
-
jsonSchema: {
|
|
195
|
-
enabled: false
|
|
196
|
-
}
|
|
189
|
+
outputFilePath: 'src/api'
|
|
197
190
|
}, item);
|
|
198
191
|
});
|
|
199
192
|
return final;
|
|
@@ -187,7 +187,7 @@ var runner = function runner(path, data, jsonSchema) {
|
|
|
187
187
|
exports.runner = runner;
|
|
188
188
|
|
|
189
189
|
var jsonSchemeFileHeader = function jsonSchemeFileHeader() {
|
|
190
|
-
return "\n import * as changeCase from 'change-case';\n import { JSONSchema4 } from 'api-
|
|
190
|
+
return "\n import * as changeCase from 'change-case';\n import { JSONSchema4 } from 'open-api-typescript-request-generator';\n\n /**\n * \u83B7\u53D6scheme\u7684key\n */\n export const jsonSchemeKey = function (path: string): string {\n const deeps = path.split('/');\n const names = deeps.splice(deeps.length - 4, deeps.length).join('_');\n return changeCase.camelCase(names);\n };\n\n export const typeStr = function (data: unknown): string {\n const type = typeof data;\n if (type === 'object' && data instanceof Array) {\n return 'array';\n }\n if (type === 'object' && data === null) {\n return 'null';\n }\n if (type === 'number') {\n return 'integer';\n }\n\n return type;\n };\n\n /**\n * \u68C0\u67E5\u5668\n */\n export const responseDataInspector = function (data: any, jsonScheme: JSONSchema4, key: any[] = ['data']): boolean {\n const { type, properties, items } = jsonScheme;\n\n if (type !== typeStr(data)) {\n console.warn(`->ResponseDataInspector\uFF1A${key.join('.')}\u4E0E\u7EA6\u5B9A\u7684\u7C7B\u578B\u4E0D\u7B26\uFF0C\u7C7B\u578B\uFF1A${type}\uFF0C\u8FD4\u56DE\u503C\uFF1A${data}`);\n return false;\n }\n if (type === 'object') {\n if (!properties || !Object.keys(properties).length) {\n return true;\n }\n return Object.keys(properties).every(k => responseDataInspector(data[k], properties[k], [...key, k]));\n }\n if (type === 'array') {\n if (items === undefined) {\n // undefined\u5219\u4E3A\u63A5\u53D7\u4EFB\u610F\u7C7B\u578B\n return true;\n }\n if (items instanceof Array) {\n return (data as Array<any>).some(v => items.every(item => responseDataInspector(v, item)));\n }\n if (items instanceof Object) {\n return (data as Array<any>).every((v, index) => responseDataInspector(v, items, [...key, index]));\n }\n }\n\n return true;\n };\n\n export const runner = function (path: string, data: any, jsonSchema: Record<string, any>): void {\n const key = jsonSchemeKey(path);\n responseDataInspector(data, jsonSchema[key]);\n };\n ";
|
|
191
191
|
};
|
|
192
192
|
|
|
193
193
|
exports.jsonSchemeFileHeader = jsonSchemeFileHeader;
|