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