Skip to content

Commit

Permalink
🐛 Do not jasonify FormData instances
Browse files Browse the repository at this point in the history
closes #231
  • Loading branch information
am1rb committed Jun 1, 2024
1 parent b299791 commit 4693837
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
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 isFormDataInstance = body => {
const formData = this._config.polyfill("FormData", false)
return formData !== null && body instanceof formData
}
const jsonify = typeof body === "object" && !isFormDataInstance(body) && (!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 = 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

0 comments on commit 4693837

Please sign in to comment.