openapi-ts-request 0.1.3 → 0.2.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
@@ -9,7 +9,7 @@
9
9
  ## Features
10
10
 
11
11
  * supports Swagger2.0/OpenAPI 3.0,3.1 specifications
12
- * generate TypeScript Interfaces, Reuquest clients, Request Mock Service, Enum, Display Field Babel
12
+ * generate TypeScript Interfaces, Reuquest clients, Request Mock Service, Enum, Display Type Field Label
13
13
  * supports Custom Request Function, Fetch、Axios、Uniapp-Request、Node.js、XHR client available
14
14
  * supports filter specifications by tags
15
15
  * supports JSON specifications
@@ -108,6 +108,7 @@ npm run openapi
108
108
  | requestOptionsType | 否 | 自定义请求方法 options 参数类型 | string | '{ [key: string]: unknown }' |
109
109
  | requestImportStatement | 否 | 自定义请求方法表达式,例如:'@/request' | string | - |
110
110
  | apiPrefix | 否 | api 的前缀,例如:'api'(动态变量), 指定字符串("'api'") | string | - |
111
+ | isDisplayTypeLabel | 否 | 是否生成 type 对应的label | boolean | false |
111
112
  | dataFields | 否 | 定义 response 中数据字段类型 | string[] | - |
112
113
  | mockFolder | 否 | mock目录 | string | './mocks' |
113
114
  | nullable | 否 | 使用null代替可选 | boolean | false |
@@ -3,11 +3,13 @@ import { ParameterObject, SchemaObject } from '../type';
3
3
  export declare const serviceEntryFileName = "index";
4
4
  export declare const interfaceFileName = "types";
5
5
  export declare const displayEnumLabelFileName = "displayEnumLabel";
6
+ export declare const displayTypeLabelFileName = "displayTypeLabel";
6
7
  export declare enum TypescriptFileType {
7
8
  interface = "interface",
8
9
  serviceController = "serviceController",
9
10
  serviceIndex = "serviceIndex",
10
- displayEnumLabel = "displayEnumLabel"
11
+ displayEnumLabel = "displayEnumLabel",
12
+ displayTypeLabel = "displayTypeLabel"
11
13
  }
12
14
  export declare const DEFAULT_SCHEMA: SchemaObject;
13
15
  export declare const DEFAULT_PATH_PARAM: ParameterObject & Dictionary<unknown>;
@@ -27,3 +29,4 @@ export declare enum parametersInsEnum {
27
29
  }
28
30
  export declare const parametersIn: string[];
29
31
  export declare const numberEnum: string[];
32
+ export declare const lineBreakReg: RegExp;
@@ -1,15 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.numberEnum = exports.parametersIn = exports.parametersInsEnum = exports.methods = exports.DEFAULT_PATH_PARAM = exports.DEFAULT_SCHEMA = exports.TypescriptFileType = exports.displayEnumLabelFileName = exports.interfaceFileName = exports.serviceEntryFileName = void 0;
3
+ exports.lineBreakReg = exports.numberEnum = exports.parametersIn = exports.parametersInsEnum = exports.methods = exports.DEFAULT_PATH_PARAM = exports.DEFAULT_SCHEMA = exports.TypescriptFileType = exports.displayTypeLabelFileName = exports.displayEnumLabelFileName = exports.interfaceFileName = exports.serviceEntryFileName = void 0;
4
4
  exports.serviceEntryFileName = 'index';
5
5
  exports.interfaceFileName = 'types';
6
6
  exports.displayEnumLabelFileName = 'displayEnumLabel';
7
+ exports.displayTypeLabelFileName = 'displayTypeLabel';
7
8
  var TypescriptFileType;
8
9
  (function (TypescriptFileType) {
9
10
  TypescriptFileType["interface"] = "interface";
10
11
  TypescriptFileType["serviceController"] = "serviceController";
11
12
  TypescriptFileType["serviceIndex"] = "serviceIndex";
12
13
  TypescriptFileType["displayEnumLabel"] = "displayEnumLabel";
14
+ TypescriptFileType["displayTypeLabel"] = "displayTypeLabel";
13
15
  })(TypescriptFileType || (exports.TypescriptFileType = TypescriptFileType = {}));
