From b0da5624726976cf80ddd785bbf9175a2c0df37d Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Fri, 22 May 2020 02:53:36 -0400 Subject: [PATCH] Refactor content type based code to separate file --- functions/utils/contenttypes.js | 17 ++++++++++++++ pages/index.vue | 41 ++++++++++----------------------- 2 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 functions/utils/contenttypes.js diff --git a/functions/utils/contenttypes.js b/functions/utils/contenttypes.js new file mode 100644 index 000000000..4e9173cee --- /dev/null +++ b/functions/utils/contenttypes.js @@ -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" + ) +} diff --git a/pages/index.vue b/pages/index.vue index 17ed6ddbd..4410efc6c 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -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 "" 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()