-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add individual content types for formadata (#4550)
Co-authored-by: jamesgeorge007 <[email protected]>
- Loading branch information
1 parent
b78cd57
commit c74c42e
Showing
10 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
packages/hoppscotch-common/src/helpers/functional/formData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
type FormDataEntry = { | ||
key: string | ||
contentType?: string | ||
value: string | Blob | ||
} | ||
|
||
export const toFormData = (values: FormDataEntry[]) => { | ||
const formData = new FormData() | ||
|
||
values.forEach(({ key, value }) => formData.append(key, value)) | ||
values.forEach(({ key, value, contentType }) => { | ||
if (contentType) { | ||
formData.append( | ||
key, | ||
new Blob([value], { | ||
type: contentType, | ||
}), | ||
key | ||
) | ||
|
||
return | ||
} | ||
|
||
formData.append(key, value) | ||
}) | ||
|
||
return formData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { defineVersion } from "verzod" | ||
import { z } from "zod" | ||
|
||
import { V8_SCHEMA } from "./8" | ||
|
||
export const FormDataKeyValue = z | ||
.object({ | ||
key: z.string(), | ||
active: z.boolean(), | ||
contentType: z.string().optional().catch(undefined), | ||
}) | ||
.and( | ||
z.union([ | ||
z.object({ | ||
isFile: z.literal(true), | ||
value: z.array(z.instanceof(Blob).nullable()), | ||
}), | ||
z.object({ | ||
isFile: z.literal(false), | ||
value: z.string(), | ||
}), | ||
]) | ||
) | ||
|
||
export type FormDataKeyValue = z.infer<typeof FormDataKeyValue> | ||
|
||
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([]), | ||
showIndividualContentType: z.boolean().optional().catch(false), | ||
}), | ||
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 V9_SCHEMA = V8_SCHEMA.extend({ | ||
v: z.literal("9"), | ||
body: HoppRESTReqBody, | ||
}) | ||
|
||
export default defineVersion({ | ||
schema: V9_SCHEMA, | ||
initial: false, | ||
up(old: z.infer<typeof V8_SCHEMA>) { | ||
// No migration, the new contentType added to each formdata field is optional | ||
return { | ||
...old, | ||
v: "9" as const, | ||
} | ||
}, | ||
}) |