swagger-to-axios 1.0.1 → 1.0.3
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 +32 -10
- package/lib/index.d.ts +7 -2
- package/lib/index.js +73 -46
- package/lib/typeing.d.ts +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,19 @@
|
|
|
6
6
|
这个函数的第一个参数(必填)为需要访问的文件名(文件名请访问内部的 api 接口文档地址网页);第二个参数(选填)是可选配置项,配置内容如下。
|
|
7
7
|
|
|
8
8
|
```TypeScript
|
|
9
|
+
/** swagger 文档配置项 */
|
|
10
|
+
interface SwaggerDocument {
|
|
11
|
+
/** swagger 文档地址 */
|
|
12
|
+
url: string;
|
|
13
|
+
/** 是否使用本地 swagger 文档,默认为 false。
|
|
14
|
+
* 如果该项为 true,则 url 应填本地地址,建议填写完整路径 */
|
|
15
|
+
localFile?: boolean;
|
|
16
|
+
/** swagger 文档文件类型,yaml 还是 json ,默认为 yaml */
|
|
17
|
+
urlType?: string;
|
|
18
|
+
/** 生成文件后该文档的文件夹名称,默认会使用随机数作为文件夹名称 */
|
|
19
|
+
name?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
9
22
|
/** 生成文件配置项 */
|
|
10
23
|
interface Config {
|
|
11
24
|
/** 在生成文件时,每个函数是否携带 baseURL 属性,默认为 true。 */
|
|
@@ -35,22 +48,23 @@ interface Config {
|
|
|
35
48
|
outputFolder?: string;
|
|
36
49
|
/** 需要引用的 axios 函数地址,默认为 window.axios。 */
|
|
37
50
|
improtAxiosPath?: string;
|
|
38
|
-
|
|
39
51
|
/** 是否生成 ts 文件,默认为 false。 */
|
|
40
52
|
typeScript?: boolean;
|
|
53
|
+
/** url是否放置于 options 中,默认为 true。如为 false,则将放在第一个参数中。 */
|
|
54
|
+
urlInOptions?: boolean;
|
|
41
55
|
}
|
|
42
56
|
```
|
|
43
57
|
|
|
44
|
-
|
|
58
|
+
## 参考用例
|
|
45
59
|
|
|
46
60
|
```JavaScript
|
|
47
61
|
const swagger2axios = require('swagger-to-axios');
|
|
48
|
-
const
|
|
49
|
-
url: `http://127.0.0.1:
|
|
62
|
+
const swaggerDocumentList = ['name'].map((name) => ({
|
|
63
|
+
url: `http://127.0.0.1:14514/swagger?name=${name}.json`,
|
|
50
64
|
urlType: 'json',
|
|
51
65
|
name,
|
|
52
66
|
}));
|
|
53
|
-
swagger2axios(
|
|
67
|
+
swagger2axios(swaggerDocumentList,{
|
|
54
68
|
includeBaseURL: true,
|
|
55
69
|
cliType: 'Vite',
|
|
56
70
|
improtAxiosPath: '@/utils/request',
|
|
@@ -59,16 +73,17 @@ swagger2axios(folderNameList,{
|
|
|
59
73
|
})
|
|
60
74
|
|
|
61
75
|
/**以下是生成的文件内容*/
|
|
62
|
-
//
|
|
63
|
-
|
|
76
|
+
// user.js
|
|
77
|
+
// 用户相关接口
|
|
78
|
+
const basePath = '/api/v1';
|
|
64
79
|
const host = `${import.meta.env.VUE_APP_HOST ? import.meta.env.VUE_APP_HOST : '127.0.0.1:1919'}`;
|
|
65
80
|
const protocol = `${import.meta.env.VITE_APP_SCHEM ? import.meta.env.VITE_APP_SCHEM : 'https'}`;
|
|
66
81
|
import request from '@/utils/request';
|
|
67
82
|
|
|
68
|
-
//
|
|
69
|
-
export function
|
|
83
|
+
// 获取用户信息
|
|
84
|
+
export function getUserInfo(params, options) {
|
|
70
85
|
return request({
|
|
71
|
-
url: `${basePath}/
|
|
86
|
+
url: `${basePath}/user/info`,
|
|
72
87
|
baseURL: `${protocol}://${host}`,
|
|
73
88
|
method: 'get',
|
|
74
89
|
params,
|
|
@@ -76,3 +91,10 @@ export function getHumHumAhAhAhAh(params, options) {
|
|
|
76
91
|
});
|
|
77
92
|
}
|
|
78
93
|
```
|
|
94
|
+
|
|
95
|
+
## 注意事项
|
|
96
|
+
如果想使用 umi-request,需要写一个文件转换一下,之后将 improtAxiosPath 配置项指向该文件的地址就好了。你要问为什么需要转换一下,因为我懒得多想一个配置项的名称。下面是实例代码:
|
|
97
|
+
```JavaScript
|
|
98
|
+
import { request } from '@umijs/max';
|
|
99
|
+
export default request;
|
|
100
|
+
```
|
package/lib/index.d.ts
CHANGED
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
interface SwaggerDocument {
|
|
3
3
|
/** swagger 文档地址 */
|
|
4
4
|
url: string;
|
|
5
|
-
/** swagger
|
|
5
|
+
/** 是否使用本地 swagger 文档,默认为 false。
|
|
6
|
+
* 如果该项为 true,则 url 应填本地地址,建议填写完整路径 */
|
|
7
|
+
localFile?: boolean;
|
|
8
|
+
/** swagger 文档文件类型,yaml 还是 json ,默认为 yaml */
|
|
6
9
|
urlType?: string;
|
|
7
|
-
/**
|
|
10
|
+
/** 生成文件后该文档的文件夹名称,默认会使用随机数作为文件夹名称 */
|
|
8
11
|
name?: string;
|
|
9
12
|
}
|
|
10
13
|
/** 生成文件配置项 */
|
|
@@ -38,6 +41,8 @@ interface Config {
|
|
|
38
41
|
improtAxiosPath?: string;
|
|
39
42
|
/** 是否生成 ts 文件,默认为 false。 */
|
|
40
43
|
typeScript?: boolean;
|
|
44
|
+
/** url是否放置于 options 中,默认为 true。如为 false,则将放在第一个参数中。 */
|
|
45
|
+
urlInOptions?: boolean;
|
|
41
46
|
}
|
|
42
47
|
/** 创建所有 API 文件
|
|
43
48
|
* @param {SwaggerDocument[]} swaggerList - swagger 文档列表
|
package/lib/index.js
CHANGED
|
@@ -9,25 +9,36 @@ import { writeFile, urlToName, urlToLinkParams } from './utils/index.js';
|
|
|
9
9
|
*/
|
|
10
10
|
const createApiFiles = async (swaggerList = [], config = {}) => {
|
|
11
11
|
try {
|
|
12
|
-
const { includeBaseURL = true, cliType = 'VueCli', envHostName = 'VUE_APP_HOST', envProtocolName = 'VUE_APP_PROTOCOL', https = false, outputFolder = './apis', improtAxiosPath, typeScript = false, } = config;
|
|
12
|
+
const { includeBaseURL = true, cliType = 'VueCli', envHostName = 'VUE_APP_HOST', envProtocolName = 'VUE_APP_PROTOCOL', https = false, outputFolder = './apis', improtAxiosPath, typeScript = false, urlInOptions = true, } = config;
|
|
13
13
|
const swagger = {
|
|
14
14
|
path: outputFolder,
|
|
15
15
|
list: [],
|
|
16
16
|
};
|
|
17
17
|
// 循环 swagger 文档列表
|
|
18
18
|
for (const element of swaggerList) {
|
|
19
|
-
const { url, urlType = 'yaml', name = Math.random().toString() } = element;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
const { url, localFile = false, urlType = 'yaml', name = Math.random().toString() } = element;
|
|
20
|
+
let json;
|
|
21
|
+
if (localFile) {
|
|
22
|
+
const fileString = await fsPromises.readFile(url, { encoding: 'utf8' }).catch((error) => console.error(error));
|
|
23
|
+
if (!fileString) {
|
|
24
|
+
console.log(`${url}下的文件没有内容,已跳过`);
|
|
25
|
+
continue;
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
json = urlType === 'json' ? fileString : YAML.load(fileString);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// 获取文档并转换成 json
|
|
31
|
+
json = await axios
|
|
32
|
+
.get(url)
|
|
33
|
+
.then((res) => {
|
|
34
|
+
if (urlType && urlType === 'json') {
|
|
35
|
+
return res.data;
|
|
36
|
+
}
|
|
37
|
+
const jsonData = YAML.load(res.data);
|
|
38
|
+
return jsonData;
|
|
39
|
+
})
|
|
40
|
+
.catch((error) => console.error(error));
|
|
41
|
+
}
|
|
31
42
|
// 创建文件夹对象
|
|
32
43
|
const folderObj = {
|
|
33
44
|
name,
|
|
@@ -53,34 +64,29 @@ const createApiFiles = async (swaggerList = [], config = {}) => {
|
|
|
53
64
|
for (const name in methods) {
|
|
54
65
|
if (methods.hasOwnProperty(name)) {
|
|
55
66
|
const method = methods[name];
|
|
56
|
-
|
|
57
|
-
if (tag) {
|
|
58
|
-
|
|
59
|
-
if (api) {
|
|
60
|
-
api.method.push(name);
|
|
61
|
-
api.comment.push(method.summary);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
tag.list.push({
|
|
65
|
-
url: path,
|
|
66
|
-
method: [name],
|
|
67
|
-
comment: [method.summary],
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
folderObj.list.push({
|
|
67
|
+
let tag = folderObj.list.find((ele) => ele.name === method.tags[0]);
|
|
68
|
+
if (!tag) {
|
|
69
|
+
tag = {
|
|
73
70
|
name: method.tags[0],
|
|
74
71
|
comment: '',
|
|
75
|
-
list: [
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
list: [],
|
|
73
|
+
};
|
|
74
|
+
folderObj.list.push(tag);
|
|
75
|
+
}
|
|
76
|
+
let api = tag.list.find((ele) => ele.url === path);
|
|
77
|
+
if (!api) {
|
|
78
|
+
api = {
|
|
79
|
+
url: path,
|
|
80
|
+
method: [],
|
|
81
|
+
comment: [],
|
|
82
|
+
};
|
|
83
|
+
tag.list.push(api);
|
|
83
84
|
}
|
|
85
|
+
api.method.push(name);
|
|
86
|
+
api.comment.push({
|
|
87
|
+
summary: method.summary,
|
|
88
|
+
description: method.description,
|
|
89
|
+
});
|
|
84
90
|
}
|
|
85
91
|
}
|
|
86
92
|
}
|
|
@@ -124,17 +130,38 @@ const createApiFiles = async (swaggerList = [], config = {}) => {
|
|
|
124
130
|
for (const api of apiList) {
|
|
125
131
|
for (let l = 0; l < api.method.length; l++) {
|
|
126
132
|
const method = api.method[l];
|
|
133
|
+
const { summary, description } = api.comment[l];
|
|
134
|
+
if (summary) {
|
|
135
|
+
fileContent += `
|
|
136
|
+
// ${summary}`;
|
|
137
|
+
}
|
|
138
|
+
if (description) {
|
|
139
|
+
fileContent += `
|
|
140
|
+
// ${description}`;
|
|
141
|
+
}
|
|
127
142
|
fileContent += `
|
|
128
|
-
|
|
129
|
-
export function ${method.toLowerCase() + urlToName(api.url)}(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
143
|
+
`;
|
|
144
|
+
fileContent += `export function ${method.toLowerCase() + urlToName(api.url)}(`;
|
|
145
|
+
fileContent += method.toUpperCase() === 'GET' ? 'params' : 'data';
|
|
146
|
+
fileContent += `${typeScript ? '?: any' : ''}, options${typeScript ? '?: { [key: string]: any }' : ''}) {
|
|
147
|
+
`;
|
|
148
|
+
fileContent += `return ${improtAxiosPath ? `request${typeScript ? '<any>' : ''}` : 'window.axios'}(`;
|
|
149
|
+
if (!urlInOptions) {
|
|
150
|
+
fileContent += `\`\${basePath}${urlToLinkParams(api.url, method)}\`, `;
|
|
151
|
+
}
|
|
152
|
+
fileContent += `{
|
|
153
|
+
`;
|
|
154
|
+
if (urlInOptions) {
|
|
155
|
+
fileContent += `url: \`\${basePath}${urlToLinkParams(api.url, method)}\`,
|
|
156
|
+
`;
|
|
157
|
+
}
|
|
158
|
+
if (includeBaseURL) {
|
|
159
|
+
fileContent += `baseURL: \`\${protocol}://\${host}\`,
|
|
160
|
+
`;
|
|
161
|
+
}
|
|
162
|
+
fileContent += `method: '${method}',
|
|
136
163
|
${method.toLowerCase() === 'get' ? 'params' : 'data'},
|
|
137
|
-
...options,
|
|
164
|
+
...(options || {}),
|
|
138
165
|
});
|
|
139
166
|
}
|
|
140
167
|
`;
|
package/lib/typeing.d.ts
CHANGED