Skip to content

Commit

Permalink
Merge pull request #232 from am1rb/master
Browse files Browse the repository at this point in the history
🐛 Do not jasonify FormData instances
  • Loading branch information
elbywan authored Jun 1, 2024
2 parents cd1b822 + 71aa327 commit 0619303
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
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 @@ -164,6 +164,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 @@ -254,6 +254,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

0 comments on commit 0619303

Please sign in to comment.