swagger-to-axios 1.0.2 → 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 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:114514/swagger?name=${name}.json`,
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
- const tag = folderObj.list.find((ele) => ele.name === method.tags[0]);
68
- if (tag) {
69
- const api = tag.list.find((ele) => ele.url === path);
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
- url: path,
89
- method: [name],
90
- comment: [method.summary],
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
  }
@@ -135,17 +130,38 @@ const createApiFiles = async (swaggerList = [], config = {}) => {
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
- // ${api.comment[l]}
140
- export function ${method.toLowerCase() + urlToName(api.url)}(${method.toUpperCase() === 'GET' ? 'params' : 'data'}${typeScript ? '?: any' : ''}, options${typeScript ? '?: { [key: string]: any }' : ''}) {
141
- return ${improtAxiosPath ? 'request' : 'window.axios'}({
142
- url: \`\${basePath}${urlToLinkParams(api.url, method)}\`,${includeBaseURL !== false
143
- ? `
144
- baseURL: \`\${protocol}://\${host}\`,`
145
- : ''}
146
- method: '${method}',
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
@@ -1,7 +1,10 @@
1
1
  declare interface Api {
2
2
  url: string;
3
3
  method: string[];
4
- comment: string[];
4
+ comment: {
5
+ summary: string;
6
+ description: string;
7
+ }[];
5
8
  }
6
9
  declare interface Tag {
7
10
  name: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-to-axios",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "将swagger转换成axios可用的api文件",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",