Skip to content

Commit

Permalink
🐛 Fix passing Headers/entries array as an argument to headers()
Browse files Browse the repository at this point in the history
Should solve #205
  • Loading branch information
elbywan committed Nov 19, 2023
1 parent 9e6fa07 commit f32845a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export const core: Wretch = {
return { ...this, _options: replace ? options : mix(this._options, options) }
},
headers(headerValues) {
return { ...this, _options: mix(this._options, { headers: headerValues || {} }) }
const headers =
!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) {
return this.headers({ Accept: headerValue })
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CONTENT_TYPE_HEADER } from "./constants.js"

export function extractContentType(headers: HeadersInit = {}): string | undefined {
export function extractContentType(headers: Record<string, string> = {}): string | undefined {
return Object.entries(headers).find(([k]) =>
k.toLowerCase() === CONTENT_TYPE_HEADER.toLowerCase()
)?.[1]
Expand Down
5 changes: 3 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"target": "es5",
"lib": [
"es2015",
"dom"
"dom",
"dom.iterable"
],
"module": "commonjs",
"noImplicitAny": false,
Expand All @@ -17,4 +18,4 @@
"include": [
"./node/**/*.spec.ts"
]
}
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"target": "ES2018",
"lib": [
"es2020",
"dom"
"dom",
"dom.iterable"
],
"module": "es2015",
"outDir": "dist",
Expand All @@ -19,4 +20,4 @@
"src/**/*",
"test.ts"
]
}
}

0 comments on commit f32845a

Please sign in to comment.