♻️ Refactor

This commit is contained in:
Liyas Thomas
2020-01-30 23:45:38 +05:30
parent 6c7643a4c3
commit f80c5d6a46

View File

@@ -1822,12 +1822,10 @@ export default {
}
const protocol = "^(https?:\\/\\/)?";
const validIP = new RegExp(
protocol +
"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
`${protocol}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`
);
const validHostname = new RegExp(
protocol +
"(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9/])$"
`${protocol}(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9/])$`
);
return validIP.test(this.url) || validHostname.test(this.url);
},
@@ -1885,39 +1883,20 @@ export default {
if (this.requestType === "JavaScript XHR") {
const requestString = [];
requestString.push("const xhr = new XMLHttpRequest()");
const user =
this.auth === "Basic Auth" ? "'" + this.httpUser + "'" : null;
const user = this.auth === "Basic Auth" ? `'${this.httpUser}'` : null;
const pswd =
this.auth === "Basic Auth" ? "'" + this.httpPassword + "'" : null;
this.auth === "Basic Auth" ? `'${this.httpPassword}'` : null;
requestString.push(
"xhr.open('" +
this.method +
"', '" +
this.url +
this.pathName +
this.queryString +
"', true, " +
user +
", " +
pswd +
")"
`xhr.open('${this.method}', '${this.url}${this.pathName}${this.queryString}', true, ${user}, ${pswd})`
);
if (this.auth === "Bearer Token" || this.auth === "OAuth 2.0") {
requestString.push(
"xhr.setRequestHeader('Authorization', 'Bearer " +
this.bearerToken +
"')"
`xhr.setRequestHeader('Authorization', 'Bearer ${this.bearerToken}')`
);
}
if (this.headers) {
this.headers.forEach(element => {
requestString.push(
"xhr.setRequestHeader('" +
element.key +
"', '" +
element.value +
"')"
);
this.headers.forEach(({ key, value }) => {
requestString.push(`xhr.setRequestHeader('${key}', '${value}')`);
});
}
if (["POST", "PUT", "PATCH"].includes(this.method)) {
@@ -1925,14 +1904,12 @@ export default {
? this.rawParams
: this.rawRequestBody;
requestString.push(
"xhr.setRequestHeader('Content-Length', " + requestBody.length + ")"
`xhr.setRequestHeader('Content-Length', ${requestBody.length})`
);
requestString.push(
"xhr.setRequestHeader('Content-Type', '" +
this.contentType +
"; charset=utf-8')"
`xhr.setRequestHeader('Content-Type', '${this.contentType}; charset=utf-8')`
);
requestString.push("xhr.send(" + requestBody + ")");
requestString.push(`xhr.send(${requestBody})`);
} else {
requestString.push("xhr.send()");
}
@@ -1941,40 +1918,36 @@ export default {
const requestString = [];
let headers = [];
requestString.push(
'fetch("' + this.url + this.pathName + this.queryString + '", {\n'
`fetch("${this.url}${this.pathName}${this.queryString}", {\n`
);
requestString.push(' method: "' + this.method + '",\n');
requestString.push(` method: "${this.method}",\n`);
if (this.auth === "Basic Auth") {
const basic = this.httpUser + ":" + this.httpPassword;
const basic = `${this.httpUser}:${this.httpPassword}`;
headers.push(
' "Authorization": "Basic ' +
window.btoa(unescape(encodeURIComponent(basic))) +
'",\n'
` "Authorization": "Basic ${window.btoa(
unescape(encodeURIComponent(basic))
)}",\n`
);
} else if (this.auth === "Bearer Token" || this.auth === "OAuth 2.0") {
headers.push(
' "Authorization": "Bearer ' + this.bearerToken + '",\n'
);
headers.push(` "Authorization": "Bearer ${this.bearerToken}",\n`);
}
if (["POST", "PUT", "PATCH"].includes(this.method)) {
const requestBody = this.rawInput
? this.rawParams
: this.rawRequestBody;
requestString.push(" body: " + requestBody + ",\n");
headers.push(' "Content-Length": ' + requestBody.length + ",\n");
requestString.push(` body: ${requestBody},\n`);
headers.push(` "Content-Length": ${requestBody.length},\n`);
headers.push(
' "Content-Type": "' + this.contentType + '; charset=utf-8",\n'
` "Content-Type": "${this.contentType}; charset=utf-8",\n`
);
}
if (this.headers) {
this.headers.forEach(element => {
headers.push(
' "' + element.key + '": "' + element.value + '",\n'
);
this.headers.forEach(({ key, value }) => {
headers.push(` "${key}": "${value}",\n`);
});
}
headers = headers.join("").slice(0, -2);
requestString.push(" headers: {\n" + headers + "\n },\n");
requestString.push(` headers: {\n${headers}\n },\n`);
requestString.push(' credentials: "same-origin"\n');
requestString.push("}).then(function(response) {\n");
requestString.push(" response.status\n");
@@ -1988,40 +1961,36 @@ export default {
return requestString.join("");
} else if (this.requestType === "cURL") {
const requestString = [];
requestString.push("curl -X " + this.method + " \n");
requestString.push(`curl -X ${this.method} \n`);
requestString.push(
" '" + this.url + this.pathName + this.queryString + "' \n"
` '${this.url}${this.pathName}${this.queryString}' \n`
);
if (this.auth === "Basic Auth") {
const basic = this.httpUser + ":" + this.httpPassword;
const basic = `${this.httpUser}:${this.httpPassword}`;
requestString.push(
" -H 'Authorization: Basic " +
window.btoa(unescape(encodeURIComponent(basic))) +
"' \n"
` -H 'Authorization: Basic ${window.btoa(
unescape(encodeURIComponent(basic))
)}' \n`
);
} else if (this.auth === "Bearer Token" || this.auth === "OAuth 2.0") {
requestString.push(
" -H 'Authorization: Bearer " + this.bearerToken + "' \n"
` -H 'Authorization: Bearer ${this.bearerToken}' \n`
);
}
if (this.headers) {
this.headers.forEach(element => {
requestString.push(
" -H '" + element.key + ": " + element.value + "' \n"
);
this.headers.forEach(({ key, value }) => {
requestString.push(` -H '${key}: ${value}' \n`);
});
}
if (["POST", "PUT", "PATCH"].includes(this.method)) {
const requestBody = this.rawInput
? this.rawParams
: this.rawRequestBody;
requestString.push(` -H 'Content-Length: ${requestBody.length}' \n`);
requestString.push(
" -H 'Content-Length: " + requestBody.length + "' \n"
` -H 'Content-Type: ${this.contentType}; charset=utf-8' \n`
);
requestString.push(
" -H 'Content-Type: " + this.contentType + "; charset=utf-8' \n"
);
requestString.push(" -d '" + requestBody + "' \n");
requestString.push(` -d '${requestBody}' \n`);
}
return requestString.join("").slice(0, -2);
}
@@ -2283,8 +2252,8 @@ export default {
}
},
getQueryStringFromPath() {
let queryString,
pathParsed = url.parse(this.path);
let queryString;
let pathParsed = url.parse(this.path);
return (queryString = pathParsed.query ? pathParsed.query : "");
},
queryStringToArray(queryString) {
@@ -2295,9 +2264,8 @@ export default {
}));
},
pathInputHandler() {
let queryString = this.getQueryStringFromPath(),
params = this.queryStringToArray(queryString);
let queryString = this.getQueryStringFromPath();
let params = this.queryStringToArray(queryString);
this.paramsWatchEnabled = false;
this.params = params;
},
@@ -2418,7 +2386,7 @@ export default {
const aux = document.createElement("textarea");
const copy =
this.responseType === "application/json"
? JSON.stringify(this.response.body)
? JSON.stringify(this.response.body, null, 2)
: this.response.body;
aux.innerText = copy;
document.body.appendChild(aux);
@@ -2433,17 +2401,12 @@ export default {
downloadResponse() {
const dataToWrite = JSON.stringify(this.response.body, null, 2);
const file = new Blob([dataToWrite], { type: this.responseType });
const a = document.createElement("a"),
url = URL.createObjectURL(file);
const a = document.createElement("a");
const url = URL.createObjectURL(file);
a.href = url;
a.download = (
this.url +
this.path +
" [" +
this.method +
"] on " +
Date()
).replace(/\./g, "[dot]");
a.download = `${this.url + this.path} [${
this.method
}] on ${Date()}`.replace(/\./g, "[dot]");
document.body.appendChild(a);
a.click();
this.$refs.downloadResponse.innerHTML = this.doneButton;
@@ -2511,13 +2474,12 @@ export default {
history.replaceState(
window.location.href,
"",
"/?" +
encodeURI(
flats
.concat(deeps, bodyParams)
.join("")
.slice(0, -1)
)
`/?${encodeURI(
flats
.concat(deeps, bodyParams)
.join("")
.slice(0, -1)
)}`
);
},
setRouteQueries(queries) {
@@ -2714,8 +2676,8 @@ export default {
let file = this.$refs.payload.files[0];
if (file !== undefined && file !== null) {
let reader = new FileReader();
reader.onload = e => {
this.rawParams = e.target.result;
reader.onload = ({ target }) => {
this.rawParams = target.result;
};
reader.readAsText(file);
this.$toast.info(this.$t("file_imported"), {
@@ -2807,7 +2769,7 @@ export default {
removeOAuthTokenReq(index) {
const oldTokenReqs = this.tokenReqs.slice();
let targetReqIndex = this.tokenReqs.findIndex(
tokenReq => tokenReq.name === this.tokenReqName
({ name }) => name === this.tokenReqName
);
if (targetReqIndex < 0) return;
this.$store.commit("removeOAuthTokenReq", targetReqIndex);
@@ -2822,10 +2784,8 @@ export default {
}
});
},
tokenReqChange(event) {
let targetReq = this.tokenReqs.find(
tokenReq => tokenReq.name === event.target.value
);
tokenReqChange({ target }) {
let targetReq = this.tokenReqs.find(({ name }) => name === target.value);
let {
oidcDiscoveryUrl,
authUrl,