open-api-typescript-request-generator 0.0.1 → 0.0.2

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 CHANGED
@@ -1,14 +1,10 @@
1
- # api-ts-generator
1
+ # open-api-typescript-request-generator
2
2
 
3
- 基于 Yapi Swagger,生成接口的 request、response 类型声明和请求方法体
4
-
5
- ## 版本更新
6
-
7
- 【2.0.0】破坏性改动,此改动将会影响接口函数名的生成,导致调用方需要同步修改生成后的调用名[CHANGELOG](https://github.com/huzhongchun/api-ts-generator/commit/748d6618cad84f3b01936921943433063b133203)
3
+ 基于 openapi 规范,生成接口的 request、response 类型声明和请求方法体
8
4
 
9
5
  ## 安装
10
6
 
11
- `npm install api-ts-generator -g`
7
+ `npm install open-api-typescript-request-generator`
12
8
 
13
9
  ## 初始化生成配置文件
14
10
 
@@ -19,8 +15,13 @@
19
15
 
20
16
  `apits-gener gen`
21
17
 
18
+ ## 只生成指定 name 的配置
22
19
 
23
- ## 配置
24
-
25
- 参考开源代码:https://fjc0k.github.io/yapi-to-typescript/handbook/
20
+ ```bash
21
+ apits-gener gen --name=test
22
+ # or
23
+ apits-gener gen -n test
24
+ ```
26
25
 
26
+ # tips
27
+ 1. 如果有代码格式要求,应该在配置中忽略/api中生成的代码
package/es/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 defaultTopImportPkgTemplate(config) {
267
- return "import request from '../request'";
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(requestFunctionExtraParams ? ",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(requestFunctionExtraParams ? "...extra" : '', "\n })\n }");
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, project, serverUrl, preproccessInterface, outputFilePath, filter, configIndex, name, typesName, openApiV3Json, componentsCode, allApi, interfaceList, categoryCode, categoryResponseDataJsonSchemaContent, _i, interfaceList_1, interfaceInfo, _b, code, responseDataJsonSchema, catOutputFilePath;
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, project = _a.project, serverUrl = _a.serverUrl, preproccessInterface = _a.preproccessInterface, outputFilePath = _a.outputFilePath, filter = _a.filter, configIndex = _a.configIndex, name = _a.name;
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(__assign({}, this.config), project), {
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: this.config.requestFunctionFilePath ? path_1.default.resolve(this.options.cwd, this.config.requestFunctionFilePath) : path_1.default.join(path_1.default.dirname(catOutputFilePath), 'request.ts'),
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, topImportPkgTemplate, rawOutputContent;
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
- topImportPkgTemplate = syntheticalConfig.topImportPkgTemplate || defaultTopImportPkgTemplate;
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)(), topImportPkgTemplate(config), content.join('\n\n').trim());
550
- outputContent += (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["", ""], ["", ""])), rawOutputContent), config.prettierConfigPath);
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, _a, requestConfigName, requestConfigTypeName, requestDataTypeName, _b, responseDataTypeName, _c, requestDataJsonSchema, requestDataType, responseDataJsonSchema, responseDataType, isRequestDataOptional, requestHookName, _d, _e, paramNames, paramNamesLiteral, paramNameType, queryNames, queryNamesLiteral, queryNameType, genComment, requestFunctionTemplate, baseURL, baseUrl, code;
633
-
634
- return __generator(this, function (_f) {
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
- if (!(0, vtils_1.isFunction)(syntheticalConfig.getRequestFunctionName)) return [3
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
- if (!(0, vtils_1.isFunction)(syntheticalConfig.getRequestDataTypeName)) return [3
662
- /*break*/
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 10:
707
- requestDataType = _f.sent();
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, syntheticalConfig.dataKey);
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 11:
721
- responseDataType = _f.sent();
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 = __assign(__assign({}, syntheticalConfig.comment), {
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 = syntheticalConfig.requestFunctionTemplate || (syntheticalConfig.proxyInterface ? adminRequestFunctionTemplate : defaultRequestFunctionTemplate);
669
+ requestFunctionTemplate = defaultRequestFunctionTemplate;
836
670
  baseURL = syntheticalConfig.baseURL;
837
671
 
838
672
  try {
@@ -845,7 +679,7 @@ function () {
845
679
  return "".concat(title, " \u8BF7\u6C42\u53C2\u6570");
846
680
  }), requestDataType.trim(), genComment(function (title) {
847
681
  return "".concat(title, " \u54CD\u5E94\u6570\u636E");
848
- }), responseDataType.trim(), syntheticalConfig.typesOnly ? '' : (0, vtils_1.dedent)(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n ", "\n ", "\n\n "], ["\n ", "\n ", "\n\n "])), genComment(function (title) {
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/es/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(): Promise<void>;
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): {
package/es/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, swaggerAnswers;
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
- swaggerAnswers = _b.sent();
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-ts-generator'\n\n export default defineConfig([{\n serverUrl: '", "',\n outputFilePath: 'src/api'\n }])\n "], ["\n import { defineConfig } from 'api-ts-generator'\n\n export default defineConfig([{\n serverUrl: '", "',\n outputFilePath: 'src/api'\n }])\n "])), (swaggerAnswers === null || swaggerAnswers === void 0 ? void 0 : swaggerAnswers.url) || '')))];
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)(config.map(function (configItem, index) {
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 2:
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
- return yargs_1.default.scriptName('apits-gener').usage('Usage: $0 <command> [options]').command('gen', '生成接口类型声明和方法', function (y) {}, function (argv) {
535
- var _a = argv;
536
- start();
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
- var _a = argv;
539
- genConfig();
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/es/constants.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export declare const DefaultServerUrl = "";
2
- export declare const CookieStoreFile = "/Users/Shared/api-ts-generator/yapiCookies.json";
3
2
  export declare enum ResponseErrorCode {
4
3
  /** 未登录 */
5
4
  UnLogin = 40011
package/es/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.CookieStoreFile = exports.DefaultServerUrl = void 0;
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/es/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 prettierConfigPath, indexFilePath, originFileContent, exportAllInterface, content;
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), prettierConfigPath));
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 prettierConfigPath, indexFilePath, originFileContent, exportAllInterface, content;
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), prettierConfigPath));
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
  ];