openapi-ts-request 0.8.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.
@@ -506,8 +506,7 @@ class ServiceGenerator {
506
506
  let schema = (resContent[mediaType].schema ||
507
507
  config_1.DEFAULT_SCHEMA);
508
508
  if ((0, util_1.isReferenceObject)(schema)) {
509
- const refPaths = schema.$ref.split('/');
510
- const refName = refPaths[refPaths.length - 1];
509
+ const refName = (0, util_1.getLastRefName)(schema.$ref);
511
510
  const childrenSchema = components.schemas[refName];
512
511
  if ((0, util_1.isNonArraySchemaObject)(childrenSchema) && this.config.dataFields) {
513
512
  schema = (((_a = this.config.dataFields
@@ -540,11 +539,9 @@ class ServiceGenerator {
540
539
  var _a, _b, _c, _d, _e;
541
540
  const isDirectObject = (((_a = p.schema) === null || _a === void 0 ? void 0 : _a.type) === 'object' ||
542
541
  p.type) === 'object';
543
- const refList = (((_b = p.schema) === null || _b === void 0 ? void 0 : _b.$ref) ||
544
- p.$ref ||
545
- '').split('/');
546
- const ref = refList[refList.length - 1];
547
- 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) || [];
548
545
  const isRefObject = ((_d = deRefObj[1]) === null || _d === void 0 ? void 0 : _d.type) === 'object' &&
549
546
  !(0, lodash_1.isEmpty)((_e = deRefObj[1]) === null || _e === void 0 ? void 0 : _e.properties);
550
547
  return Object.assign(Object.assign({}, p), { isObject: isDirectObject || isRefObject, type: this.getType(p.schema || config_1.DEFAULT_SCHEMA, this.config.namespace) });
@@ -596,9 +593,9 @@ class ServiceGenerator {
596
593
  }
597
594
  resolveArray(schemaObject) {
598
595
  if ((0, util_1.isReferenceObject)(schemaObject.items)) {
599
- const refPaths = schemaObject.items.$ref.split('/');
596
+ const refName = (0, util_1.getLastRefName)(schemaObject.items.$ref);
600
597
  return {
601
- type: `${refPaths[refPaths.length - 1]}[]`,
598
+ type: `${refName}[]`,
602
599
  };
603
600
  }
604
601
  // 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回 unknown[]
@@ -681,7 +678,7 @@ class ServiceGenerator {
681
678
  // 测试了很多用例,很少有用例走到这里
682
679
  const refPaths = refObject.$ref.split('/');
683
680
  if (refPaths[0] === '#') {
684
- 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)];
685
682
  if (!schema) {
686
683
  throw new Error(`[GenSDK] Data Error! Notfoud: ${refObject.$ref}`);
687
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.8.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",