Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Refactor content type based code to separate file

Andrew Bastinb0da56247269
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 "<?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()