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

@@ -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

@@ -1340,6 +1340,7 @@ import {
} from "../functions/requestParams.js" } from "../functions/requestParams.js"
import { parseUrlAndPath } from "../functions/utils/uri.js" import { parseUrlAndPath } from "../functions/utils/uri.js"
import { httpValid } from "../functions/utils/valid" import { httpValid } from "../functions/utils/valid"
import { knownContentTypes, isJSONContentType } from "../functions/utils/contenttypes"
const statusCategories = [ const statusCategories = [
{ {
@@ -1427,6 +1428,7 @@ export default {
headers: "", headers: "",
body: "", body: "",
}, },
validContentTypes: knownContentTypes,
previewEnabled: false, previewEnabled: false,
paramsWatchEnabled: true, paramsWatchEnabled: true,
expandResponse: false, expandResponse: false,
@@ -1479,11 +1481,9 @@ export default {
}, },
contentType(contentType, oldContentType) { contentType(contentType, oldContentType) {
const getDefaultParams = (contentType) => { const getDefaultParams = (contentType) => {
if (isJSONContentType(contentType)) return "{}"
switch (contentType) { switch (contentType) {
case "application/json":
case "application/vnd.api+json":
case "application/hal+json":
return "{}"
case "application/xml": case "application/xml":
return "<?xml version='1.0' encoding='utf-8'?>" return "<?xml version='1.0' encoding='utf-8'?>"
case "text/html": case "text/html":
@@ -1504,11 +1504,7 @@ export default {
this.responseBodyText = this.response.body this.responseBodyText = this.response.body
this.responseBodyType = "text" this.responseBodyType = "text"
} else { } else {
if ( if (isJSONContentType(this.responseType)) {
this.responseType === "application/json" ||
this.responseType === "application/hal+json" ||
this.responseType === "application/vnd.api+json"
) {
this.responseBodyText = JSON.stringify(this.response.body, null, 2) this.responseBodyText = JSON.stringify(this.response.body, null, 2)
this.responseBodyType = this.responseBodyType =
this.response.body.constructor.name === "Object" ? "json" : "json5" this.response.body.constructor.name === "Object" ? "json" : "json5"
@@ -1580,18 +1576,6 @@ export default {
}, },
}, },
computed: { 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 * Check content types that can be automatically
* serialized by postwoman. * serialized by postwoman.
@@ -1599,7 +1583,7 @@ export default {
canListParameters() { canListParameters() {
return ( return (
this.contentType === "application/x-www-form-urlencoded" || this.contentType === "application/x-www-form-urlencoded" ||
this.contentType.endsWith("json") isJSONContentType(this.contentType)
) )
}, },
uri: { uri: {
@@ -1871,7 +1855,7 @@ export default {
}, },
rawRequestBody() { rawRequestBody() {
const { bodyParams, contentType } = this const { bodyParams, contentType } = this
if (contentType.endsWith("json")) { if (isJSONContentType(contentType)) {
try { try {
const obj = JSON.parse( const obj = JSON.parse(
`{${bodyParams `{${bodyParams
@@ -1929,7 +1913,7 @@ export default {
} }
if (["POST", "PUT", "PATCH"].includes(this.method)) { if (["POST", "PUT", "PATCH"].includes(this.method)) {
let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody
if (this.contentType.includes("json")) { if (isJSONContentType(this.contentType)) {
requestBody = `JSON.stringify(${requestBody})` requestBody = `JSON.stringify(${requestBody})`
} else if (this.contentType.includes("x-www-form-urlencoded")) { } else if (this.contentType.includes("x-www-form-urlencoded")) {
requestBody = `"${requestBody}"` requestBody = `"${requestBody}"`
@@ -1958,7 +1942,7 @@ export default {
} }
if (["POST", "PUT", "PATCH"].includes(this.method)) { if (["POST", "PUT", "PATCH"].includes(this.method)) {
let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody
if (this.contentType.includes("json")) { if (isJSONContentType(this.contentType)) {
requestBody = `JSON.stringify(${requestBody})` requestBody = `JSON.stringify(${requestBody})`
} else if (this.contentType.includes("x-www-form-urlencoded")) { } else if (this.contentType.includes("x-www-form-urlencoded")) {
requestBody = `"${requestBody}"` requestBody = `"${requestBody}"`
@@ -2402,10 +2386,9 @@ export default {
icon: "done", icon: "done",
}) })
const aux = document.createElement("textarea") const aux = document.createElement("textarea")
const copy = const copy = isJSONContentType(this.responseType)
this.responseType === "application/json" ? JSON.stringify(this.response.body, null, 2)
? 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)
aux.select() aux.select()