openapi-ts-request 1.5.2 → 1.6.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.
package/README.md CHANGED
@@ -340,7 +340,7 @@ export default {
340
340
  1. 熟悉 [Pull Request]("https://help.github.com/articles/using-pull-requests") 规范
341
341
  2. fork 此仓库
342
342
  3. 开一个新分支修改代码:`git checkout -b my-branch main`
343
- 4. 确保你的代码可以通过所有测试用例(新增功能需要添加新的功能测试用例):`pnpm test`
343
+ 4. 确保你的代码可以通过所有测试用例(新增功能需要添加新的功能测试用例):`pnpm test:unit`
344
344
  5. 创建 changeset 文件通过命令:`pnpm changeset`
345
345
  6. 使用 commit 提交你的修改(需遵循 commitlint 规范)
346
346
  7. 发起 Pull Request
package/dist/config.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IReactQueryMode } from './type';
1
+ import type { IReactQueryMode } from './type';
2
2
  export declare enum SchemaObjectFormat {
3
3
  int32 = "int32",
4
4
  int64 = "int64",
@@ -1,5 +1,5 @@
1
- import { Dictionary } from 'lodash';
2
- import { IReactQueryMode, ParameterObject, SchemaObject } from '../type';
1
+ import type { Dictionary } from 'lodash';
2
+ import type { IReactQueryMode, 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";
@@ -1,4 +1,4 @@
1
- import { OpenAPIObject } from '../type';
1
+ import type { OpenAPIObject } from '../type';
2
2
  export type genMockDataServerConfig = {
3
3
  openAPI: OpenAPIObject;
4
4
  mockFolder: string;
@@ -1,2 +1,2 @@
1
- import { ComponentsObject, ISchemaObject } from '../type';
1
+ import type { ComponentsObject, ISchemaObject } from '../type';
2
2
  export declare function patchSchema<T extends object>(schema: ISchemaObject, schemas: ComponentsObject['schemas']): T;
@@ -1,6 +1,6 @@
1
1
  import type { GenerateServiceProps } from '../index';
2
- import { OpenAPIObject } from '../type';
3
- import { ControllerType, ISchemaItem, ITypeItem, TagAPIDataType } from './type';
2
+ import type { OpenAPIObject } from '../type';
3
+ import type { ControllerType, ISchemaItem, ITypeItem, TagAPIDataType } from './type';
4
4
  export default class ServiceGenerator {
5
5
  protected apiData: TagAPIDataType;
6
6
  protected classNameList: ControllerType[];
@@ -133,6 +133,7 @@ class ServiceGenerator {
133
133
  }
134
134
  }
135
135
  genFile() {
136
+ var _a, _b, _c, _d;
136
137
  if (this.config.full) {
137
138
  try {
138
139
  (0, glob_1.globSync)(`${this.config.serversPath}/**/*`)
@@ -192,8 +193,12 @@ class ServiceGenerator {
192
193
  // 生成枚举翻译
193
194
  const enums = (0, lodash_1.filter)(this.interfaceTPConfigs, (item) => item.isEnum);
194
195
  if (!isGenJavaScript && !isOnlyGenTypeScriptType && !(0, lodash_1.isEmpty)(enums)) {
196
+ const hookCustomTemplateService = (_b = (_a = this.config.hook) === null || _a === void 0 ? void 0 : _a.customTemplates) === null || _b === void 0 ? void 0 : _b[config_2.TypescriptFileType.displayEnumLabel];
195
197
  this.genFileFromTemplate(`${config_2.displayEnumLabelFileName}.ts`, config_2.TypescriptFileType.displayEnumLabel, {
196
- list: enums,
198
+ customTemplate: !!hookCustomTemplateService,
199
+ list: hookCustomTemplateService
200
+ ? hookCustomTemplateService(enums, this.config)
201
+ : enums,
197
202
  namespace: this.config.namespace,
198
203
  interfaceFileName: config_2.interfaceFileName,
199
204
  });
@@ -204,8 +209,12 @@ class ServiceGenerator {
204
209
  !isOnlyGenTypeScriptType &&
205
210
  this.config.isDisplayTypeLabel &&
206
211
  !(0, lodash_1.isEmpty)(displayTypeLabels)) {
212
+ const hookCustomTemplateService = (_d = (_c = this.config.hook) === null || _c === void 0 ? void 0 : _c.customTemplates) === null || _d === void 0 ? void 0 : _d[config_2.TypescriptFileType.displayTypeLabel];
207
213
  this.genFileFromTemplate(`${config_2.displayTypeLabelFileName}.ts`, config_2.TypescriptFileType.displayTypeLabel, {
208
- list: displayTypeLabels,
214
+ customTemplate: !!hookCustomTemplateService,
215
+ list: hookCustomTemplateService
216
+ ? hookCustomTemplateService(enums, this.config)
217
+ : displayTypeLabels,
209
218
  namespace: this.config.namespace,
210
219
  interfaceFileName: config_2.interfaceFileName,
211
220
  });
@@ -335,6 +344,7 @@ class ServiceGenerator {
335
344
  isEnum: enumObj.isEnum,
336
345
  displayLabelFuncName: (0, util_1.camelCase)(`display-${item.name}-Enum`),
337
346
  enumLabelType: enumObj.enumLabelType,
347
+ description: enumObj.description,
338
348
  });
339
349
  }
340
350
  });
@@ -377,7 +387,8 @@ class ServiceGenerator {
377
387
  const newApi = api;
378
388
  try {
379
389
  const params = this.getParamsTP(newApi.parameters, newApi.path) || {};
380
- const body = this.getBodyTP(newApi.requestBody);
390
+ const body = this.getBodyTP(newApi.requestBody, this.config.namespace);
391
+ const bodyWithoutNamespace = this.getBodyTP(newApi.requestBody);
381
392
  const response = this.getResponseTP(newApi.responses);
382
393
  const file = this.getFileTP(newApi.requestBody);
383
394
  let formData = false;
@@ -395,7 +406,7 @@ class ServiceGenerator {
395
406
  const bodyName = (0, lodash_1.upperFirst)(`${functionName}Body`);
396
407
  this.interfaceTPConfigs.push({
397
408
  typeName: bodyName,
398
- type: body === null || body === void 0 ? void 0 : body.type,
409
+ type: bodyWithoutNamespace === null || bodyWithoutNamespace === void 0 ? void 0 : bodyWithoutNamespace.type,
399
410
  isEnum: false,
400
411
  props: [],
401
412
  });
@@ -574,7 +585,7 @@ class ServiceGenerator {
574
585
  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);
575
586
  return (0, util_2.resolveTypeName)(`${namespace}${typeName !== null && typeName !== void 0 ? typeName : data.operationId}Params`);
576
587
  }
577
- getBodyTP(requestBody) {
588
+ getBodyTP(requestBody, namespace) {
578
589
  var _a;
579
590
  const reqBody = this.resolveRefObject(requestBody);
580
591
  if ((0, lodash_1.isEmpty)(reqBody)) {
@@ -594,14 +605,11 @@ class ServiceGenerator {
594
605
  const bodySchema = {
595
606
  mediaType,
596
607
  required,
597
- type: this.getType(schema, this.config.namespace),
608
+ type: this.getType(schema, namespace),
598
609
  isAnonymous: false,
599
610
  };
600
- // 具名 body 场景
601
- if ((0, util_2.isReferenceObject)(schema)) {
602
- bodySchema.type = `${this.config.namespace}.${bodySchema.type}`;
603
- }
604
- else {
611
+ // 匿名 body 场景
612
+ if (!(0, util_2.isReferenceObject)(schema)) {
605
613
  bodySchema.isAnonymous = true;
606
614
  }
607
615
  return bodySchema;
@@ -677,7 +685,7 @@ class ServiceGenerator {
677
685
  resContent[mediaType].schema ||
678
686
  config_2.DEFAULT_SCHEMA);
679
687
  }
680
- responseSchema.type = `${this.config.namespace}.${this.getType(schema, this.config.namespace)}`;
688
+ responseSchema.type = this.getType(schema, this.config.namespace);
681
689
  return responseSchema;
682
690
  }
683
691
  if ((0, util_2.isSchemaObject)(schema)) {
@@ -842,6 +850,7 @@ class ServiceGenerator {
842
850
  isEnum: true,
843
851
  type: Array.isArray(enumArray) ? enumStr : 'string',
844
852
  enumLabelType: enumLabelTypeStr,
853
+ description: schemaObject.description,
845
854
  };
846
855
  }
847
856
  resolveAllOfObject(schemaObject) {
@@ -1,6 +1,7 @@
1
1
  import type { ProjectOptions } from 'ts-morph';
2
- import { type MutuallyExclusive, OperationObject, ParameterObject, SchemaObject } from '../type';
3
- import { TypescriptFileType } from './config';
2
+ import type { OperationObject, ParameterObject, SchemaObject } from '../type';
3
+ import { type MutuallyExclusive } from '../type';
4
+ import type { TypescriptFileType } from './config';
4
5
  export type ITypescriptFileType = keyof typeof TypescriptFileType;
5
6
  export interface APIDataType extends OperationObject {
6
7
  path: string;
@@ -25,6 +26,7 @@ export interface ITypeItem {
25
26
  isEnum: boolean;
26
27
  displayLabelFuncName?: string;
27
28
  enumLabelType?: string;
29
+ description?: string;
28
30
  }
29
31
  export type ICustomSchemaObject = SchemaObject & {
30
32
  isAllowed?: boolean;
@@ -1,5 +1,5 @@
1
- import { ArraySchemaObject, BinaryArraySchemaObject, ComponentsObject, ISchemaObject, NonArraySchemaObject, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from '../type';
2
- import { ITypeItem } from './type';
1
+ import type { ArraySchemaObject, BinaryArraySchemaObject, ComponentsObject, ISchemaObject, NonArraySchemaObject, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from '../type';
2
+ import type { ITypeItem } from './type';
3
3
  export declare function stripDot(str: string): string;
4
4
  export declare function resolveTypeName(typeName: string): string;
5
5
  export declare function getRefName(refObject: ReferenceObject | string): string;
@@ -98,8 +98,8 @@ function getDefaultType(schemaObject, namespace = '', schemas) {
98
98
  return schemaObject;
99
99
  }
100
100
  if (isReferenceObject(schemaObject)) {
101
- return getRefName(schemaObject);
102
- // return [namespace, getRefName(schemaObject)].filter((s) => s).join('.');
101
+ // return getRefName(schemaObject);
102
+ return [namespace, getRefName(schemaObject)].filter((s) => s).join('.');
103
103
  }
104
104
  let type = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.type;
105
105
  const dateEnum = ['Date', 'date', 'dateTime', 'date-time', 'datetime'];
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { TypescriptFileType } from './generator/config';
2
- import { APIDataType } from './generator/type';
3
- import { ComponentsObject, type GetSchemaByApifoxProps, IPriorityRule, IReactQueryMode, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from './type';
1
+ import type { TypescriptFileType } from './generator/config';
2
+ import type { APIDataType, ITypeItem } from './generator/type';
3
+ import type { ComponentsObject, IPriorityRule, IReactQueryMode, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from './type';
4
+ import { type GetSchemaByApifoxProps } from './type';
4
5
  export * from './generator/patchSchema';
5
6
  export type GenerateServiceProps = {
6
7
  /**
@@ -203,6 +204,14 @@ export type GenerateServiceProps = {
203
204
  * 自定义 serviceController 模板
204
205
  */
205
206
  [TypescriptFileType.serviceController]?: <T, U>(item: T, context: U) => string;
207
+ /**
208
+ * 自定义 displayEnumLabel 模板
209
+ */
210
+ [TypescriptFileType.displayEnumLabel]?: <T, U>(enums: T[], config: U) => ITypeItem[];
211
+ /**
212
+ * 自定义 displayTypeLabel 模板
213
+ */
214
+ [TypescriptFileType.displayTypeLabel]?: <T, U>(types: T[], config: U) => ITypeItem[];
206
215
  };
207
216
  };
208
217
  };
package/dist/index.js CHANGED
@@ -4,7 +4,6 @@ exports.generateService = generateService;
4
4
  const tslib_1 = require("tslib");
5
5
  const lodash_1 = require("lodash");
6
6
  const config_1 = require("./config");
7
- const config_2 = require("./generator/config");
8
7
  const mockGenarator_1 = require("./generator/mockGenarator");
9
8
  const serviceGenarator_1 = tslib_1.__importDefault(require("./generator/serviceGenarator"));
10
9
  const util_1 = require("./util");
@@ -1,5 +1,5 @@
1
- import { OpenAPIV3 } from 'openapi-types';
2
- import { OpenAPIObject } from '../type';
1
+ import type { OpenAPIV3 } from 'openapi-types';
2
+ import type { OpenAPIObject } from '../type';
3
3
  export default class OpenAPIGeneratorMockJs {
4
4
  protected openAPI: OpenAPIObject;
5
5
  constructor(openAPI: OpenAPIObject);
@@ -1,5 +1,5 @@
1
- import { OpenAPIV3 } from 'openapi-types';
2
- import { OpenAPIObject, ParameterObject, ReferenceObject, SchemaObject } from '../type';
1
+ import type { OpenAPIV3 } from 'openapi-types';
2
+ import type { OpenAPIObject, ParameterObject, ReferenceObject, SchemaObject } from '../type';
3
3
  export declare function objectify<T>(thing: T): T;
4
4
  export declare function get(openAPI: OpenAPIObject, path: string): OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
5
5
  export declare function normalizeArray(arr: string[] | string): string[];
package/dist/type.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { OpenAPIV3 } from 'openapi-types';
2
- import { PriorityRule, ReactQueryMode, SchemaObjectFormat, SchemaObjectType } from './config';
1
+ import type { OpenAPIV3 } from 'openapi-types';
2
+ import type { PriorityRule, ReactQueryMode, SchemaObjectFormat, SchemaObjectType } from './config';
3
3
  export type MutuallyExclusive<T> = {
4
4
  [K in keyof T]: {
5
5
  [P in K]: T[K];
package/dist/util.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { OpenAPI } from 'openapi-types';
2
- import { type GetSchemaByApifoxProps, OpenAPIObject } from './type';
1
+ import type { OpenAPI } from 'openapi-types';
2
+ import type { OpenAPIObject } from './type';
3
+ import { type GetSchemaByApifoxProps } from './type';
3
4
  export declare const getImportStatement: (requestLibPath: string) => string;
4
5
  export declare const getOpenAPIConfigByApifox: (props: GetSchemaByApifoxProps) => Promise<OpenAPI.Document<{}>>;
5
6
  export declare const getOpenAPIConfig: (schemaPath: string, authorization?: string, timeout?: number) => Promise<OpenAPI.Document<{}>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "1.5.2",
3
+ "version": "1.6.1",
4
4
  "description": "Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas",
5
5
  "engines": {
6
6
  "node": ">=18.0.0",
@@ -68,8 +68,10 @@
68
68
  "husky": "^9.0.11",
69
69
  "lint-staged": "^16.0.0",
70
70
  "openapi-types": "^12.1.3",
71
+ "sanitize-filename": "^1.6.3",
71
72
  "ts-node": "^10.9.2",
72
- "typescript": "5.8.3"
73
+ "typescript": "5.8.3",
74
+ "vitest": "^3.2.4"
73
75
  },
74
76
  "keywords": [
75
77
  "openapi",
@@ -92,9 +94,7 @@
92
94
  "changeset": "changeset",
93
95
  "lint": "eslint ./src --report-unused-disable-directives --max-warnings=0",
94
96
  "lint:fix": "eslint ./src --report-unused-disable-directives --max-warnings=0 --fix",
95
- "test": "rm -rf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
96
- "test:new": "npm run build && cd ./test && node ./test.js && cd ..",
97
- "test:windows": "rimraf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
97
+ "test:unit": "vitest",
98
98
  "openapi-ts-request": "openapi-ts"
99
99
  }
100
100
  }
@@ -2,9 +2,14 @@
2
2
  // @ts-ignore
3
3
  import * as {{ namespace }} from './{{ interfaceFileName }}';
4
4
 
5
+ {% if customTemplate %}
6
+ {{ list }}
7
+ {% else %}
8
+
5
9
  {% for type in list -%}
6
10
  export function {{ type.displayLabelFuncName }}(field: {{ namespace }}.{{ type.typeName }}) {
7
11
  return ({{ type.enumLabelType }})[field]
8
12
  }
9
13
 
10
- {% endfor -%}
14
+ {% endfor -%}
15
+ {%- endif %}
@@ -2,6 +2,10 @@
2
2
  // @ts-ignore
3
3
  import * as {{ namespace }} from './{{ interfaceFileName }}';
4
4
 
5
+ {% if customTemplate %}
6
+ {{ list }}
7
+ {% else %}
8
+
5
9
  {% for type in list -%}
6
10
  {%- if type.props.length %}
7
11
  export function display{{ type.typeName }}(field: keyof {{ namespace }}.{{ type.typeName }}) {
@@ -20,7 +24,7 @@ import * as {{ namespace }} from './{{ interfaceFileName }}';
20
24
  display{{ p.type | safe }}(field as keyof {{ namespace }}.{{ p.type }})
21
25
  {%- else %}
22
26
  {{ p.name }}: '{{ p.desc if p.desc else p.name }}',
23
- {%- endif %}
27
+ {%- endif %}
24
28
  {%- endfor %}
25
29
  {%- if prop.length > 1 %}
26
30
  }[field]
@@ -39,4 +43,5 @@ import * as {{ namespace }} from './{{ interfaceFileName }}';
39
43
  }
40
44
  {%- endif %}
41
45
 
42
- {% endfor -%}
46
+ {% endfor -%}
47
+ {%- endif %}