-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: smater header defaults and handling
- stop setting Content-Type header for multipart requests - support HeaderInit in request objects - support default headers BREAKING CHANGE: multipart/form-data headers are not set by oazapfts anymore as these are usually automatically set by the browser/node when this causes problems on your end you can set Content-Type=multipart/form-data manually via RequestOpts oazapfts runtime now works with `Header` instead of plain Objects `{}`. This might affect you when you use a custom fetch implementation and manipulate headers there fix #512 ref #509
- Loading branch information
Showing
10 changed files
with
152 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export type CustomHeaders = Record< | ||
string, | ||
string | null | boolean | number | undefined | ||
>; | ||
|
||
export function mergeHeaders( | ||
base: HeadersInit | CustomHeaders | undefined, | ||
overwrite?: HeadersInit | CustomHeaders, | ||
) { | ||
const baseHeaders = normalizeHeaders(base); | ||
const overwriteHeaders = normalizeHeaders(overwrite); | ||
|
||
overwriteHeaders.forEach((value, key) => { | ||
baseHeaders.set(key, value); | ||
}); | ||
|
||
return baseHeaders; | ||
} | ||
|
||
export function normalizeHeaders( | ||
headers: HeadersInit | CustomHeaders | undefined, | ||
) { | ||
// This might be custom header config containing null | boolean | number | undefined | ||
// By default Headers constructor will convert them to string but we don't want that | ||
// for nullish values. | ||
if (headers && !(headers instanceof Headers) && !Array.isArray(headers)) { | ||
return new Headers( | ||
Object.fromEntries( | ||
Object.entries(headers) | ||
.filter(([, v]) => v != null) | ||
.map(([k, v]) => [k, String(v)]), | ||
), | ||
); | ||
} | ||
|
||
return new Headers(headers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.