14
16
  exports.DEFAULT_SCHEMA = {
15
17
  type: 'object',
@@ -55,3 +57,5 @@ exports.numberEnum = [
55
57
  'int32',
56
58
  'int64',
57
59
  ];
60
+ // 匹配换行符的正则
61
+ exports.lineBreakReg = /[\r\n]+/g;
@@ -82,6 +82,14 @@ class ServiceGenerator {
82
82
  namespace: this.config.namespace,
83
83
  interfaceFileName: config_1.interfaceFileName,
84
84
  });
85
+ // 生成 type 翻译
86
+ if (this.config.isDisplayTypeLabel) {
87
+ this.genFileFromTemplate(`${config_1.displayTypeLabelFileName}.ts`, config_1.TypescriptFileType.displayTypeLabel, {
88
+ list: (0, lodash_1.filter)(interfaceTPConfigs, (item) => !item.isEnum),
89
+ namespace: this.config.namespace,
90
+ interfaceFileName: config_1.interfaceFileName,
91
+ });
92
+ }
85
93
  const prettierError = [];
86
94
  // 生成 service controller 文件
87
95
  this.getServiceTPConfigs().forEach((tp) => {
@@ -129,7 +137,7 @@ class ServiceGenerator {
129
137
  var _a;
130
138
  props.push({
131
139
  name: parameter.name,
132
- desc: (_a = parameter.description) !== null && _a !== void 0 ? _a : '',
140
+ desc: ((_a = parameter.description) !== null && _a !== void 0 ? _a : '').replace(config_1.lineBreakReg, ''),
133
141
  required: parameter.required || false,
134
142
  type: this.getType(parameter.schema),
135
143
  });
@@ -139,7 +147,7 @@ class ServiceGenerator {
139
147
  var _a;
140
148
  props.push({
141
149
  name: parameter.name,
142
- desc: (_a = parameter.description) !== null && _a !== void 0 ? _a : '',
150
+ desc: ((_a = parameter.description) !== null && _a !== void 0 ? _a : '').replace(config_1.lineBreakReg, ''),
143
151
  required: parameter.required,
144
152
  type: this.getType(parameter.schema),
145
153
  });
@@ -279,7 +287,7 @@ class ServiceGenerator {
279
287
  : functionName, typeName: this.getTypeName(newApi), path: getPrefixPath(), pathInComment: formattedPath.replace(/\*/g, '&#42;'), hasPathVariables: formattedPath.includes('{'), hasApiPrefix: !!this.config.apiPrefix, method: newApi.method,
280
288
  // 如果 functionName 和 summary 相同,则不显示 summary
281
289
  desc: functionName === newApi.summary
282
- ? newApi.description
290
+ ? (newApi.description || '').replace(config_1.lineBreakReg, '')
283
291
  : [
284
292
  newApi.summary,
285
293
  newApi.description,
@@ -288,7 +296,8 @@ class ServiceGenerator {
288
296
  : '',
289
297
  ]
290
298
  .filter((s) => s)
291
- .join(' '), hasHeader: !!(params === null || params === void 0 ? void 0 : params.header) || !!(body === null || body === void 0 ? void 0 : body.mediaType), params: finalParams, hasParams: Boolean((0, lodash_1.keys)(finalParams).length), options: ((_f = (_e = this.config.hook) === null || _e === void 0 ? void 0 : _e.customOptionsDefaultValue) === null || _f === void 0 ? void 0 : _f.call(_e, newApi)) || {}, body,
299
+ .join(' ')
300
+ .replace(config_1.lineBreakReg, ''), hasHeader: !!(params === null || params === void 0 ? void 0 : params.header) || !!(body === null || body === void 0 ? void 0 : body.mediaType), params: finalParams, hasParams: Boolean((0, lodash_1.keys)(finalParams).length), options: ((_f = (_e = this.config.hook) === null || _e === void 0 ? void 0 : _e.customOptionsDefaultValue) === null || _f === void 0 ? void 0 : _f.call(_e, newApi)) || {}, body,
292
301
  file, hasFormData: formData, response });
293
302
  }
294
303
  catch (error) {
@@ -325,7 +334,7 @@ class ServiceGenerator {
325
334
  nunjucks_1.default.configure({
326
335
  autoescape: false,
327
336
  });
328
- return (0, file_1.writeFile)(this.config.serversPath, fileName, nunjucks_1.default.renderString(template, Object.assign({ ddisableTypeCheck: false }, params)));
337
+ return (0, file_1.writeFile)(this.config.serversPath, fileName, nunjucks_1.default.renderString(template, Object.assign({ disableTypeCheck: false }, params)));
329
338
  }
330
339
  catch (error) {
331
340
  console.error('[GenSDK] file gen fail:', fileName, 'type:', type);
@@ -612,7 +621,8 @@ class ServiceGenerator {
612
621
  // 复用 schema 部分字段
613
622
  return Object.assign(Object.assign({}, schema), { name: propKey, type: this.getType(schema), desc: [schema.title, schema.description]
614
623
  .filter((item) => item)
615
- .join(' '),
624
+ .join(' ')
625
+ .replace(config_1.lineBreakReg, ''),
616
626
  // 如果没有 required 信息,默认全部是非必填
617
627
  required: requiredPropKeys
618
628
  ? requiredPropKeys.some((key) => key === propKey)
package/dist/index.d.ts CHANGED
@@ -38,6 +38,10 @@ export type GenerateServiceProps = {
38
38
  namespace: string;
39
39
  functionName: string;
40
40
  }) => string);
41
+ /**
42
+ * 是否生成 type 对应的label, 默认: false
43
+ */
44
+ isDisplayTypeLabel?: boolean;
41
45
  /**
42
46
  * response中数据字段
43
47
  * example: ['result', 'res']
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ function generateService(_a) {
17
17
  return;
18
18
  }
19
19
  const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
20
- const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', nullable: false, isCamelCase: true, allowedTags: allowedTags
20
+ 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, allowedTags: allowedTags
21
21
  ? (0, lodash_1.map)(allowedTags, (item) => item.toLowerCase())
22
22
  : null }, rest), openAPI);
23
23
  serviceGenerator.genFile();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Swagger2/OpenAPI3 to TypeScript, Request Client, Request Mock Service, Enum, Display Field Label",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,42 @@
1
+ /* eslint-disable */
2
+ // @ts-ignore
3
+ import * as {{ namespace }} from './{{ interfaceFileName }}';
4
+
5
+ {% for type in list -%}
6
+ {%- if type.props.length %}
7
+ export function display{{ type.typeName }}(field: keyof {{ namespace }}.{{ type.typeName }}) {
8
+ return (
9
+ {%- for prop in type.props %}
10
+ {%- if prop.length > 1 %}
11
+ {
12
+ {%- endif %}
13
+ {%- if prop.length == 1 %}
14
+ {%- if not prop[0].$ref or prop[0].name %}
15
+ {
16
+ {%- endif %}
17
+ {%- endif %}
18
+ {%- for p in prop %}
19
+ {%- if p["$ref"] and not p.name %}
20
+ display{{ p.type | safe }}(field as keyof {{ namespace }}.{{ p.type }})
21
+ {%- else %}
22
+ {{ p.name }}: '{{ p.desc if p.desc else p.name }}',
23
+ {%- endif %}
24
+ {%- endfor %}
25
+ {%- if prop.length > 1 %}
26
+ }[field]
27
+ {%- endif %}
28
+ {%- if prop.length == 1 %}
29
+ {%- if not prop[0].$ref or prop[0].name %}
30
+ }[field]
31
+ {%- endif %}
32
+ {%- endif %}
33
+ {%- if prop.length == 0 %}
34
+ {}
35
+ {%- endif %}
36
+ {{ '' if loop.last === true else ' || ' }}
37
+ {%- endfor %}
38
+ )
39
+ }
40
+ {%- endif %}
41
+
42
+ {% endfor -%}