swagger-to-axios 1.0.6 → 1.0.8

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
@@ -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
- /** 是否使用本地 swagger 文档,默认为 false。
14
- * 如果该项为 true,则 url 应填本地地址,建议填写完整路径 */
15
- localFile?: boolean;
16
- /** swagger 文档文件类型,yaml 还是 json ,默认为 yaml */
17
- urlType?: string;
18
- /** 生成文件后该文档的文件夹名称,默认会使用随机数作为文件夹名称 */
13
+ /**
14
+ * 生成文件后该文档的文件夹名称。
15
+ * 默认值:字符串随机数。
16
+ * */
19
17
  name?: string;
20
18
  }
21
19
 
22
20
  /** 生成文件配置项 */
23
- interface Config {
24
- /** 在生成文件时,每个函数是否携带 baseURL 属性,默认为 true。 */
21
+ declare interface Config {
22
+ /**
23
+ * 生成文件时,每个函数是否携带 baseURL 属性。
24
+ * 默认值:true。
25
+ * */
25
26
  includeBaseURL?: boolean;
26
27
  /**
27
- * 如果 includeBaseURL 为 false,则不需要配置该项。
28
- * cli类型,是 Vite 还是 VueCli ,默认 VueCli
28
+ * cli类型。
29
+ * 默认值:VueCli。
30
+ * 注:
31
+ * 1、如果 includeBaseURL 为 false,则不需要配置该项。
29
32
  */
30
- cliType?: string;
33
+ cliType?: 'Vite' | 'VueCli';
31
34
  /**
32
- * 如果 includeBaseURL 为 false,则不需要配置该项。
33
- * host 的配置名称,不填时会根据 cliType 属性自动设为 VUE_APP_HOST 或者 VITE_APP_HOST。
34
- * 如果 swagger 的 host 填写了正确的地址,你也可以完全不配置该项,生成的代码会使用三目运算符,并将非的表达式设置为 swagger 的 host。
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
- * 如果 includeBaseURL 为 false,则不需要配置该项。
39
- * 网络协议的配置名称,不填时会根据 cliType 属性自动设为 VUE_APP_PROTOCOL 或者 VITE_APP_PROTOCOL。
40
- * VUE_APP_PROTOCOL / VITE_APP_PROTOCOL 的值应该为 'https' 或者 'http'。
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
- * 如果 includeBaseURL 为 false,则不需要配置该项。
45
- * 是否使用 https,默认为 false。 */
52
+ * 是否使用 https。
53
+ * 默认值:false。
54
+ * 注:
55
+ * 1、如果 includeBaseURL 为 false,则不需要配置该项。
56
+ * */
46
57
  https?: boolean;
47
- /** 生成的文件所在目录,默认为 ./apis。 */
58
+ /**
59
+ * 生成的文件所在目录。
60
+ * 默认值:./apis。
61
+ * */
48
62
  outputFolder?: string;
49
- /** 需要引用的 axios 函数地址,默认为 window.axios。 */
63
+ /**
64
+ * 需要引用的 axios 函数地址。
65
+ * 默认值:window.axios。
66
+ * */
50
67
  improtAxiosPath?: string;
51
- /** 是否生成 ts 文件,默认为 false。 */
68
+ /**
69
+ * 是否生成 ts 文件。
70
+ * 默认值:false。
71
+ * */
52
72
  typeScript?: boolean;
53
- /** url是否放置于 options 中,默认为 true。如为 false,则将放在第一个参数中。 */
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: true,
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/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import "./types.js";
1
2
  /** 创建所有 API 文件
2
3
  * @param {SwaggerDocument[]} swaggerList - swagger 文档列表
3
4
  * @param {Config} config - 配置项
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import fs from 'fs';
2
2
  const fsPromises = fs.promises;
3
3
  import { writeFile, urlToName, urlToLinkParams, getFolderList, } from './utils/index.js';
4
+ import "./types.js";
4
5
  /** 创建所有 API 文件
5
6
  * @param {SwaggerDocument[]} swaggerList - swagger 文档列表
6
7
  * @param {Config} config - 配置项
@@ -3,18 +3,7 @@ declare interface SwaggerDocument {
3
3
  /** swagger 文档地址 */
4
4
  url: string;
5
5
  /**
6
- * 是否使用本地 swagger 文档。
7
- * 默认值:false。
8
- * 注:
9
- * 1、如果该项为 true,则 url 应填本地地址,建议填写完整路径。
10
- * */
11
- localFile?: boolean;
12
- /**
13
- * swagger 文档文件类型。
14
- * 默认值:yaml。
15
- * */
16
- urlType?: 'yaml' | 'json';
17
- /** 生成文件后该文档的文件夹名称。
6
+ * 生成文件后该文档的文件夹名称。
18
7
  * 默认值:字符串随机数。
19
8
  * */
20
9
  name?: string;
@@ -46,7 +35,8 @@ declare interface Config {
46
35
  * 默认值:VUE_APP_PROTOCOL | VITE_APP_PROTOCOL(根据 cliType 属性)。
47
36
  * 注:
48
37
  * 1、如果 includeBaseURL 为 false,则不需要配置该项。
49
- * 2、VUE_APP_PROTOCOL / VITE_APP_PROTOCOL 的值应该为 'https' 或者 'http'。
38
+ * 2、推荐只在生产环境和开发环境使用不同协议时配置该项。
39
+ * 3、VUE_APP_PROTOCOL / VITE_APP_PROTOCOL 的值应该为 'https' 或者 'http'。
50
40
  */
51
41
  envProtocolName?: string;
52
42
  /**
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";
@@ -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
@@ -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 {
@@ -20,11 +47,12 @@ export const getSwaggerJson = async ({ url, }) => {
20
47
  return swagger;
21
48
  }
22
49
  catch (error) {
50
+ console.error(`${url}异常:${error}`);
23
51
  return undefined;
24
52
  }
25
53
  };
26
54
  /**
27
-
55
+ * 获取 swagger 文档列表
28
56
  * @param {SwaggerDocument[]} swaggerList - swagger 文档列表
29
57
  * @param {Config['cliType']} cliType - cli类型
30
58
  * @returns {Promise<Folder[]>} 文件夹数组
@@ -33,14 +61,14 @@ export const getFolderList = async (swaggerList, cliType) => {
33
61
  const result = [];
34
62
  for (const element of swaggerList) {
35
63
  const { url, name = Math.random().toString() } = element;
36
- const json = await getSwaggerJson({ url });
64
+ const json = await getSwaggerJson({ url, });
37
65
  if (json === undefined)
38
66
  continue;
39
67
  const { tags, paths, servers } = json;
40
- const basePath = servers?.[0].url.replace(/.+:\/\/.+\//, '/') ?? '';
68
+ const basePath = servers?.[0].url.replace(/^.+:\/\/((\d|\w)+\.{0,1})+(:\d+){0,1}/, '') ?? '';
41
69
  // 三次替换,分别匹配 http://api.example.com/api , /api , //api.example.com,使结果保持为 api.example.com
42
70
  const host = servers?.[0].url
43
- .replace(/.+:\/\/(.+)\/.+/, '$1')
71
+ .replace(/^.+:\/\/(((\d|\w)+\.{0,1})+(:\d+){0,1}).*$/, '$1')
44
72
  .replace(/^\/[\w|\d]+/, '')
45
73
  .replace(/^\/\//, '') ?? '';
46
74
  result.push({
@@ -99,18 +127,7 @@ export const getTagList = (tags = [], paths) => {
99
127
  }
100
128
  return tagList;
101
129
  };
102
- /** 重组 response */
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
- };
130
+ /** openapi 类型转换为 TS 类型 */
114
131
  export const openapiTypeToTypeScript = (schemaObject) => {
115
132
  const { type = '' } = schemaObject;
116
133
  const numberEnum = [
@@ -177,32 +194,16 @@ export const openapiTypeToTypeScript = (schemaObject) => {
177
194
  }
178
195
  return 'any';
179
196
  };
180
- /**
181
- * 合并 allOf
182
- * @param object
183
- */
184
- const expandAllOf = (object) => {
185
- while (object.hasOwnProperty('allOf')) {
186
- const allOf = object.allOf;
187
- delete object.allOf;
188
- const override = mergeWith({}, ...allOf, object, function (target, source) {
189
- if (Array.isArray(target)) {
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
- });
197
+ /** 重组 response */
198
+ export const getResponse = (response) => {
199
+ if (response === undefined)
200
+ return response;
201
+ const result = {
202
+ description: response.description,
203
+ data: {},
204
+ };
205
+ result.data = openapiTypeToTypeScript(response?.content?.['application/json']?.schema ?? {});
206
+ return result;
206
207
  };
207
208
  /**
208
209
  * 写文件
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-to-axios",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "将swagger转换成axios可用的api文件",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",