openapi-ts-request 0.4.6 → 0.4.8
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 +54 -52
- package/dist/generator/serviceGenarator.js +12 -8
- package/dist/generator/util.js +1 -1
- package/package.json +1 -1
- package/prettier.config.cjs +2 -1
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
## 介绍
|
|
2
2
|
|
|
3
|
-
[](https://github.com/openapi-ui/openapi-ts-request)
|
|
4
|
-
[](https://www.npmjs.com/package/openapi-ts-request)
|
|
5
|
-

|
|
3
|
+
[](https://github.com/openapi-ui/openapi-ts-request) [](https://www.npmjs.com/package/openapi-ts-request) 
|
|
6
4
|
|
|
7
5
|
<a href="https://github.com/openapi-ui/openapi-ts-request/blob/master/README-en_US.md">English</a> | 简体中文
|
|
8
6
|
|
|
@@ -10,12 +8,12 @@
|
|
|
10
8
|
|
|
11
9
|
## 功能
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
- 支持 Swagger2.0/OpenAPI 3.0,3.1 定义
|
|
12
|
+
- 生成 TS 类型, 请求客户端, 请求模拟服务, 枚举, 类型字段翻译, JSON Schemas
|
|
13
|
+
- 支持通过 npx、CLI、Nodejs 的方式使用
|
|
14
|
+
- 支持自定义请求工具函数, 支持 Fetch、Axios、UniApp-Request、Node.js、XHR 客户端
|
|
15
|
+
- 支持通过 tags 过滤生成结果
|
|
16
|
+
- 支持 JSON 定义文件
|
|
19
17
|
|
|
20
18
|
## 使用
|
|
21
19
|
|
|
@@ -29,21 +27,22 @@ pnpm i openapi-ts-request -D
|
|
|
29
27
|
|
|
30
28
|
### CosmiConfig
|
|
31
29
|
|
|
32
|
-
在项目根目录新建
|
|
33
|
-
|
|
30
|
+
在项目根目录新建 `openapi-ts-request.config.ts`
|
|
31
|
+
|
|
32
|
+
> 配置文件还支持 **_.openapi-ts-request.ts_**, **_openapi-ts-request.config.cjs_** 等格式,参考 [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig?tab=readme-ov-file#cosmiconfig)
|
|
34
33
|
|
|
35
34
|
```ts
|
|
36
|
-
import type { GenerateServiceProps } from 'openapi-ts-request'
|
|
35
|
+
import type { GenerateServiceProps } from 'openapi-ts-request';
|
|
37
36
|
|
|
38
37
|
export default {
|
|
39
38
|
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
|
|
40
|
-
} as GenerateServiceProps
|
|
39
|
+
} as GenerateServiceProps;
|
|
41
40
|
```
|
|
42
41
|
|
|
43
42
|
支持传入数组配置进行生成
|
|
44
43
|
|
|
45
44
|
```ts
|
|
46
|
-
import type { GenerateServiceProps } from 'openapi-ts-request'
|
|
45
|
+
import type { GenerateServiceProps } from 'openapi-ts-request';
|
|
47
46
|
|
|
48
47
|
export default [
|
|
49
48
|
{
|
|
@@ -53,11 +52,11 @@ export default [
|
|
|
53
52
|
{
|
|
54
53
|
schemaPath: 'http://auth.swagger.io/v2/swagger.json',
|
|
55
54
|
serversPath: './src/apis/auth',
|
|
56
|
-
}
|
|
57
|
-
] as GenerateServiceProps[]
|
|
55
|
+
},
|
|
56
|
+
] as GenerateServiceProps[];
|
|
58
57
|
```
|
|
59
58
|
|
|
60
|
-
在
|
|
59
|
+
在 `package.json` 的 `script` 中添加命令: `"openapi": "openapi-ts",`
|
|
61
60
|
|
|
62
61
|
生成结果:
|
|
63
62
|
|
|
@@ -67,18 +66,18 @@ npm run openapi
|
|
|
67
66
|
|
|
68
67
|
### JS
|
|
69
68
|
|
|
70
|
-
任意目录
|
|
69
|
+
任意目录 `xxx/xxx` 新建 `openapi-ts-request.config.js`
|
|
71
70
|
|
|
72
71
|
```ts
|
|
73
|
-
const { generateService } = require('openapi-ts-request')
|
|
72
|
+
const { generateService } = require('openapi-ts-request');
|
|
74
73
|
|
|
75
74
|
generateService({
|
|
76
75
|
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
|
|
77
76
|
serversPath: './apis',
|
|
78
|
-
})
|
|
77
|
+
});
|
|
79
78
|
```
|
|
80
79
|
|
|
81
|
-
在
|
|
80
|
+
在 `package.json` 的 `script` 中添加命令: `"openapi": "node xxx/xxx/openapi-ts-request.config.js"`
|
|
82
81
|
|
|
83
82
|
生成结果:
|
|
84
83
|
|
|
@@ -88,18 +87,18 @@ npm run openapi
|
|
|
88
87
|
|
|
89
88
|
### TS
|
|
90
89
|
|
|
91
|
-
任意目录
|
|
90
|
+
任意目录 `xxx/xxx` 新建 `openapi-ts-request.config.ts`
|
|
92
91
|
|
|
93
92
|
```ts
|
|
94
|
-
const { generateService } = require('openapi-ts-request')
|
|
93
|
+
const { generateService } = require('openapi-ts-request');
|
|
95
94
|
|
|
96
95
|
generateService({
|
|
97
96
|
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
|
|
98
97
|
serversPath: './apis',
|
|
99
|
-
})
|
|
98
|
+
});
|
|
100
99
|
```
|
|
101
100
|
|
|
102
|
-
在
|
|
101
|
+
在 `package.json` 的 `script` 中添加命令: `"openapi": "ts-node xxx/xxx/openapi-ts-request.config.ts",`
|
|
103
102
|
|
|
104
103
|
生成结果:
|
|
105
104
|
|
|
@@ -158,32 +157,32 @@ openapi --i ./spec.json --o ./apis
|
|
|
158
157
|
|
|
159
158
|
## 参数
|
|
160
159
|
|
|
161
|
-
| 属性
|
|
162
|
-
|
|
|
163
|
-
| schemaPath
|
|
164
|
-
| serversPath
|
|
165
|
-
| requestLibPath
|
|
166
|
-
| allowedTags
|
|
167
|
-
| requestOptionsType
|
|
168
|
-
| requestImportStatement | 否
|
|
169
|
-
| apiPrefix
|
|
170
|
-
| isDisplayTypeLabel
|
|
171
|
-
| isGenJsonSchemas
|
|
172
|
-
| mockFolder
|
|
173
|
-
| nullable
|
|
174
|
-
| isCamelCase
|
|
175
|
-
| hook
|
|
160
|
+
| 属性 | 必填 | 类型 | 默认值 | 说明 |
|
|
161
|
+
| --- | --- | --- | --- | --- |
|
|
162
|
+
| schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
|
|
163
|
+
| serversPath | 否 | string | './src/apis' | 生成结果的文件夹路径 |
|
|
164
|
+
| requestLibPath | 否 | string | - | 自定义请求方法路径,例如:'@/request', 'node-fetch' |
|
|
165
|
+
| allowedTags | 否 | string[] | - | 根据指定的 tags 生成结果 |
|
|
166
|
+
| requestOptionsType | 否 | string | '{ [key: string]: unknown }' | 自定义请求方法 options 参数类型 |
|
|
167
|
+
| requestImportStatement | 否 | string | - | 自定义请求方法表达式,例如:"const request = require('@/request')" |
|
|
168
|
+
| apiPrefix | 否 | string | - | api path的前缀,例如:'api'(动态变量), "'api'"(字符串) |
|
|
169
|
+
| isDisplayTypeLabel | 否 | boolean | false | 是否生成 type 对应的label |
|
|
170
|
+
| isGenJsonSchemas | 否 | boolean | false | 是否生成 JSON Schemas |
|
|
171
|
+
| mockFolder | 否 | string | './mocks' | mock文件路径 |
|
|
172
|
+
| nullable | 否 | boolean | false | 使用 null 代替可选 |
|
|
173
|
+
| isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
|
|
174
|
+
| hook | 否 | [Custom Hook](#Custom-Hook) | - | 自定义 hook |
|
|
176
175
|
|
|
177
176
|
## 自定义 Hook
|
|
178
177
|
|
|
179
|
-
| 属性
|
|
180
|
-
|
|
|
181
|
-
| afterOpenApiDataInited | (openAPIData: OpenAPIObject) => OpenAPIObject
|
|
182
|
-
| customFunctionName
|
|
183
|
-
| customTypeName
|
|
184
|
-
| customClassName
|
|
185
|
-
| customType
|
|
186
|
-
| customFileNames
|
|
178
|
+
| 属性 | 类型 | 说明 |
|
|
179
|
+
| --- | --- | --- |
|
|
180
|
+
| afterOpenApiDataInited | (openAPIData: OpenAPIObject) => OpenAPIObject | 自定义 OpenAPI 数据 |
|
|
181
|
+
| customFunctionName | (data: APIDataType) => string | 自定义请求方法函数名称 |
|
|
182
|
+
| customTypeName | (data: APIDataType) => string | 自定义类型名称 |
|
|
183
|
+
| customClassName | (tagName: string) => string | 自定义标签名 |
|
|
184
|
+
| customType | (<br>schemaObject: SchemaObject \| ReferenceObject,<br>namespace: string,<br>originGetType:(schemaObject: SchemaObject \| ReferenceObject, namespace: string) => string,<br>) => string | 自定义类型 <br> _返回非字符串将使用默认方法获取type_ |
|
|
185
|
+
| customFileNames | (<br>operationObject: OperationObject,<br>apiPath: string,<br>apiMethod: string,<br>) => string[] | 自定义生成的请求客户端文件名称,可以返回多个文件名称的数组(表示生成多个文件). <br> _返回为空,则使用默认的方法获取_ |
|
|
187
186
|
|
|
188
187
|
## JSON Schemas
|
|
189
188
|
|
|
@@ -191,7 +190,10 @@ openapi --i ./spec.json --o ./apis
|
|
|
191
190
|
- 提供一个解析 schema 的函数,用于将 `$ref`,`$allOf` 的引用填充到 `当前schema`
|
|
192
191
|
|
|
193
192
|
```ts
|
|
194
|
-
export declare function patchSchema<T extends object>(
|
|
193
|
+
export declare function patchSchema<T extends object>(
|
|
194
|
+
schema: ISchemaObject,
|
|
195
|
+
schemas: ComponentsObject['schemas']
|
|
196
|
+
): T;
|
|
195
197
|
```
|
|
196
198
|
|
|
197
199
|
## Mock
|
|
@@ -202,8 +204,8 @@ export declare function patchSchema<T extends object>(schema: ISchemaObject, sch
|
|
|
202
204
|
|
|
203
205
|
### 环境要求
|
|
204
206
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
+
- node 18+
|
|
208
|
+
- pnpm 9+
|
|
207
209
|
|
|
208
210
|
### 提交 Pull Request
|
|
209
211
|
|
|
@@ -219,4 +221,4 @@ export declare function patchSchema<T extends object>(schema: ISchemaObject, sch
|
|
|
219
221
|
|
|
220
222
|
- [openapi2typescript](https://github.com/chenshuai2144/openapi2typescript)
|
|
221
223
|
|
|
222
|
-
ps:由于 openapi2typescript 仓库作者不怎么维护这个工具,不会主动增加功能,有些激进的pr也不再合并,为了更大的自主性,也为了方便自己更好的维护此工具,所以基于此仓库重构代码并添加了很多功能,感谢原作者!
|
|
224
|
+
ps:由于 openapi2typescript 仓库作者不怎么维护这个工具,不会主动增加功能,有些激进的pr也不再合并,为了更大的自主性,也为了方便自己更好的维护此工具,所以基于此仓库重构代码并添加了很多功能,感谢原作者!
|
|
@@ -79,11 +79,14 @@ class ServiceGenerator {
|
|
|
79
79
|
list: interfaceTPConfigs,
|
|
80
80
|
});
|
|
81
81
|
// 生成枚举翻译
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
const enums = (0, lodash_1.filter)(interfaceTPConfigs, (item) => item.isEnum);
|
|
83
|
+
if (!(0, lodash_1.isEmpty)(enums)) {
|
|
84
|
+
this.genFileFromTemplate(`${config_1.displayEnumLabelFileName}.ts`, config_1.TypescriptFileType.displayEnumLabel, {
|
|
85
|
+
list: enums,
|
|
86
|
+
namespace: this.config.namespace,
|
|
87
|
+
interfaceFileName: config_1.interfaceFileName,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
87
90
|
// 生成 type 翻译
|
|
88
91
|
if (this.config.isDisplayTypeLabel) {
|
|
89
92
|
this.genFileFromTemplate(`${config_1.displayTypeLabelFileName}.ts`, config_1.TypescriptFileType.displayTypeLabel, {
|
|
@@ -206,7 +209,7 @@ class ServiceGenerator {
|
|
|
206
209
|
}
|
|
207
210
|
// 判断哪些 schema 需要添加进 type, schemas 渲染数组
|
|
208
211
|
if ((0, lodash_1.isEmpty)(this.config.allowedTags) ||
|
|
209
|
-
schema.isAllowed) {
|
|
212
|
+
(schema === null || schema === void 0 ? void 0 : schema.isAllowed)) {
|
|
210
213
|
const isEnum = result.isEnum;
|
|
211
214
|
const typeName = (0, util_1.resolveTypeName)(schemaKey);
|
|
212
215
|
lastTypes.push({
|
|
@@ -293,8 +296,9 @@ class ServiceGenerator {
|
|
|
293
296
|
prefix.startsWith('"') ||
|
|
294
297
|
prefix.startsWith('`')) {
|
|
295
298
|
const finalPrefix = prefix.slice(1, prefix.length - 1);
|
|
296
|
-
|
|
297
|
-
|
|
299
|
+
const firstPath = formattedPath.split('/')[1];
|
|
300
|
+
if (firstPath === finalPrefix ||
|
|
301
|
+
`/${firstPath}` === finalPrefix) {
|
|
298
302
|
return formattedPath;
|
|
299
303
|
}
|
|
300
304
|
return `${finalPrefix}${formattedPath}`;
|
package/dist/generator/util.js
CHANGED
|
@@ -279,7 +279,7 @@ function markAllowSchema(schemaStr, schemas) {
|
|
|
279
279
|
(0, lodash_1.forEach)(refs, (ref) => {
|
|
280
280
|
const refPaths = ref.split('/');
|
|
281
281
|
const schema = schemas === null || schemas === void 0 ? void 0 : schemas[refPaths[refPaths.length - 1]];
|
|
282
|
-
if (
|
|
282
|
+
if (schema && !schema.isAllowed) {
|
|
283
283
|
schema.isAllowed = true;
|
|
284
284
|
markAllowSchema(JSON.stringify(schema), schemas);
|
|
285
285
|
}
|
package/package.json
CHANGED