openapi-ts-request 0.7.0 → 0.8.1

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.
@@ -87,10 +87,11 @@ class ServiceGenerator {
87
87
  interfaceFileName: config_1.interfaceFileName,
88
88
  });
89
89
  }
90
+ const displayTypeLabels = (0, lodash_1.filter)(interfaceTPConfigs, (item) => !item.isEnum);
90
91
  // 生成 type 翻译
91
- if (this.config.isDisplayTypeLabel) {
92
+ if (this.config.isDisplayTypeLabel && !(0, lodash_1.isEmpty)(displayTypeLabels)) {
92
93
  this.genFileFromTemplate(`${config_1.displayTypeLabelFileName}.ts`, config_1.TypescriptFileType.displayTypeLabel, {
93
- list: (0, lodash_1.filter)(interfaceTPConfigs, (item) => !item.isEnum),
94
+ list: displayTypeLabels,
94
95
  namespace: this.config.namespace,
95
96
  interfaceFileName: config_1.interfaceFileName,
96
97
  });
@@ -104,15 +105,7 @@ class ServiceGenerator {
104
105
  if (prettierError.includes(true)) {
105
106
  (0, log_1.default)('🚥 格式化失败,请检查 service controller 文件内可能存在的语法错误');
106
107
  }
107
- // 生成 service index 文件
108
- this.genFileFromTemplate(`${config_1.serviceEntryFileName}.ts`, config_1.TypescriptFileType.serviceIndex, {
109
- list: this.classNameList,
110
- namespace: this.config.namespace,
111
- interfaceFileName: config_1.interfaceFileName,
112
- schemaFileName: config_1.schemaFileName,
113
- isGenJsonSchemas: this.config.isGenJsonSchemas,
114
- });
115
- if (this.config.isGenJsonSchemas) {
108
+ if (this.config.isGenJsonSchemas && !(0, lodash_1.isEmpty)(this.schemaList)) {
116
109
  // 处理重复的 schemaName
117
110
  (0, util_1.handleDuplicateTypeNames)(this.schemaList);
118
111
  // 生成 schema 文件
@@ -120,6 +113,18 @@ class ServiceGenerator {
120
113
  list: this.schemaList,
121
114
  });
122
115
  }
116
+ // 生成 service index 文件
117
+ this.genFileFromTemplate(`${config_1.serviceEntryFileName}.ts`, config_1.TypescriptFileType.serviceIndex, {
118
+ list: this.classNameList,
119
+ namespace: this.config.namespace,
120
+ interfaceFileName: config_1.interfaceFileName,
121
+ isGenJsonSchemas: this.config.isGenJsonSchemas && !(0, lodash_1.isEmpty)(this.schemaList),
122
+ schemaFileName: config_1.schemaFileName,
123
+ isDisplayEnumLabel: !(0, lodash_1.isEmpty)(enums),
124
+ displayEnumLabelFileName: config_1.displayEnumLabelFileName,
125
+ isDisplayTypeLabel: this.config.isDisplayTypeLabel && !(0, lodash_1.isEmpty)(displayTypeLabels),
126
+ displayTypeLabelFileName: config_1.displayTypeLabelFileName,
127
+ });
123
128
  // 打印日志
124
129
  (0, log_1.default)('✅ 成功生成 api 文件');
125
130
  }
@@ -501,8 +506,7 @@ class ServiceGenerator {
501
506
  let schema = (resContent[mediaType].schema ||
502
507
  config_1.DEFAULT_SCHEMA);
503
508
  if ((0, util_1.isReferenceObject)(schema)) {
504
- const refPaths = schema.$ref.split('/');
505
- const refName = refPaths[refPaths.length - 1];
509
+ const refName = (0, util_1.getLastRefName)(schema.$ref);
506
510
  const childrenSchema = components.schemas[refName];
507
511
  if ((0, util_1.isNonArraySchemaObject)(childrenSchema) && this.config.dataFields) {
508
512
  schema = (((_a = this.config.dataFields
@@ -535,11 +539,9 @@ class ServiceGenerator {
535
539
  var _a, _b, _c, _d, _e;
536
540
  const isDirectObject = (((_a = p.schema) === null || _a === void 0 ? void 0 : _a.type) === 'object' ||
537
541
  p.type) === 'object';
538
- const refList = (((_b = p.schema) === null || _b === void 0 ? void 0 : _b.$ref) ||
539
- p.$ref ||
540
- '').split('/');
541
- const ref = refList[refList.length - 1];
542
- const deRefObj = (0, lodash_1.entries)((_c = this.openAPIData.components) === null || _c === void 0 ? void 0 : _c.schemas).find(([k]) => k === ref) || [];
542
+ const refName = (0, util_1.getLastRefName)(((_b = p.schema) === null || _b === void 0 ? void 0 : _b.$ref) ||
543
+ p.$ref);
544
+ const deRefObj = (0, lodash_1.entries)((_c = this.openAPIData.components) === null || _c === void 0 ? void 0 : _c.schemas).find(([k]) => k === refName) || [];
543
545
  const isRefObject = ((_d = deRefObj[1]) === null || _d === void 0 ? void 0 : _d.type) === 'object' &&
544
546
  !(0, lodash_1.isEmpty)((_e = deRefObj[1]) === null || _e === void 0 ? void 0 : _e.properties);
545
547
  return Object.assign(Object.assign({}, p), { isObject: isDirectObject || isRefObject, type: this.getType(p.schema || config_1.DEFAULT_SCHEMA, this.config.namespace) });
@@ -591,9 +593,9 @@ class ServiceGenerator {
591
593
  }
592
594
  resolveArray(schemaObject) {
593
595
  if ((0, util_1.isReferenceObject)(schemaObject.items)) {
594
- const refPaths = schemaObject.items.$ref.split('/');
596
+ const refName = (0, util_1.getLastRefName)(schemaObject.items.$ref);
595
597
  return {
596
- type: `${refPaths[refPaths.length - 1]}[]`,
598
+ type: `${refName}[]`,
597
599
  };
598
600
  }
599
601
  // 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回 unknown[]
@@ -676,7 +678,7 @@ class ServiceGenerator {
676
678
  // 测试了很多用例,很少有用例走到这里
677
679
  const refPaths = refObject.$ref.split('/');
678
680
  if (refPaths[0] === '#') {
679
- const schema = (_b = (_a = this.openAPIData.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b[refPaths[refPaths.length - 1]];
681
+ const schema = (_b = (_a = this.openAPIData.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b[(0, util_1.getLastRefName)(refObject.$ref)];
680
682
  if (!schema) {
681
683
  throw new Error(`[GenSDK] Data Error! Notfoud: ${refObject.$ref}`);
682
684
  }
@@ -2,6 +2,7 @@ import { ArraySchemaObject, BinaryArraySchemaObject, ComponentsObject, ISchemaOb
2
2
  import { ITypeItem } from './type';
3
3
  export declare function stripDot(str: string): string;
4
4
  export declare function resolveTypeName(typeName: string): string;
5
+ export declare function getLastRefName(refPath?: string): string;
5
6
  export declare function getDefaultType(schemaObject?: ISchemaObject | string, namespace?: string, schemas?: ComponentsObject['schemas']): string;
6
7
  export declare function getDefaultFileTag(operationObject: OperationObject, apiPath: string): string[];
7
8
  export declare function handleDuplicateTypeNames(interfaceTPConfigs: Array<Pick<ITypeItem, 'typeName' | 'displayLabelFuncName'>>): void;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.stripDot = stripDot;
4
4
  exports.resolveTypeName = resolveTypeName;
5
+ exports.getLastRefName = getLastRefName;
5
6
  exports.getDefaultType = getDefaultType;
6
7
  exports.getDefaultFileTag = getDefaultFileTag;
7
8
  exports.handleDuplicateTypeNames = handleDuplicateTypeNames;
@@ -73,8 +74,13 @@ function getRefName(refObject) {
73
74
  if (!isReferenceObject(refObject)) {
74
75
  return refObject;
75
76
  }
76
- const refPaths = refObject.$ref.split('/');
77
- return resolveTypeName(refPaths[refPaths.length - 1]);
77
+ return resolveTypeName(getLastRefName(refObject.$ref));
78
+ }
79
+ function getLastRefName(refPath = '') {
80
+ const refPaths = refPath.split('/');
81
+ return refPaths.length > 0
82
+ ? decodeURIComponent(refPaths[refPaths.length - 1])
83
+ : '';
78
84
  }
79
85
  function getDefaultType(schemaObject, namespace = '', schemas) {
80
86
  var _a, _b;
@@ -147,8 +153,7 @@ function getDefaultType(schemaObject, namespace = '', schemas) {
147
153
  var _a;
148
154
  if (isReferenceObject(item)) {
149
155
  // 不使用 getRefName 函数处理,无法通过 schemas[schemaKey] 获取到schema
150
- const refPaths = item.$ref.split('/');
151
- const schemaKey = refPaths[refPaths.length - 1];
156
+ const schemaKey = getLastRefName(item.$ref);
152
157
  if ((_a = schemas === null || schemas === void 0 ? void 0 : schemas[schemaKey]) === null || _a === void 0 ? void 0 : _a.enum) {
153
158
  return `I${getDefaultType(item, namespace)}`;
154
159
  }
@@ -282,8 +287,7 @@ function resolveFunctionName(functionName, methodName) {
282
287
  function markAllowSchema(schemaStr, schemas) {
283
288
  const refs = schemaStr === null || schemaStr === void 0 ? void 0 : schemaStr.match(/#\/components\/schemas\/([A-Za-z0-9._-]+)/g);
284
289
  (0, lodash_1.forEach)(refs, (ref) => {
285
- const refPaths = ref.split('/');
286
- const schema = schemas === null || schemas === void 0 ? void 0 : schemas[refPaths[refPaths.length - 1]];
290
+ const schema = schemas === null || schemas === void 0 ? void 0 : schemas[getLastRefName(ref)];
287
291
  if (schema && !schema.isAllowed) {
288
292
  schema.isAllowed = true;
289
293
  markAllowSchema(JSON.stringify(schema), schemas);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "description": "Swagger2/OpenAPI3 to TypeScript, request client, request mock service, enum, type field label, JSON Schemas",
5
5
  "repository": {
6
6
  "type": "git",
@@ -60,7 +60,7 @@
60
60
  "husky": "^9.0.11",
61
61
  "lint-staged": "^15.2.5",
62
62
  "openapi-types": "^12.1.3",
63
- "typescript": "5.6.2"
63
+ "typescript": "5.6.3"
64
64
  },
65
65
  "keywords": [
66
66
  "openapi",
@@ -52,5 +52,5 @@
52
52
  {%- endif %}
53
53
  {% endfor %}
54
54
  {%- else %}
55
- export default {}
55
+ export {}
56
56
  {%- endif %}
@@ -3,6 +3,4 @@
3
3
  export const {{ type.typeName }} = {{ type.type }}
4
4
 
5
5
  {% endfor -%}
6
- {%- else %}
7
- export default {}
8
6
  {%- endif %}
@@ -1,6 +1,12 @@
1
1
  /* eslint-disable */
2
2
  // @ts-ignore
3
3
  import * as {{ namespace }} from './{{ interfaceFileName }}';
4
+ {%- if isDisplayEnumLabel %}
5
+ import * as displayEnumLabels from './{{ displayEnumLabelFileName }}';
6
+ {%- endif %}
7
+ {%- if isDisplayTypeLabel %}
8
+ import * as displayTypeLabels from './{{ displayTypeLabelFileName }}';
9
+ {%- endif %}
4
10
  {%- if isGenJsonSchemas %}
5
11
  import * as schemas from './{{ schemaFileName }}';
6
12
  {%- endif %}
@@ -8,11 +14,17 @@ import * as schemas from './{{ schemaFileName }}';
8
14
  import * as {{ api.controllerName }} from './{{ api.fileName }}';
9
15
  {% endfor -%}
10
16
 
11
- export default {
17
+ export {
18
+ {{ namespace }},
19
+ {%- if isDisplayEnumLabel %}
20
+ displayEnumLabels,
21
+ {%- endif %}
22
+ {%- if isDisplayTypeLabel %}
23
+ displayTypeLabels,
24
+ {%- endif %}
12
25
  {%- if isGenJsonSchemas %}
13
26
  schemas,
14
27
  {%- endif %}
15
- {{ namespace }},
16
28
  {% for api in list -%}
17
29
  {{ api.controllerName }},
18
30
  {% endfor -%}