swagger-to-axios 1.0.3 → 1.0.5
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/lib/index.js +8 -8
- package/lib/utils/index.js +1 -1
- package/package.json +4 -3
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, urlInOptions = true, } = config;
|
|
12
|
+
const { includeBaseURL = true, cliType = 'VueCli', envHostName = cliType === 'VueCli' ? 'VUE_APP_HOST' : 'VITE_APP_HOST', envProtocolName = cliType === 'VueCli' ? 'VUE_APP_PROTOCOL' : 'VITE_APP_PROTOCOL', https = false, outputFolder = './apis', improtAxiosPath, typeScript = false, urlInOptions = true, } = config;
|
|
13
13
|
const swagger = {
|
|
14
14
|
path: outputFolder,
|
|
15
15
|
list: [],
|
|
@@ -103,27 +103,27 @@ const createApiFiles = async (swaggerList = [], config = {}) => {
|
|
|
103
103
|
// 如果包含注释,则在文件顶部添加注释
|
|
104
104
|
if (file.comment) {
|
|
105
105
|
fileContent += `// ${file.comment}
|
|
106
|
+
`;
|
|
107
|
+
}
|
|
108
|
+
if (improtAxiosPath) {
|
|
109
|
+
fileContent += `import request from '${improtAxiosPath}';
|
|
106
110
|
`;
|
|
107
111
|
}
|
|
108
112
|
fileContent += `const basePath = '${folder.baseURL}';
|
|
109
113
|
`;
|
|
110
114
|
if (includeBaseURL) {
|
|
115
|
+
const cliTypePrefix = folder.cliType === 'Vite' ? `import.meta.env.` : `process.env.`;
|
|
111
116
|
if (folder.host && folder.host.includes('127.0.0.1')) {
|
|
112
117
|
fileContent += `const host = '${folder.host}';
|
|
113
118
|
`;
|
|
114
119
|
}
|
|
115
120
|
else {
|
|
116
|
-
const cliTypePrefix = folder.cliType === 'Vite' ? `import.meta.env.` : `process.env.`;
|
|
117
121
|
const hostCliTypeString = `${cliTypePrefix}${envHostName}`;
|
|
118
122
|
fileContent += `const host = \`\${${hostCliTypeString} ? ${hostCliTypeString} : '${folder.host}'}\`;
|
|
119
|
-
`;
|
|
120
|
-
const protocolCliTypeString = `${cliTypePrefix}${envProtocolName}`;
|
|
121
|
-
fileContent += `const protocol = \`\${${protocolCliTypeString} ? ${protocolCliTypeString} : 'http${https ? 's' : ''}'}\`;
|
|
122
123
|
`;
|
|
123
124
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
fileContent += `import request from '${improtAxiosPath}';
|
|
125
|
+
const protocolCliTypeString = `${cliTypePrefix}${envProtocolName}`;
|
|
126
|
+
fileContent += `const protocol = \`\${${protocolCliTypeString} ? ${protocolCliTypeString} : 'http${https ? 's' : ''}'}\`;
|
|
127
127
|
`;
|
|
128
128
|
}
|
|
129
129
|
const apiList = file.list;
|
package/lib/utils/index.js
CHANGED
|
@@ -22,7 +22,7 @@ export const upperFirstCase = (str) => str.charAt(0).toUpperCase() + str.slice(1
|
|
|
22
22
|
/** 链接变成名称 */
|
|
23
23
|
export const urlToName = (str) => str
|
|
24
24
|
.split('/')
|
|
25
|
-
.reduce((accumulator, currentValue) => upperFirstCase(accumulator) + upperFirstCase(currentValue.replace(
|
|
25
|
+
.reduce((accumulator, currentValue) => upperFirstCase(accumulator) + upperFirstCase(currentValue.replace(/{/g, '').replace(/}/g, '')));
|
|
26
26
|
/**
|
|
27
27
|
* 链接变成带参链接(/record/{recordID}/{userID} GET 变成 /record/${params.recordID}/${params.userID})
|
|
28
28
|
* @param {string} url 链接名
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swagger-to-axios",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "将swagger转换成axios可用的api文件",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
15
15
|
"lint": "tslint -p tsconfig.json",
|
|
16
16
|
"pub": "npm version patch && npm publish",
|
|
17
|
-
"test": "node
|
|
17
|
+
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --config ./jest.config.js"
|
|
18
18
|
},
|
|
19
19
|
"author": "FreezeNow",
|
|
20
20
|
"keywords": [
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/js-yaml": "^4.0.3",
|
|
34
34
|
"@types/node": "^16.9.1",
|
|
35
|
-
"eslint-plugin-tsdoc": "
|
|
35
|
+
"eslint-plugin-tsdoc": "^0.2.17",
|
|
36
|
+
"jest": "^29.5.0",
|
|
36
37
|
"prettier": "^2.4.0",
|
|
37
38
|
"tslint": "^6.1.3",
|
|
38
39
|
"tslint-config-prettier": "^1.18.0"
|