openapi-ts-request 0.11.0 → 0.12.0

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
@@ -150,6 +150,7 @@ $ openapi --help
150
150
  --authorization <string> docs authorization
151
151
  --nullable <boolean> null instead of optional (default: false)
152
152
  --isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
153
+ --isOnlyGenTypeScriptType <boolean>only generate typescript type (default: false)
153
154
  --isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
154
155
  -h, --help display help for command
155
156
  ```
@@ -177,6 +178,7 @@ openapi --i ./spec.json --o ./apis
177
178
  | authorization | 否 | string | - | 文档权限凭证 |
178
179
  | nullable | 否 | boolean | false | 使用 null 代替可选 |
179
180
  | isTranslateToEnglishTag | 否 | boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
181
+ | isOnlyGenTypeScriptType | 否 | boolean | false | 仅生成 typescript 类型 |
180
182
  | isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
181
183
  | hook | 否 | [Custom Hook](#Custom-Hook) | - | 自定义 hook |
182
184
 
@@ -23,6 +23,7 @@ const params = commander_1.program
23
23
  .option('--authorization <string>', 'docs authorization')
24
24
  .option('--nullable <boolean>', 'null instead of optional', false)
25
25
  .option('--isTranslateToEnglishTag <boolean>', 'translate chinese tag name to english tag name', false)
26
+ .option('--isOnlyGenTypeScriptType <boolean>', 'only generate typescript type', false)
26
27
  .option('--isCamelCase <boolean>', 'camelCase naming of controller files and request client', true)
27
28
  .parse(process.argv)
28
29
  .opts();
@@ -51,6 +52,7 @@ function run() {
51
52
  authorization: params.authorization,
52
53
  nullable: JSON.parse(params.nullable) === true,
53
54
  isTranslateToEnglishTag: JSON.parse(params.isTranslateToEnglishTag) === true,
55
+ isOnlyGenTypeScriptType: JSON.parse(params.isOnlyGenTypeScriptType) === true,
54
56
  isCamelCase: JSON.parse(params.isCamelCase) === true,
55
57
  });
56
58
  process.exit(0);
@@ -16,7 +16,7 @@ export default class ServiceGenerator {
16
16
  private getTemplate;
17
17
  private getFunctionName;
18
18
  private getType;
19
- private getTypeName;
19
+ private getFunctionParamsTypeName;
20
20
  private getBodyTP;
21
21
  private getFileTP;
22
22
  private resolveFileTP;
@@ -80,7 +80,7 @@ class ServiceGenerator {
80
80
  });
81
81
  // 生成枚举翻译
82
82
  const enums = (0, lodash_1.filter)(interfaceTPConfigs, (item) => item.isEnum);
83
- if (!(0, lodash_1.isEmpty)(enums)) {
83
+ if (!this.config.isOnlyGenTypeScriptType && !(0, lodash_1.isEmpty)(enums)) {
84
84
  this.genFileFromTemplate(`${config_1.displayEnumLabelFileName}.ts`, config_1.TypescriptFileType.displayEnumLabel, {
85
85
  list: enums,
86
86
  namespace: this.config.namespace,
@@ -89,23 +89,29 @@ class ServiceGenerator {
89
89
  }
90
90
  const displayTypeLabels = (0, lodash_1.filter)(interfaceTPConfigs, (item) => !item.isEnum);
91
91
  // 生成 type 翻译
92
- if (this.config.isDisplayTypeLabel && !(0, lodash_1.isEmpty)(displayTypeLabels)) {
92
+ if (!this.config.isOnlyGenTypeScriptType &&
93
+ this.config.isDisplayTypeLabel &&
94
+ !(0, lodash_1.isEmpty)(displayTypeLabels)) {
93
95
  this.genFileFromTemplate(`${config_1.displayTypeLabelFileName}.ts`, config_1.TypescriptFileType.displayTypeLabel, {
94
96
  list: displayTypeLabels,
95
97
  namespace: this.config.namespace,
96
98
  interfaceFileName: config_1.interfaceFileName,
97
99
  });
98
100
  }
99
- const prettierError = [];
100
- // 生成 service controller 文件
101
- this.getServiceTPConfigs().forEach((tp) => {
102
- const hasError = this.genFileFromTemplate((0, util_1.getFinalFileName)(`${tp.className}.ts`), config_1.TypescriptFileType.serviceController, Object.assign({ namespace: this.config.namespace, requestOptionsType: this.config.requestOptionsType, requestImportStatement: this.config.requestImportStatement, interfaceFileName: config_1.interfaceFileName }, tp));
103
- prettierError.push(hasError);
104
- });
105
- if (prettierError.includes(true)) {
106
- (0, log_1.default)('🚥 格式化失败,请检查 service controller 文件内可能存在的语法错误');
101
+ if (!this.config.isOnlyGenTypeScriptType) {
102
+ const prettierError = [];
103
+ // 生成 service controller 文件
104
+ this.getServiceTPConfigs().forEach((tp) => {
105
+ const hasError = this.genFileFromTemplate((0, util_1.getFinalFileName)(`${tp.className}.ts`), config_1.TypescriptFileType.serviceController, Object.assign({ namespace: this.config.namespace, requestOptionsType: this.config.requestOptionsType, requestImportStatement: this.config.requestImportStatement, interfaceFileName: config_1.interfaceFileName }, tp));
106
+ prettierError.push(hasError);
107
+ });
108
+ if (prettierError.includes(true)) {
109
+ (0, log_1.default)('🚥 格式化失败,请检查 service controller 文件内可能存在的语法错误');
110
+ }
107
111
  }
108
- if (this.config.isGenJsonSchemas && !(0, lodash_1.isEmpty)(this.schemaList)) {
112
+ if (!this.config.isOnlyGenTypeScriptType &&
113
+ this.config.isGenJsonSchemas &&
114
+ !(0, lodash_1.isEmpty)(this.schemaList)) {
109
115
  // 处理重复的 schemaName
110
116
  (0, util_1.handleDuplicateTypeNames)(this.schemaList);
111
117
  // 生成 schema 文件
@@ -118,11 +124,15 @@ class ServiceGenerator {
118
124
  list: this.classNameList,
119
125
  namespace: this.config.namespace,
120
126
  interfaceFileName: config_1.interfaceFileName,
121
- isGenJsonSchemas: this.config.isGenJsonSchemas && !(0, lodash_1.isEmpty)(this.schemaList),
127
+ isGenJsonSchemas: !this.config.isOnlyGenTypeScriptType &&
128
+ this.config.isGenJsonSchemas &&
129
+ !(0, lodash_1.isEmpty)(this.schemaList),
122
130
  schemaFileName: config_1.schemaFileName,
123
- isDisplayEnumLabel: !(0, lodash_1.isEmpty)(enums),
131
+ isDisplayEnumLabel: !this.config.isOnlyGenTypeScriptType && !(0, lodash_1.isEmpty)(enums),
124
132
  displayEnumLabelFileName: config_1.displayEnumLabelFileName,
125
- isDisplayTypeLabel: this.config.isDisplayTypeLabel && !(0, lodash_1.isEmpty)(displayTypeLabels),
133
+ isDisplayTypeLabel: !this.config.isOnlyGenTypeScriptType &&
134
+ this.config.isDisplayTypeLabel &&
135
+ !(0, lodash_1.isEmpty)(displayTypeLabels),
126
136
  displayTypeLabelFileName: config_1.displayTypeLabelFileName,
127
137
  });
128
138
  // 打印日志
@@ -172,7 +182,7 @@ class ServiceGenerator {
172
182
  type: this.getType(parameter.schema),
173
183
  });
174
184
  });
175
- const typeName = this.getTypeName(Object.assign(Object.assign({}, operationObject), { method, path: pathKey }));
185
+ const typeName = this.getFunctionParamsTypeName(Object.assign(Object.assign({}, operationObject), { method, path: pathKey }));
176
186
  if (props.length > 0 && typeName) {
177
187
  lastTypes.push({
178
188
  typeName,
@@ -316,7 +326,7 @@ class ServiceGenerator {
316
326
  };
317
327
  return Object.assign(Object.assign({}, newApi), { functionName: this.config.isCamelCase
318
328
  ? (0, lodash_1.camelCase)(functionName)
319
- : functionName, typeName: this.getTypeName(newApi), path: getPrefixPath(), pathInComment: formattedPath.replace(/\*/g, '&#42;'), apifoxRunLink: newApi === null || newApi === void 0 ? void 0 : newApi['x-run-in-apifox'], hasPathVariables: formattedPath.includes('{'), hasApiPrefix: !!this.config.apiPrefix, method: newApi.method,
329
+ : functionName, typeName: this.getFunctionParamsTypeName(newApi), path: getPrefixPath(), pathInComment: formattedPath.replace(/\*/g, '&#42;'), apifoxRunLink: newApi === null || newApi === void 0 ? void 0 : newApi['x-run-in-apifox'], hasPathVariables: formattedPath.includes('{'), hasApiPrefix: !!this.config.apiPrefix, method: newApi.method,
320
330
  // 如果 functionName 和 summary 相同,则不显示 summary
321
331
  desc: functionName === newApi.summary
322
332
  ? (newApi.description || '').replace(config_1.lineBreakReg, '')
@@ -402,7 +412,7 @@ class ServiceGenerator {
402
412
  }
403
413
  return (0, util_1.getDefaultType)(schemaObject, namespace, schemas);
404
414
  }
405
- getTypeName(data) {
415
+ getFunctionParamsTypeName(data) {
406
416
  var _a, _b, _c;
407
417
  const namespace = this.config.namespace ? `${this.config.namespace}.` : '';
408
418
  const typeName = ((_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.hook) === null || _b === void 0 ? void 0 : _b.customTypeName) === null || _c === void 0 ? void 0 : _c.call(_b, data)) || this.getFunctionName(data);
@@ -592,12 +602,18 @@ class ServiceGenerator {
592
602
  return schemaObject;
593
603
  }
594
604
  resolveArray(schemaObject) {
605
+ var _a;
595
606
  if ((0, util_1.isReferenceObject)(schemaObject.items)) {
596
607
  const refName = (0, util_1.getRefName)(schemaObject.items);
597
608
  return {
598
609
  type: `${refName}[]`,
599
610
  };
600
611
  }
612
+ else if ((_a = schemaObject.items) === null || _a === void 0 ? void 0 : _a.enum) {
613
+ return {
614
+ type: this.getType(schemaObject, this.config.namespace),
615
+ };
616
+ }
601
617
  // 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回 unknown[]
602
618
  return { type: 'unknown[]' };
603
619
  }
package/dist/index.d.ts CHANGED
@@ -68,6 +68,10 @@ export type GenerateServiceProps = {
68
68
  * 是否将中文 tag 名称翻译成英文 tag 名称
69
69
  */
70
70
  isTranslateToEnglishTag?: boolean;
71
+ /**
72
+ * 仅仅生成类型,不生成请求函数
73
+ */
74
+ isOnlyGenTypeScriptType?: boolean;
71
75
  /**
72
76
  * 模板文件、请求函数采用小驼峰命名
73
77
  */
package/dist/index.js CHANGED
@@ -21,7 +21,7 @@ function generateService(_a) {
21
21
  yield (0, util_1.translateChineseModuleNodeToEnglish)(openAPI);
22
22
  }
23
23
  const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
24
- const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', nullable: false, isCamelCase: true, isDisplayTypeLabel: false, isGenJsonSchemas: false, allowedTags: allowedTags
24
+ const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', nullable: false, isCamelCase: true, isDisplayTypeLabel: false, isGenJsonSchemas: false, isOnlyGenTypeScriptType: false, allowedTags: allowedTags
25
25
  ? (0, lodash_1.map)(allowedTags, (item) => item.toLowerCase())
26
26
  : null }, rest), openAPI);
27
27
  serviceGenerator.genFile();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Swagger2/OpenAPI3 to TypeScript, request client, request mock service, enum, type field label, JSON Schemas",
5
5
  "engines": {
6
6
  "node": ">=18.0.0",