Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Spreading headers may lose data #188

Open
OliverJAsh opened this issue Dec 15, 2021 · 0 comments
Open

Spreading headers may lose data #188

OliverJAsh opened this issue Dec 15, 2021 · 0 comments

Comments

@OliverJAsh
Copy link
Member

Related: https://github.com/unsplash/unsplash-web/pull/7397

headers: {
...headers,
...additionalFetchOptions.headers,
},

The type of the headers variable that we're spreading here is HeadersInit | undefined.

HeadersInit is defined as:

type HeadersInit = string[][] | Record<string, string> | Headers;

What happens when we merge an array (string[][]) into an object? The array indexes become header keys. That's not the correct behaviour—we should instead use the key inside the entries array.

const a = [['a', '1']]
const b = { ...a }
JSON.stringify(b)
// => '{"0":["a","1"]}'

Desired result: { a: 1 }

What happens when we merge a Headers class into an object? We lose all data!

const a = new Headers([['a', '1']])
const b = { ...a }
JSON.stringify(b)
// => '{}'

Desired result: { a: 1 }

Related: a lint rule could have caught this typescript-eslint/typescript-eslint#748

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant