swagger-to-axios 1.0.2 → 1.0.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 +12 -2
- package/lib/index.d.ts +2 -0
- package/lib/index.js +58 -42
- package/lib/typeing.d.ts +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
```TypeScript
|
|
9
9
|
/** swagger 文档配置项 */
|
|
10
10
|
interface SwaggerDocument {
|
|
11
|
-
/** swagger
|
|
11
|
+
/** swagger 文档地址 */
|
|
12
12
|
url: string;
|
|
13
13
|
/** 是否使用本地 swagger 文档,默认为 false。
|
|
14
14
|
* 如果该项为 true,则 url 应填本地地址,建议填写完整路径 */
|
|
@@ -18,6 +18,7 @@ interface SwaggerDocument {
|
|
|
18
18
|
/** 生成文件后该文档的文件夹名称,默认会使用随机数作为文件夹名称 */
|
|
19
19
|
name?: string;
|
|
20
20
|
}
|
|
21
|
+
|
|
21
22
|
/** 生成文件配置项 */
|
|
22
23
|
interface Config {
|
|
23
24
|
/** 在生成文件时,每个函数是否携带 baseURL 属性,默认为 true。 */
|
|
@@ -49,6 +50,8 @@ interface Config {
|
|
|
49
50
|
improtAxiosPath?: string;
|
|
50
51
|
/** 是否生成 ts 文件,默认为 false。 */
|
|
51
52
|
typeScript?: boolean;
|
|
53
|
+
/** url是否放置于 options 中,默认为 true。如为 false,则将放在第一个参数中。 */
|
|
54
|
+
urlInOptions?: boolean;
|
|
52
55
|
}
|
|
53
56
|
```
|
|
54
57
|
|
|
@@ -57,7 +60,7 @@ interface Config {
|
|
|
57
60
|
```JavaScript
|
|
58
61
|
const swagger2axios = require('swagger-to-axios');
|
|
59
62
|
const swaggerDocumentList = ['name'].map((name) => ({
|
|
60
|
-
url: `http://127.0.0.1:
|
|
63
|
+
url: `http://127.0.0.1:14514/swagger?name=${name}.json`,
|
|
61
64
|
urlType: 'json',
|
|
62
65
|
name,
|
|
63
66
|
}));
|
|
@@ -88,3 +91,10 @@ export function getUserInfo(params, options) {
|
|
|
88
91
|
});
|
|
89
92
|
}
|
|
90
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
|
@@ -41,6 +41,8 @@ interface Config {
|
|
|
41
41
|
improtAxiosPath?: string;
|
|
42
42
|
/** 是否生成 ts 文件,默认为 false。 */
|
|
43
43
|
typeScript?: boolean;
|
|
44
|
+
/** url是否放置于 options 中,默认为 true。如为 false,则将放在第一个参数中。 */
|
|
45
|
+
urlInOptions?: boolean;
|
|
44
46
|
}
|
|
45
47
|
/** 创建所有 API 文件
|
|
46
48
|
* @param {SwaggerDocument[]} swaggerList - swagger 文档列表
|
package/lib/index.js
CHANGED
|
@@ -9,7 +9,7 @@ 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: [],
|
|
@@ -64,34 +64,29 @@ const createApiFiles = async (swaggerList = [], config = {}) => {
|
|
|
64
64
|
for (const name in methods) {
|
|
65
65
|
if (methods.hasOwnProperty(name)) {
|
|
66
66
|
const method = methods[name];
|
|
67
|
-
|
|
68
|
-
if (tag) {
|
|
69
|
-
|
|
70
|
-
if (api) {
|
|
71
|
-
api.method.push(name);
|
|
72
|
-
api.comment.push(method.summary);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
tag.list.push({
|
|
76
|
-
url: path,
|
|
77
|
-
method: [name],
|
|
78
|
-
comment: [method.summary],
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
folderObj.list.push({
|
|
67
|
+
let tag = folderObj.list.find((ele) => ele.name === method.tags[0]);
|
|
68
|
+
if (!tag) {
|
|
69
|
+
tag = {
|
|
84
70
|
name: method.tags[0],
|
|
85
71
|
comment: '',
|
|
86
|
-
list: [
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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);
|
|
94
84
|
}
|
|
85
|
+
api.method.push(name);
|
|
86
|
+
api.comment.push({
|
|
87
|
+
summary: method.summary,
|
|
88
|
+
description: method.description,
|
|
89
|
+
});
|
|
95
90
|
}
|
|
96
91
|
}
|
|
97
92
|
}
|
|
@@ -108,44 +103,65 @@ const createApiFiles = async (swaggerList = [], config = {}) => {
|
|
|
108
103
|
// 如果包含注释,则在文件顶部添加注释
|
|
109
104
|
if (file.comment) {
|
|
110
105
|
fileContent += `// ${file.comment}
|
|
106
|
+
`;
|
|
107
|
+
}
|
|
108
|
+
if (improtAxiosPath) {
|
|
109
|
+
fileContent += `import request from '${improtAxiosPath}';
|
|
111
110
|
`;
|
|
112
111
|
}
|
|
113
112
|
fileContent += `const basePath = '${folder.baseURL}';
|
|
114
113
|
`;
|
|
115
114
|
if (includeBaseURL) {
|
|
115
|
+
const cliTypePrefix = folder.cliType === 'Vite' ? `import.meta.env.` : `process.env.`;
|
|
116
116
|
if (folder.host && folder.host.includes('127.0.0.1')) {
|
|
117
117
|
fileContent += `const host = '${folder.host}';
|
|
118
118
|
`;
|
|
119
119
|
}
|
|
120
120
|
else {
|
|
121
|
-
const cliTypePrefix = folder.cliType === 'Vite' ? `import.meta.env.` : `process.env.`;
|
|
122
121
|
const hostCliTypeString = `${cliTypePrefix}${envHostName}`;
|
|
123
122
|
fileContent += `const host = \`\${${hostCliTypeString} ? ${hostCliTypeString} : '${folder.host}'}\`;
|
|
124
|
-
`;
|
|
125
|
-
const protocolCliTypeString = `${cliTypePrefix}${envProtocolName}`;
|
|
126
|
-
fileContent += `const protocol = \`\${${protocolCliTypeString} ? ${protocolCliTypeString} : 'http${https ? 's' : ''}'}\`;
|
|
127
123
|
`;
|
|
128
124
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
fileContent += `import request from '${improtAxiosPath}';
|
|
125
|
+
const protocolCliTypeString = `${cliTypePrefix}${envProtocolName}`;
|
|
126
|
+
fileContent += `const protocol = \`\${${protocolCliTypeString} ? ${protocolCliTypeString} : 'http${https ? 's' : ''}'}\`;
|
|
132
127
|
`;
|
|
133
128
|
}
|
|
134
129
|
const apiList = file.list;
|
|
135
130
|
for (const api of apiList) {
|
|
136
131
|
for (let l = 0; l < api.method.length; l++) {
|
|
137
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
|
+
}
|
|
138
142
|
fileContent += `
|
|
139
|
-
|
|
140
|
-
export function ${method.toLowerCase() + urlToName(api.url)}(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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}',
|
|
147
163
|
${method.toLowerCase() === 'get' ? 'params' : 'data'},
|
|
148
|
-
...options,
|
|
164
|
+
...(options || {}),
|
|
149
165
|
});
|
|
150
166
|
}
|
|
151
167
|
`;
|
package/lib/typeing.d.ts
CHANGED