Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant