Skip to content

Commit f32845a

Browse files
committed
🐛 Fix passing Headers/entries array as an argument to headers()
Should solve #205
1 parent 9e6fa07 commit f32845a

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

src/core.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ export const core: Wretch = {
4949
return { ...this, _options: replace ? options : mix(this._options, options) }
5050
},
5151
headers(headerValues) {
52-
return { ...this, _options: mix(this._options, { headers: headerValues || {} }) }
52+
const headers =
53+
!headerValues ? {} :
54+
Array.isArray(headerValues) ? Object.fromEntries(headerValues) :
55+
"entries" in headerValues ? Object.fromEntries((headerValues as Headers).entries()) :
56+
headerValues
57+
return { ...this, _options: mix(this._options, { headers }) }
5358
},
5459
accept(headerValue) {
5560
return this.headers({ Accept: headerValue })

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CONTENT_TYPE_HEADER } from "./constants.js"
22

3-
export function extractContentType(headers: HeadersInit = {}): string | undefined {
3+
export function extractContentType(headers: Record<string, string> = {}): string | undefined {
44
return Object.entries(headers).find(([k]) =>
55
k.toLowerCase() === CONTENT_TYPE_HEADER.toLowerCase()
66
)?.[1]

test/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"target": "es5",
44
"lib": [
55
"es2015",
6-
"dom"
6+
"dom",
7+
"dom.iterable"
78
],
89
"module": "commonjs",
910
"noImplicitAny": false,
@@ -17,4 +18,4 @@
1718
"include": [
1819
"./node/**/*.spec.ts"
1920
]
20-
}
21+
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"target": "ES2018",
44
"lib": [
55
"es2020",
6-
"dom"
6+
"dom",
7+
"dom.iterable"
78
],
89
"module": "es2015",
910
"outDir": "dist",
@@ -19,4 +20,4 @@
1920
"src/**/*",
2021
"test.ts"
2122
]
22-
}
23+
}

0 commit comments

Comments
 (0)