fix: ensure Content-Type header priority in the CLI (#4242)

- Ensure the `Content-Type` header takes priority over the value set in the request body.
- Introduces `HoppRESTRequest` schema `v6` with `text/xml` added under the supported content types.
This commit is contained in:
James George
2024-08-07 09:16:27 -07:00
committed by GitHub
parent 31b691bb37
commit a8bcc75467
11 changed files with 290 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ export const knownContentTypes = {
"application/hal+json": "json",
"application/vnd.api+json": "json",
"application/xml": "xml",
"text/xml": "xml",
"application/x-www-form-urlencoded": "multipart",
"multipart/form-data": "multipart",
"text/html": "html",
@@ -12,4 +13,6 @@ export const knownContentTypes = {
export type ValidContentTypes = keyof typeof knownContentTypes
export const ValidContentTypesList = Object.keys(knownContentTypes) as ValidContentTypes[]
export const ValidContentTypesList = Object.keys(
knownContentTypes
) as ValidContentTypes[]

View File

@@ -1,32 +1,34 @@
import * as Eq from "fp-ts/Eq"
import * as S from "fp-ts/string"
import cloneDeep from "lodash/cloneDeep"
import { createVersionedEntity, InferredEntity } from "verzod"
import { z } from "zod"
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
import V0_VERSION from "./v/0"
import V1_VERSION from "./v/1"
import V2_VERSION from "./v/2"
import V3_VERSION from "./v/3"
import V4_VERSION from "./v/4"
import V5_VERSION from "./v/5"
import { createVersionedEntity, InferredEntity } from "verzod"
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
import V6_VERSION, { HoppRESTReqBody } from "./v/6"
import { HoppRESTReqBody, HoppRESTHeaders, HoppRESTParams } from "./v/1"
import { HoppRESTHeaders, HoppRESTParams } from "./v/1"
import { HoppRESTAuth } from "./v/5"
import { HoppRESTRequestVariables } from "./v/2"
import { z } from "zod"
import { HoppRESTAuth } from "./v/5"
export * from "./content-types"
export {
FormDataKeyValue,
HoppRESTReqBodyFormData,
HoppRESTAuthBasic,
HoppRESTAuthInherit,
HoppRESTAuthBearer,
HoppRESTAuthInherit,
HoppRESTAuthNone,
HoppRESTReqBody,
HoppRESTHeaders,
HoppRESTReqBodyFormData,
} from "./v/1"
export {
@@ -37,21 +39,23 @@ export {
export {
AuthCodeGrantTypeParams,
HoppRESTAuthOAuth2,
HoppRESTAuth,
HoppRESTAuthOAuth2,
} from "./v/5"
export { HoppRESTAuthAPIKey } from "./v/4"
export { HoppRESTRequestVariables } from "./v/2"
export { HoppRESTReqBody } from "./v/6"
const versionedObject = z.object({
// v is a stringified number
v: z.string().regex(/^\d+$/).transform(Number),
})
export const HoppRESTRequest = createVersionedEntity({
latestVersion: 5,
latestVersion: 6,
versionMap: {
0: V0_VERSION,
1: V1_VERSION,
@@ -59,6 +63,7 @@ export const HoppRESTRequest = createVersionedEntity({
3: V3_VERSION,
4: V4_VERSION,
5: V5_VERSION,
6: V6_VERSION,
},
getVersion(data) {
// For V1 onwards we have the v string storing the number
@@ -100,7 +105,7 @@ const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
),
})
export const RESTReqSchemaVersion = "5"
export const RESTReqSchemaVersion = "6"
export type HoppRESTParam = HoppRESTRequest["params"][number]
export type HoppRESTHeader = HoppRESTRequest["headers"][number]
@@ -195,7 +200,7 @@ export function makeRESTRequest(
export function getDefaultRESTRequest(): HoppRESTRequest {
return {
v: "5",
v: RESTReqSchemaVersion,
endpoint: "https://echo.hoppscotch.io",
name: "Untitled",
params: [],

View File

@@ -0,0 +1,49 @@
import { defineVersion } from "verzod"
import { z } from "zod"
import { FormDataKeyValue } from "./1"
import { V5_SCHEMA } from "./5"
export const HoppRESTReqBody = z.union([
z.object({
contentType: z.literal(null),
body: z.literal(null).catch(null),
}),
z.object({
contentType: z.literal("multipart/form-data"),
body: z.array(FormDataKeyValue).catch([]),
}),
z.object({
contentType: z.union([
z.literal("application/json"),
z.literal("application/ld+json"),
z.literal("application/hal+json"),
z.literal("application/vnd.api+json"),
z.literal("application/xml"),
z.literal("text/xml"),
z.literal("application/x-www-form-urlencoded"),
z.literal("text/html"),
z.literal("text/plain"),
]),
body: z.string().catch(""),
}),
])
export type HoppRESTReqBody = z.infer<typeof HoppRESTReqBody>
export const V6_SCHEMA = V5_SCHEMA.extend({
v: z.literal("6"),
body: HoppRESTReqBody,
})
export default defineVersion({
schema: V6_SCHEMA,
initial: false,
up(old: z.infer<typeof V5_SCHEMA>) {
// No migration, `text/xml` is added to the list of supported content types
return {
...old,
v: "6" as const,
}
},
})