Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 94dafd4

Browse files
don't send empty headers
1 parent 492e5f7 commit 94dafd4

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/utils/http.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@ const getBody = async (
2525
}
2626
};
2727

28-
const getHeaders = (headers: Headers) =>
28+
const createRequestHeaders = (headers: { [key: string]: string }) =>
29+
new Headers(
30+
Object.keys(headers)
31+
.filter(key => headers[key])
32+
.reduce(
33+
(akk, key) => ({
34+
...akk,
35+
[key]: headers[key],
36+
}),
37+
{},
38+
),
39+
);
40+
41+
const getResponseHeaders = (headers: Headers) =>
2942
[...headers.entries()].reduce(
3043
(acc, [key, value]) =>
3144
value
@@ -40,7 +53,7 @@ const getHeaders = (headers: Headers) =>
4053
const mapResponse = async (response: Response): Promise<HttpResponse> => {
4154
const { ok, status, statusText, url } = response;
4255

43-
const headers = getHeaders(response.headers);
56+
const headers = getResponseHeaders(response.headers);
4457

4558
return {
4659
body: await getBody(response, headers),
@@ -55,7 +68,7 @@ const mapResponse = async (response: Response): Promise<HttpResponse> => {
5568
const get = ({ link, headers = {} }: HttpRequest) =>
5669
fetch(link.href, {
5770
credentials: 'omit',
58-
headers: new Headers({
71+
headers: createRequestHeaders({
5972
accept: link.type || mediaTypes.any,
6073
...headers,
6174
}),
@@ -73,7 +86,7 @@ const post = <TRequest extends object, TResponse extends object>({
7386
fetch(link.href, {
7487
body: JSON.stringify(body),
7588
credentials: 'omit',
76-
headers: new Headers({
89+
headers: createRequestHeaders({
7790
accept: link.type || mediaTypes.any,
7891
'content-type': mediaTypes.json,
7992
...headers,
@@ -84,7 +97,7 @@ const post = <TRequest extends object, TResponse extends object>({
8497
const _delete = ({ link, headers = {} }: HttpRequest) =>
8598
fetch(link.href, {
8699
credentials: 'omit',
87-
headers: new Headers({
100+
headers: createRequestHeaders({
88101
accept: link.type || mediaTypes.any,
89102
...headers,
90103
}),

0 commit comments

Comments
 (0)