openapi-ts-request 1.5.1 → 1.6.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
@@ -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
  });
@@ -599,7 +609,7 @@ class ServiceGenerator {
599
609
  };
600
610
  // 具名 body 场景
601
611
  if ((0, util_2.isReferenceObject)(schema)) {
602
- bodySchema.type = `${this.config.namespace}.${bodySchema.type}`;
612
+ bodySchema.type = `${bodySchema.type}`;
603
613
  }
604
614
  else {
605
615
  bodySchema.isAnonymous = true;
@@ -677,7 +687,7 @@ class ServiceGenerator {
677
687
  resContent[mediaType].schema ||
678
688
  config_2.DEFAULT_SCHEMA);
679
689
  }
680
- responseSchema.type = `${this.config.namespace}.${this.getType(schema, this.config.namespace)}`;
690
+ responseSchema.type = `${this.getType(schema, this.config.namespace)}`;
681
691
  return responseSchema;
682
692
  }
683
693
  if ((0, util_2.isSchemaObject)(schema)) {
@@ -796,7 +806,7 @@ class ServiceGenerator {
796
806
  enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `"STRING_NUMBER_${value}"="${value}"`).join(',')}}`;
797
807
  }
798
808
  else {
799
- enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `${value}="${value}"`).join(',')}}`;
809
+ enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `"${value}"="${value}"`).join(',')}}`;
800
810
  }
801
811
  // 翻译枚举
802
812
  if (schemaObject['x-enum-varnames'] && schemaObject['x-enum-comments']) {
@@ -835,13 +845,14 @@ class ServiceGenerator {
835
845
  enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `"${value}":"STRING_NUMBER_${value}"`).join(',')}}`;
836
846
  }
837
847
  else {
838
- enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `${value}:"${value}"`).join(',')}}`;
848
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `"${value}":"${value}"`).join(',')}}`;
839
849
  }
840
850
  }
841
851
  return {
842
852
  isEnum: true,
843
853
  type: Array.isArray(enumArray) ? enumStr : 'string',
844
854
  enumLabelType: enumLabelTypeStr,
855
+ description: schemaObject.description,
845
856
  };
846
857
  }
847
858
  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,7 +204,15 @@ 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
  };
209
- export declare function generateService({ requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, authorization, isTranslateToEnglishTag, priorityRule, timeout, reactQueryMode, apifoxConfig, ...rest }: GenerateServiceProps): Promise<void>;
218
+ export declare function generateService({ requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, includePaths, excludePaths, authorization, isTranslateToEnglishTag, priorityRule, timeout, reactQueryMode, apifoxConfig, ...rest }: GenerateServiceProps): Promise<void>;
package/dist/index.js CHANGED
@@ -4,14 +4,13 @@ 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");
11
10
  tslib_1.__exportStar(require("./generator/patchSchema"), exports);
12
11
  function generateService(_a) {
13
12
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
14
- var { requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, authorization, isTranslateToEnglishTag, priorityRule = config_1.PriorityRule.include, timeout = 60000, reactQueryMode = config_1.ReactQueryMode.react, apifoxConfig } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "includeTags", "excludeTags", "authorization", "isTranslateToEnglishTag", "priorityRule", "timeout", "reactQueryMode", "apifoxConfig"]);
13
+ var { requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, includePaths, excludePaths, authorization, isTranslateToEnglishTag, priorityRule = config_1.PriorityRule.include, timeout = 60000, reactQueryMode = config_1.ReactQueryMode.react, apifoxConfig } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "includeTags", "excludeTags", "includePaths", "excludePaths", "authorization", "isTranslateToEnglishTag", "priorityRule", "timeout", "reactQueryMode", "apifoxConfig"]);
15
14
  if (!schemaPath && !apifoxConfig) {
16
15
  return;
17
16
  }
@@ -31,10 +30,18 @@ function generateService(_a) {
31
30
  const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
32
31
  const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, enableLogging: false, priorityRule, includeTags: includeTags
33
32
  ? (0, lodash_1.map)(includeTags, (item) => typeof item === 'string' ? item.toLowerCase() : item)
34
- : priorityRule === config_1.PriorityRule.include
33
+ : priorityRule === config_1.PriorityRule.include ||
34
+ priorityRule === config_1.PriorityRule.both
35
+ ? [/.*/g]
36
+ : null, includePaths: includePaths
37
+ ? (0, lodash_1.map)(includePaths, (item) => typeof item === 'string' ? item.toLowerCase() : item)
38
+ : priorityRule === config_1.PriorityRule.include ||
39
+ priorityRule === config_1.PriorityRule.both
35
40
  ? [/.*/g]
36
41
  : null, excludeTags: excludeTags
37
42
  ? (0, lodash_1.map)(excludeTags, (item) => typeof item === 'string' ? item.toLowerCase() : item)
43
+ : null, excludePaths: excludePaths
44
+ ? (0, lodash_1.map)(excludePaths, (item) => typeof item === 'string' ? item.toLowerCase() : item)
38
45
  : null, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', isGenReactQuery: false, reactQueryMode, isGenJavaScript: false, isDisplayTypeLabel: false, isGenJsonSchemas: false, nullable: false, isOnlyGenTypeScriptType: false, isCamelCase: true, isSupportParseEnumDesc: false, full: true }, rest), openAPI);
39
46
  serviceGenerator.genFile();
40
47
  if (mockFolder) {
@@ -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.1",
3
+ "version": "1.6.0",
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 %}