Skip to content

Commit 81c031d

Browse files
authored
Improve header handling (#2308)
1 parent 0058128 commit 81c031d

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

.changeset/bumpy-parrots-lie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Improve header handling

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"size-limit": "^11.2.0",
3131
"turbo": "^2.5.2",
3232
"typescript": "^5.8.3",
33-
"vitest": "^3.0.0"
33+
"vitest": "^3.1.3"
3434
},
3535
"size-limit": [
3636
{

packages/openapi-fetch/src/index.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,38 @@ export default function createClient(clientOptions) {
7373
});
7474
}
7575

76-
const serializedBody = body === undefined ? undefined : bodySerializer(body, headers);
77-
78-
const defaultHeaders =
76+
const serializedBody =
77+
body === undefined
78+
? undefined
79+
: bodySerializer(
80+
body,
81+
// Note: we declare mergeHeaders() both here and below because it’s a bit of a chicken-or-egg situation:
82+
// bodySerializer() needs all headers so we aren’t dropping ones set by the user, however,
83+
// the result of this ALSO sets the lowest-priority content-type header. So we re-merge below,
84+
// setting the content-type at the very beginning to be overwritten.
85+
// Lastly, based on the way headers work, it’s not a simple “present-or-not” check becauase null intentionally un-sets headers.
86+
mergeHeaders(baseHeaders, headers, params.header),
87+
);
88+
const finalHeaders = mergeHeaders(
7989
// with no body, we should not to set Content-Type
8090
serializedBody === undefined ||
81-
// if serialized body is FormData; browser will correctly set Content-Type & boundary expression
82-
serializedBody instanceof FormData
91+
// if serialized body is FormData; browser will correctly set Content-Type & boundary expression
92+
serializedBody instanceof FormData
8393
? {}
8494
: {
8595
"Content-Type": "application/json",
86-
};
96+
},
97+
baseHeaders,
98+
headers,
99+
params.header,
100+
);
87101

88102
const requestInit = {
89103
redirect: "follow",
90104
...baseOptions,
91105
...init,
92106
body: serializedBody,
93-
headers: mergeHeaders(defaultHeaders, baseHeaders, headers, params.header),
107+
headers: finalHeaders,
94108
};
95109

96110
let id;

packages/openapi-metadata/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
},
6969
"devDependencies": {
7070
"@types/node": "^22.15.3",
71-
"@vitest/coverage-v8": "^3.0.0",
71+
"@vitest/coverage-v8": "^3.1.3",
7272
"reflect-metadata": "^0.2.2",
7373
"typescript": "^5.8.3",
7474
"unbuild": "^3.5.0",

pnpm-lock.yaml

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)