fix(client): 🐛 If requests don't have bodies, don't set Content-Type to JSON

This commit is contained in:
Jesse Wierzbinski 2024-06-07 13:04:25 -10:00
parent f10ef84c0f
commit c392b20f31
No known key found for this signature in database

View file

@ -92,16 +92,26 @@ export class BaseClient {
body?: object | FormData,
extra?: RequestInit,
): Promise<Request> {
const headers = new Headers({
"User-Agent": DEFAULT_UA,
});
if (this.accessToken) {
headers.set("Authorization", `Bearer ${this.accessToken}`);
}
if (body) {
if (!(body instanceof FormData)) {
headers.set("Content-Type", "application/json; charset=utf-8");
} // else: let FormData set the content type, as it knows best (boundary, etc.)
}
for (const [key, value] of Object.entries(extra?.headers || {})) {
headers.set(key, value);
}
return new Request(new URL(path, this.baseUrl).toString(), {
method,
headers: {
Authorization: this.accessToken
? `Bearer ${this.accessToken}`
: "",
"Content-Type": "application/json",
"User-Agent": DEFAULT_UA,
...extra?.headers,
},
headers,
body: body
? body instanceof FormData
? body