swagger-to-axios 1.0.1 → 1.0.2

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
@@ -6,6 +6,18 @@
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
+ }
9
21
  /** 生成文件配置项 */
10
22
  interface Config {
11
23
  /** 在生成文件时,每个函数是否携带 baseURL 属性,默认为 true。 */
@@ -35,22 +47,21 @@ interface Config {
35
47
  outputFolder?: string;
36
48
  /** 需要引用的 axios 函数地址,默认为 window.axios。 */
37
49
  improtAxiosPath?: string;
38
-
39
50
  /** 是否生成 ts 文件,默认为 false。 */
40
51
  typeScript?: boolean;
41
52
  }
42
53
  ```
43
54
 
44
- 以下是参考用例
55
+ ## 参考用例
45
56
 
46
57
  ```JavaScript
47
58
  const swagger2axios = require('swagger-to-axios');
48
- const folderNameList = ['name'].map((name) => ({
59
+ const swaggerDocumentList = ['name'].map((name) => ({
49
60
  url: `http://127.0.0.1:114514/swagger?name=${name}.json`,
50
61
  urlType: 'json',
51
62
  name,
52
63
  }));
53
- swagger2axios(folderNameList,{
64
+ swagger2axios(swaggerDocumentList,{
54
65
  includeBaseURL: true,
55
66
  cliType: 'Vite',
56
67
  improtAxiosPath: '@/utils/request',
@@ -59,16 +70,17 @@ swagger2axios(folderNameList,{
59
70
  })
60
71
 
61
72
  /**以下是生成的文件内容*/
62
- // 仲夏夜之淫梦相关接口
63
- const basePath = '/midsummer/night/lewd/dream';
73
+ // user.js
74
+ // 用户相关接口
75
+ const basePath = '/api/v1';
64
76
  const host = `${import.meta.env.VUE_APP_HOST ? import.meta.env.VUE_APP_HOST : '127.0.0.1:1919'}`;
65
77
  const protocol = `${import.meta.env.VITE_APP_SCHEM ? import.meta.env.VITE_APP_SCHEM : 'https'}`;
66
78
  import request from '@/utils/request';
67
79
 
68
- // 获取怪叫声
69
- export function getHumHumAhAhAhAh(params, options) {
80
+ // 获取用户信息
81
+ export function getUserInfo(params, options) {
70
82
  return request({
71
- url: `${basePath}/hum/hum/ah/ah/ah/ah`,
83
+ url: `${basePath}/user/info`,
72
84
  baseURL: `${protocol}://${host}`,
73
85
  method: 'get',
74
86
  params,
package/lib/index.d.ts CHANGED
@@ -2,9 +2,12 @@
2
2
  interface SwaggerDocument {
3
3
  /** swagger 文档地址 */
4
4
  url: string;
5
- /** swagger 文档文件类型, yaml 还是 json ,默认为 yaml */
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
  /** 生成文件配置项 */
package/lib/index.js CHANGED
@@ -16,18 +16,29 @@ const createApiFiles = async (swaggerList = [], config = {}) => {
16
16
  };
17
17
  // 循环 swagger 文档列表
18
18
  for (const element of swaggerList) {
19
- const { url, urlType = 'yaml', name = Math.random().toString() } = element;
20
- // 获取文档并转换成 json
21
- const json = await axios
22
- .get(url)
23
- .then((res) => {
24
- if (urlType && urlType === 'json') {
25
- return res.data;
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
- const jsonData = YAML.load(res.data);
28
- return jsonData;
29
- })
30
- .catch((error) => console.error(error));
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-to-axios",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "将swagger转换成axios可用的api文件",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",