Skip to content

Commit 41ed9b5

Browse files
committed
Avoid type-casting in headersToObj
1 parent a9cd889 commit 41ed9b5

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

packages/openapi-fetch/test/helpers.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ export function createObservedClient<T extends {}, M extends MediaType = MediaTy
2727
* Convert a Headers object to a plain object for easier comparison
2828
*/
2929
export function headersToObj(headers: Headers | Record<string, string>): Record<string, string> {
30-
// Headers is iterable at runtime in both TS 5 and TS 6, but the lib type only
31-
// declares Iterable in TS 6's lib.dom. Double-cast bridges the gap.
32-
const entries =
33-
headers instanceof Headers ? Array.from(headers as unknown as Iterable<[string, string]>) : Object.entries(headers);
3430
const result: Record<string, string> = {};
35-
for (const [k, v] of entries) {
36-
result[k] = v;
31+
if (headers instanceof Headers) {
32+
headers.forEach((value, key) => {
33+
result[key] = value;
34+
});
35+
} else {
36+
for (const [key, value] of Object.entries(headers)) {
37+
result[key] = value;
38+
}
3739
}
3840
return result;
3941
}

0 commit comments

Comments
 (0)