Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Do not jasonify FormData instances #232

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export const core: Wretch = {
headers(headerValues) {
const headers =
!headerValues ? {} :
Array.isArray(headerValues) ? Object.fromEntries(headerValues) :
"entries" in headerValues ? Object.fromEntries((headerValues as Headers).entries()) :
headerValues
Array.isArray(headerValues) ? Object.fromEntries(headerValues) :
"entries" in headerValues ? Object.fromEntries((headerValues as Headers).entries()) :
headerValues
return { ...this, _options: mix(this._options, { headers }) }
},
accept(headerValue) {
Expand Down Expand Up @@ -92,7 +92,11 @@ export const core: Wretch = {
let base = this.url(url).options({ method })
// "Jsonify" the body if it is an object and if it is likely that the content type targets json.
const contentType = extractContentType(base._options.headers)
const jsonify = typeof body === "object" && (!base._options.headers || !contentType || isLikelyJsonMime(contentType))
const formDataClass = this._config.polyfill("FormData", false)
const jsonify =
typeof body === "object" &&
!(formDataClass && body instanceof formDataClass) &&
(!base._options.headers || !contentType || isLikelyJsonMime(contentType))
base =
!body ? base :
jsonify ? base.json(body, contentType) :
Expand Down
19 changes: 19 additions & 0 deletions test/browser/wretch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ describe("Wretch", function () {
})
})

it("should not Jasonify a FormData instance", async function () {
const FormData = wretch()._config.polyfill(
"FormData",
false
);

let formData = new FormData()
formData.append("hello", "world")
formData.append("duck", "Muscovy")

let decoded = await wretch(`${_URL}/formData/decode`)
.post(formData)
.json()
expect(decoded).toEqual({
hello: "world",
duck: "Muscovy",
})
})

it("should perform OPTIONS and HEAD requests", async function () {
const optsRes = await wretch(_URL + "/options").opts().res()
const optsRes2 = await wretch(_URL + "/options").opts("").res()
Expand Down
19 changes: 19 additions & 0 deletions test/deno/wretch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ describe("Wretch", function () {
})
})

it("should not Jasonify a FormData instance", async function () {
const FormData = wretch()._config.polyfill(
"FormData",
false
);

let formData = new FormData()
formData.append("hello", "world")
formData.append("duck", "Muscovy")

let decoded = await wretch(`${_URL}/formData/decode`)
.post(formData)
.json()
assertEquals(decoded, {
hello: "world",
duck: "Muscovy",
})
})

it("should perform OPTIONS and HEAD requests", async function () {
const optsRes = await wretch(_URL + "/options").opts().res()
const optsRes2 = await wretch(_URL + "/options").opts("").res()
Expand Down
24 changes: 24 additions & 0 deletions test/node/wretch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ describe("Wretch", function () {
})
})

it("should not Jasonify a FormData instance", async function () {
const FormData = wretch()._config.polyfill(
"FormData",
false
);

let formData: any = new FormData()
formData.append("hello", "world")
formData.append("duck", "Muscovy")
formData.append("duckImage", fs.createReadStream(duckImagePath))

let decoded = await wretch(`${_URL}/formData/decode`)
.post(formData)
.json()
expect(decoded).toMatchObject({
hello: "world",
duck: "Muscovy",
duckImage: {
data: duckImage,
type: "Buffer"
}
})
})

it("should perform OPTIONS and HEAD requests", async function () {
const optsRes = await wretch(_URL + "/options").opts().res()
const optsRes2 = await wretch(_URL + "/options").opts("").res()
Expand Down
Loading