openapi-ts-request 0.13.2 → 0.13.4

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
@@ -4,7 +4,14 @@
4
4
 
5
5
  <a href="https://github.com/openapi-ui/openapi-ts-request/blob/master/README-en_US.md">English</a> | 简体中文
6
6
 
7
- 根据 [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成 TS 类型, 客户端请求函数, 模拟请求响应服务, 枚举, 类型字段翻译, JSON Schemas
7
+ 根据 [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成:
8
+
9
+ - TS 类型
10
+ - 客户端请求函数
11
+ - 模拟请求响应服务
12
+ - 枚举和枚举翻译
13
+ - 类型字段翻译
14
+ - JSON Schemas
8
15
 
9
16
  文档:[使用手册](https://github.com/openapi-ui/openapi-ts-request/issues/100)
10
17
 
@@ -38,6 +45,8 @@ pnpm i openapi-ts-request -D
38
45
  import type { GenerateServiceProps } from 'openapi-ts-request';
39
46
 
40
47
  export default {
48
+ // schemaPath: './openapi.json', // 本地openapi文件
49
+ // serversPath: './src/apis', // 接口存放路径
41
50
  schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
42
51
  } as GenerateServiceProps;
43
52
  ```
@@ -61,12 +70,47 @@ export default [
61
70
 
62
71
  在 `package.json` 的 `script` 中添加命令: `"openapi": "openapi-ts",`
63
72
 
64
- 生成结果:
73
+ 运行:
65
74
 
66
75
  ```bash
67
76
  npm run openapi
68
77
  ```
69
78
 
79
+ 生成的接口:
80
+
81
+ ```bash
82
+ src/apis/index.ts #接口入口文件
83
+ src/apis/types.ts #类型定义文件
84
+ src/apis/pet.ts #接口文件
85
+ ```
86
+
87
+ ```typescript
88
+ // src/apis/pet.ts
89
+
90
+ /* eslint-disable */
91
+ // @ts-ignore
92
+ import request from 'axios';
93
+
94
+ import * as API from './types';
95
+
96
+ /** Update an existing pet PUT /pet */
97
+ export async function updatePet(
98
+ body: API.Pet,
99
+ options?: { [key: string]: unknown }
100
+ ) {
101
+ return request<unknown>(`/pet`, {
102
+ method: 'PUT',
103
+ headers: {
104
+ 'Content-Type': 'application/json',
105
+ },
106
+ data: body,
107
+ ...(options || {}),
108
+ });
109
+ }
110
+
111
+ // ... 其他接口
112
+ ```
113
+
70
114
  ### JS
71
115
 
72
116
  任意目录 `xxx/xxx` 新建 `openapi-ts-request.config.js`
@@ -82,7 +126,7 @@ generateService({
82
126
 
83
127
  在 `package.json` 的 `script` 中添加命令: `"openapi": "node xxx/xxx/openapi-ts-request.config.js"`
84
128
 
85
- 生成结果:
129
+ 运行:
86
130
 
87
131
  ```bash
88
132
  npm run openapi
@@ -103,7 +147,7 @@ generateService({
103
147
 
104
148
  在 `package.json` 的 `script` 中添加命令: `"openapi": "ts-node xxx/xxx/openapi-ts-request.config.ts",`
105
149
 
106
- 生成结果:
150
+ 运行:
107
151
 
108
152
  ```bash
109
153
  npm run openapi
@@ -160,7 +204,7 @@ $ openapi --help
160
204
  -h, --help display help for command
161
205
  ```
162
206
 
163
- 生成结果:
207
+ 运行:
164
208
 
165
209
  ```bash
166
210
  openapi --i ./spec.json --o ./apis
@@ -171,7 +215,7 @@ openapi --i ./spec.json --o ./apis
171
215
  | 属性 | 必填 | 类型 | 默认值 | 说明 |
172
216
  | --- | --- | --- | --- | --- |
173
217
  | schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
174
- | serversPath | 否 | string | './src/apis' | 生成结果的文件夹路径 |
218
+ | serversPath | 否 | string | './src/apis' | 运行结果文件夹路径 |
175
219
  | requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
176
220
  | enableLogging | 否 | boolean | false | 是否开启日志 |
177
221
  | priorityRule | 否 | string | 'include' | 模式规则,可选include/exclude/both |
@@ -722,6 +722,13 @@ class ServiceGenerator {
722
722
  return `${value}:"${enumLabel}"`;
723
723
  }).join(',')}}`;
724
724
  }
725
+ else if (schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject['x-apifox-enum']) {
726
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => {
727
+ var _a;
728
+ const enumLabel = (_a = (0, lodash_1.find)(schemaObject['x-apifox-enum'], (item) => item.value === value)) === null || _a === void 0 ? void 0 : _a.description;
729
+ return `${value}:"${enumLabel}"`;
730
+ }).join(',')}}`;
731
+ }
725
732
  else {
726
733
  if (config_1.numberEnum.includes(schemaObject.type) || (0, util_1.isAllNumber)(enumArray)) {
727
734
  enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `"NUMBER_${value}":${Number(value)}`).join(',')}}`;
@@ -47,6 +47,11 @@ export declare function inferSchema(thing: ParameterObject | SchemaObject | Refe
47
47
  'x-apifox'?: {
48
48
  enumDescriptions: Record<string, string>;
49
49
  };
50
+ 'x-apifox-enum'?: {
51
+ value: string;
52
+ name: string;
53
+ description: string;
54
+ }[];
50
55
  items: import("../type").ISchemaObject;
51
56
  } | {
52
57
  type: string;
@@ -92,5 +97,10 @@ export declare function inferSchema(thing: ParameterObject | SchemaObject | Refe
92
97
  'x-apifox'?: {
93
98
  enumDescriptions: Record<string, string>;
94
99
  };
100
+ 'x-apifox-enum'?: {
101
+ value: string;
102
+ name: string;
103
+ description: string;
104
+ }[];
95
105
  };
96
106
  export declare function getRandomInt(min: number, max: number): number;
package/dist/type.d.ts CHANGED
@@ -17,6 +17,11 @@ type ICustomBaseSchemaObject = {
17
17
  'x-apifox'?: {
18
18
  enumDescriptions: Record<string, string>;
19
19
  };
20
+ 'x-apifox-enum'?: {
21
+ value: string;
22
+ name: string;
23
+ description: string;
24
+ }[];
20
25
  };
21
26
  export type ArraySchemaObject = Modify<OpenAPIV3.ArraySchemaObject, ICustomBaseSchemaObject & {
22
27
  items: ISchemaObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "0.13.2",
3
+ "version": "0.13.4",
4
4
  "description": "Swagger2/OpenAPI3 to TypeScript, request client, request mock service, enum, type field label, JSON Schemas",
5
5
  "engines": {
6
6
  "node": ">=18.0.0",