swagger-to-axios 1.0.6 → 1.0.7
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 +53 -32
- package/lib/utils/index.d.ts +8 -2
- package/lib/utils/index.js +41 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,50 +7,75 @@
|
|
|
7
7
|
|
|
8
8
|
```TypeScript
|
|
9
9
|
/** swagger 文档配置项 */
|
|
10
|
-
interface SwaggerDocument {
|
|
10
|
+
declare interface SwaggerDocument {
|
|
11
11
|
/** swagger 文档地址 */
|
|
12
12
|
url: string;
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
urlType?: string;
|
|
18
|
-
/** 生成文件后该文档的文件夹名称,默认会使用随机数作为文件夹名称 */
|
|
13
|
+
/**
|
|
14
|
+
* 生成文件后该文档的文件夹名称。
|
|
15
|
+
* 默认值:字符串随机数。
|
|
16
|
+
* */
|
|
19
17
|
name?: string;
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
/** 生成文件配置项 */
|
|
23
|
-
interface Config {
|
|
24
|
-
/**
|
|
21
|
+
declare interface Config {
|
|
22
|
+
/**
|
|
23
|
+
* 生成文件时,每个函数是否携带 baseURL 属性。
|
|
24
|
+
* 默认值:true。
|
|
25
|
+
* */
|
|
25
26
|
includeBaseURL?: boolean;
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
28
|
+
* cli类型。
|
|
29
|
+
* 默认值:VueCli。
|
|
30
|
+
* 注:
|
|
31
|
+
* 1、如果 includeBaseURL 为 false,则不需要配置该项。
|
|
29
32
|
*/
|
|
30
|
-
cliType?:
|
|
33
|
+
cliType?: 'Vite' | 'VueCli';
|
|
31
34
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
+
* host 的配置名称。
|
|
36
|
+
* 默认值:VUE_APP_HOST | VITE_APP_HOST(根据 cliType 属性)。
|
|
37
|
+
* 注:
|
|
38
|
+
* 1、如果 includeBaseURL 为 false,则不需要配置该项。
|
|
39
|
+
* 2、如果 swagger 的 host 填写了正确的地址,你也可以完全不配置该项,生成的代码会使用三目运算符,并将非的表达式设置为 swagger 的 host。
|
|
35
40
|
*/
|
|
36
41
|
envHostName?: string;
|
|
37
42
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
43
|
+
* 网络协议的配置名称。
|
|
44
|
+
* 默认值:VUE_APP_PROTOCOL | VITE_APP_PROTOCOL(根据 cliType 属性)。
|
|
45
|
+
* 注:
|
|
46
|
+
* 1、如果 includeBaseURL 为 false,则不需要配置该项。
|
|
47
|
+
* 2、推荐只在生产环境和开发环境使用不同协议时配置该项。
|
|
48
|
+
* 3、VUE_APP_PROTOCOL / VITE_APP_PROTOCOL 的值应该为 'https' 或者 'http'。
|
|
41
49
|
*/
|
|
42
50
|
envProtocolName?: string;
|
|
43
51
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
52
|
+
* 是否使用 https。
|
|
53
|
+
* 默认值:false。
|
|
54
|
+
* 注:
|
|
55
|
+
* 1、如果 includeBaseURL 为 false,则不需要配置该项。
|
|
56
|
+
* */
|
|
46
57
|
https?: boolean;
|
|
47
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* 生成的文件所在目录。
|
|
60
|
+
* 默认值:./apis。
|
|
61
|
+
* */
|
|
48
62
|
outputFolder?: string;
|
|
49
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* 需要引用的 axios 函数地址。
|
|
65
|
+
* 默认值:window.axios。
|
|
66
|
+
* */
|
|
50
67
|
improtAxiosPath?: string;
|
|
51
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* 是否生成 ts 文件。
|
|
70
|
+
* 默认值:false。
|
|
71
|
+
* */
|
|
52
72
|
typeScript?: boolean;
|
|
53
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* url是否放置于 options 中。
|
|
75
|
+
* 默认值:true。
|
|
76
|
+
* 注:
|
|
77
|
+
* 1、如为 false,则将放在第一个参数中。
|
|
78
|
+
* */
|
|
54
79
|
urlInOptions?: boolean;
|
|
55
80
|
}
|
|
56
81
|
```
|
|
@@ -61,25 +86,21 @@ interface Config {
|
|
|
61
86
|
const swagger2axios = require('swagger-to-axios');
|
|
62
87
|
const swaggerDocumentList = ['name'].map((name) => ({
|
|
63
88
|
url: `http://127.0.0.1:14514/swagger?name=${name}.json`,
|
|
64
|
-
urlType: 'json',
|
|
65
89
|
name,
|
|
66
90
|
}));
|
|
67
|
-
swagger2axios(swaggerDocumentList,{
|
|
68
|
-
includeBaseURL:
|
|
69
|
-
cliType: 'Vite',
|
|
91
|
+
swagger2axios(swaggerDocumentList, {
|
|
92
|
+
includeBaseURL: false,
|
|
70
93
|
improtAxiosPath: '@/utils/request',
|
|
71
|
-
envProtocolName: 'VITE_APP_SCHEM',
|
|
72
94
|
https: true
|
|
73
95
|
})
|
|
74
96
|
|
|
75
97
|
/**以下是生成的文件内容*/
|
|
76
98
|
// user.js
|
|
77
99
|
// 用户相关接口
|
|
78
|
-
const basePath = '/api/v1';
|
|
79
|
-
const host = `${import.meta.env.VUE_APP_HOST ? import.meta.env.VUE_APP_HOST : '127.0.0.1:1919'}`;
|
|
80
|
-
const protocol = `${import.meta.env.VITE_APP_SCHEM ? import.meta.env.VITE_APP_SCHEM : 'https'}`;
|
|
81
100
|
import request from '@/utils/request';
|
|
82
101
|
|
|
102
|
+
const basePath = '/api/v1';
|
|
103
|
+
|
|
83
104
|
// 获取用户信息
|
|
84
105
|
export function getUserInfo(params, options) {
|
|
85
106
|
return request({
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
|
|
2
|
+
/**
|
|
3
|
+
* 合并 allOf
|
|
4
|
+
* @param object
|
|
5
|
+
*/
|
|
6
|
+
export declare const expandAllOf: (object: any) => void;
|
|
2
7
|
/** 获取 swagger 文档 */
|
|
3
8
|
export declare const getSwaggerJson: ({ url, }: {
|
|
4
9
|
url: string;
|
|
5
10
|
}) => Promise<OpenAPIV3.Document | OpenAPIV3_1.Document | undefined>;
|
|
6
11
|
/**
|
|
7
|
-
|
|
12
|
+
* 获取 swagger 文档列表
|
|
8
13
|
* @param {SwaggerDocument[]} swaggerList - swagger 文档列表
|
|
9
14
|
* @param {Config['cliType']} cliType - cli类型
|
|
10
15
|
* @returns {Promise<Folder[]>} 文件夹数组
|
|
@@ -12,12 +17,13 @@ export declare const getSwaggerJson: ({ url, }: {
|
|
|
12
17
|
export declare const getFolderList: (swaggerList: SwaggerDocument[], cliType: string) => Promise<Folder[]>;
|
|
13
18
|
/** 重组 tag 数组 */
|
|
14
19
|
export declare const getTagList: (tags?: OpenAPIV3.TagObject[], paths?: OpenAPIV3.PathsObject) => Tag[];
|
|
20
|
+
/** 将 openapi 类型转换为 TS 类型 */
|
|
21
|
+
export declare const openapiTypeToTypeScript: (schemaObject: OpenAPIV3.SchemaObject) => string;
|
|
15
22
|
/** 重组 response */
|
|
16
23
|
export declare const getResponse: (response?: OpenAPIV3.ResponseObject) => {
|
|
17
24
|
description: string;
|
|
18
25
|
data?: any;
|
|
19
26
|
} | undefined;
|
|
20
|
-
export declare const openapiTypeToTypeScript: (schemaObject: OpenAPIV3.SchemaObject) => string;
|
|
21
27
|
/**
|
|
22
28
|
* 写文件
|
|
23
29
|
* @param {string} pathname
|
package/lib/utils/index.js
CHANGED
|
@@ -3,6 +3,33 @@ import SwaggerParser from '@apidevtools/swagger-parser';
|
|
|
3
3
|
import { convertObj } from 'swagger2openapi';
|
|
4
4
|
import lodash from 'lodash';
|
|
5
5
|
const mergeWith = lodash.mergeWith;
|
|
6
|
+
/**
|
|
7
|
+
* 合并 allOf
|
|
8
|
+
* @param object
|
|
9
|
+
*/
|
|
10
|
+
export const expandAllOf = (object) => {
|
|
11
|
+
while (object.hasOwnProperty('allOf')) {
|
|
12
|
+
const allOf = object.allOf;
|
|
13
|
+
delete object.allOf;
|
|
14
|
+
const override = mergeWith({}, ...allOf, object, function (target, source) {
|
|
15
|
+
if (Array.isArray(target)) {
|
|
16
|
+
return target.concat(source).filter((v, i, a) => a.indexOf(v) === i);
|
|
17
|
+
}
|
|
18
|
+
if (typeof target === 'object') {
|
|
19
|
+
expandAllOf(target);
|
|
20
|
+
}
|
|
21
|
+
if (typeof source === 'object') {
|
|
22
|
+
expandAllOf(source);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
Object.assign(object, override);
|
|
26
|
+
}
|
|
27
|
+
Object.keys(object).forEach((key) => {
|
|
28
|
+
if (typeof object[key] === 'object') {
|
|
29
|
+
expandAllOf(object[key]);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
6
33
|
/** 获取 swagger 文档 */
|
|
7
34
|
export const getSwaggerJson = async ({ url, }) => {
|
|
8
35
|
try {
|
|
@@ -24,7 +51,7 @@ export const getSwaggerJson = async ({ url, }) => {
|
|
|
24
51
|
}
|
|
25
52
|
};
|
|
26
53
|
/**
|
|
27
|
-
|
|
54
|
+
* 获取 swagger 文档列表
|
|
28
55
|
* @param {SwaggerDocument[]} swaggerList - swagger 文档列表
|
|
29
56
|
* @param {Config['cliType']} cliType - cli类型
|
|
30
57
|
* @returns {Promise<Folder[]>} 文件夹数组
|
|
@@ -37,10 +64,10 @@ export const getFolderList = async (swaggerList, cliType) => {
|
|
|
37
64
|
if (json === undefined)
|
|
38
65
|
continue;
|
|
39
66
|
const { tags, paths, servers } = json;
|
|
40
|
-
const basePath = servers?.[0].url.replace(
|
|
67
|
+
const basePath = servers?.[0].url.replace(/^.+:\/\/((\d|\w)+\.{0,1})+(:\d+){0,1}/, '') ?? '';
|
|
41
68
|
// 三次替换,分别匹配 http://api.example.com/api , /api , //api.example.com,使结果保持为 api.example.com
|
|
42
69
|
const host = servers?.[0].url
|
|
43
|
-
.replace(
|
|
70
|
+
.replace(/^.+:\/\/(((\d|\w)+\.{0,1})+(:\d+){0,1}).*$/, '$1')
|
|
44
71
|
.replace(/^\/[\w|\d]+/, '')
|
|
45
72
|
.replace(/^\/\//, '') ?? '';
|
|
46
73
|
result.push({
|
|
@@ -99,18 +126,7 @@ export const getTagList = (tags = [], paths) => {
|
|
|
99
126
|
}
|
|
100
127
|
return tagList;
|
|
101
128
|
};
|
|
102
|
-
/**
|
|
103
|
-
export const getResponse = (response) => {
|
|
104
|
-
if (response === undefined)
|
|
105
|
-
return response;
|
|
106
|
-
const result = {
|
|
107
|
-
description: response.description,
|
|
108
|
-
data: {},
|
|
109
|
-
};
|
|
110
|
-
result.data = openapiTypeToTypeScript(response?.content?.['application/json']?.schema ?? {});
|
|
111
|
-
console.log('result.data', result.data);
|
|
112
|
-
return result;
|
|
113
|
-
};
|
|
129
|
+
/** 将 openapi 类型转换为 TS 类型 */
|
|
114
130
|
export const openapiTypeToTypeScript = (schemaObject) => {
|
|
115
131
|
const { type = '' } = schemaObject;
|
|
116
132
|
const numberEnum = [
|
|
@@ -177,32 +193,16 @@ export const openapiTypeToTypeScript = (schemaObject) => {
|
|
|
177
193
|
}
|
|
178
194
|
return 'any';
|
|
179
195
|
};
|
|
180
|
-
/**
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
return target.concat(source).filter((v, i, a) => a.indexOf(v) === i);
|
|
191
|
-
}
|
|
192
|
-
if (typeof target === 'object') {
|
|
193
|
-
expandAllOf(target);
|
|
194
|
-
}
|
|
195
|
-
if (typeof source === 'object') {
|
|
196
|
-
expandAllOf(source);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
Object.assign(object, override);
|
|
200
|
-
}
|
|
201
|
-
Object.keys(object).forEach((key) => {
|
|
202
|
-
if (typeof object[key] === 'object') {
|
|
203
|
-
expandAllOf(object[key]);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
196
|
+
/** 重组 response */
|
|
197
|
+
export const getResponse = (response) => {
|
|
198
|
+
if (response === undefined)
|
|
199
|
+
return response;
|
|
200
|
+
const result = {
|
|
201
|
+
description: response.description,
|
|
202
|
+
data: {},
|
|
203
|
+
};
|
|
204
|
+
result.data = openapiTypeToTypeScript(response?.content?.['application/json']?.schema ?? {});
|
|
205
|
+
return result;
|
|
206
206
|
};
|
|
207
207
|
/**
|
|
208
208
|
* 写文件
|