Merge pull request #893 from AndrewBastin/refactor/893

Some small refactoring
This commit is contained in:
Andrew Bastin
2020-05-22 04:56:20 -04:00
committed by GitHub
4 changed files with 40 additions and 48 deletions

View File

@@ -40,7 +40,7 @@
</template>
<script>
import { sseValid } from "~/functions/utils/valid"
import { httpValid } from "~/functions/utils/valid"
export default {
components: {
@@ -60,7 +60,7 @@ export default {
},
computed: {
serverValid() {
return sseValid(this.server)
return httpValid(this.server)
},
},
methods: {
@@ -81,7 +81,7 @@ export default {
if (typeof EventSource !== "undefined") {
try {
this.sse = new EventSource(this.server)
this.sse.onopen = event => {
this.sse.onopen = (event) => {
this.connectionSSEState = true
this.events.log = [
{
@@ -95,10 +95,10 @@ export default {
icon: "sync",
})
}
this.sse.onerror = event => {
this.sse.onerror = (event) => {
this.handleSSEError()
}
this.sse.onclose = event => {
this.sse.onclose = (event) => {
this.connectionSSEState = false
this.events.log.push({
payload: this.$t("disconnected_from", { name: this.server }),
@@ -110,7 +110,7 @@ export default {
icon: "sync_disabled",
})
}
this.sse.onmessage = event => {
this.sse.onmessage = (event) => {
this.events.log.push({
payload: event.data,
source: "server",

View File

@@ -0,0 +1,17 @@
export const knownContentTypes = [
"application/json",
"application/vnd.api+json",
"application/hal+json",
"application/xml",
"application/x-www-form-urlencoded",
"text/html",
"text/plain",
]
export function isJSONContentType(contentType) {
return (
contentType === "application/json" ||
contentType === "application/vnd.api+json" ||
contentType === "application/hal+json"
)
}

View File

@@ -25,7 +25,7 @@ export function wsValid(url) {
/**
* valid url for http/https
*/
export function sseValid(url) {
export function httpValid(url) {
return sseRegexIP.test(url) || sseRegexHostname.test(url)
}

View File

@@ -1339,6 +1339,9 @@ import {
getQueryParams,
} from "../functions/requestParams.js"
import { parseUrlAndPath } from "../functions/utils/uri.js"
import { httpValid } from "../functions/utils/valid"
import { knownContentTypes, isJSONContentType } from "../functions/utils/contenttypes"
const statusCategories = [
{
name: "informational",
@@ -1425,6 +1428,7 @@ export default {
headers: "",
body: "",
},
validContentTypes: knownContentTypes,
previewEnabled: false,
paramsWatchEnabled: true,
expandResponse: false,
@@ -1477,11 +1481,9 @@ export default {
},
contentType(contentType, oldContentType) {
const getDefaultParams = (contentType) => {
if (isJSONContentType(contentType)) return "{}"
switch (contentType) {
case "application/json":
case "application/vnd.api+json":
case "application/hal+json":
return "{}"
case "application/xml":
return "<?xml version='1.0' encoding='utf-8'?>"
case "text/html":
@@ -1502,11 +1504,7 @@ export default {
this.responseBodyText = this.response.body
this.responseBodyType = "text"
} else {
if (
this.responseType === "application/json" ||
this.responseType === "application/hal+json" ||
this.responseType === "application/vnd.api+json"
) {
if (isJSONContentType(this.responseType)) {
this.responseBodyText = JSON.stringify(this.response.body, null, 2)
this.responseBodyType =
this.response.body.constructor.name === "Object" ? "json" : "json5"
@@ -1578,18 +1576,6 @@ export default {
},
},
computed: {
/**
* These are a list of Content Types known to Postwoman.
*/
validContentTypes: () => [
"application/json",
"application/vnd.api+json",
"application/hal+json",
"application/xml",
"application/x-www-form-urlencoded",
"text/html",
"text/plain",
],
/**
* Check content types that can be automatically
* serialized by postwoman.
@@ -1597,7 +1583,7 @@ export default {
canListParameters() {
return (
this.contentType === "application/x-www-form-urlencoded" ||
this.contentType.endsWith("json")
isJSONContentType(this.contentType)
)
},
uri: {
@@ -1858,18 +1844,8 @@ export default {
return findStatusGroup(this.response.status)
},
isValidURL() {
if (this.showPreRequestScript) {
// we cannot determine if a URL is valid because the full string is not known ahead of time
return true
}
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])$`
)
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/])$`
)
return validIP.test(this.url) || validHostname.test(this.url)
// if showPreRequestScript, we cannot determine if a URL is valid because the full string is not known ahead of time
return this.showPreRequestScript || httpValid(this.url)
},
hasRequestBody() {
return ["POST", "PUT", "PATCH"].includes(this.method)
@@ -1879,7 +1855,7 @@ export default {
},
rawRequestBody() {
const { bodyParams, contentType } = this
if (contentType.endsWith("json")) {
if (isJSONContentType(contentType)) {
try {
const obj = JSON.parse(
`{${bodyParams
@@ -1937,7 +1913,7 @@ export default {
}
if (["POST", "PUT", "PATCH"].includes(this.method)) {
let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody
if (this.contentType.includes("json")) {
if (isJSONContentType(this.contentType)) {
requestBody = `JSON.stringify(${requestBody})`
} else if (this.contentType.includes("x-www-form-urlencoded")) {
requestBody = `"${requestBody}"`
@@ -1966,7 +1942,7 @@ export default {
}
if (["POST", "PUT", "PATCH"].includes(this.method)) {
let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody
if (this.contentType.includes("json")) {
if (isJSONContentType(this.contentType)) {
requestBody = `JSON.stringify(${requestBody})`
} else if (this.contentType.includes("x-www-form-urlencoded")) {
requestBody = `"${requestBody}"`
@@ -2410,10 +2386,9 @@ export default {
icon: "done",
})
const aux = document.createElement("textarea")
const copy =
this.responseType === "application/json"
? JSON.stringify(this.response.body, null, 2)
: this.response.body
const copy = isJSONContentType(this.responseType)
? JSON.stringify(this.response.body, null, 2)
: this.response.body
aux.innerText = copy
document.body.appendChild(aux)
aux.select()