Refactor content type based code to separate file

This commit is contained in:
Andrew Bastin
2020-05-22 02:53:36 -04:00
parent a0d8b9029e
commit b0da562472
2 changed files with 29 additions and 29 deletions

View File

@@ -1340,6 +1340,7 @@ import {
} 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 = [
{
@@ -1427,6 +1428,7 @@ export default {
headers: "",
body: "",
},
validContentTypes: knownContentTypes,
previewEnabled: false,
paramsWatchEnabled: true,
expandResponse: false,
@@ -1479,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":
@@ -1504,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"
@@ -1580,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.
@@ -1599,7 +1583,7 @@ export default {
canListParameters() {
return (
this.contentType === "application/x-www-form-urlencoded" ||
this.contentType.endsWith("json")
isJSONContentType(this.contentType)
)
},
uri: {
@@ -1871,7 +1855,7 @@ export default {
},
rawRequestBody() {
const { bodyParams, contentType } = this
if (contentType.endsWith("json")) {
if (isJSONContentType(contentType)) {
try {
const obj = JSON.parse(
`{${bodyParams
@@ -1929,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}"`
@@ -1958,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}"`
@@ -2402,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()