mirror of
https://github.com/versia-pub/api.git
synced 2025-12-08 17:28:20 +01:00
fix(client): 🐛 If requests don't have bodies, don't set Content-Type to JSON
This commit is contained in:
parent
f10ef84c0f
commit
c392b20f31
|
|
@ -92,16 +92,26 @@ export class BaseClient {
|
||||||
body?: object | FormData,
|
body?: object | FormData,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Request> {
|
): 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(), {
|
return new Request(new URL(path, this.baseUrl).toString(), {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers,
|
||||||
Authorization: this.accessToken
|
|
||||||
? `Bearer ${this.accessToken}`
|
|
||||||
: "",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"User-Agent": DEFAULT_UA,
|
|
||||||
...extra?.headers,
|
|
||||||
},
|
|
||||||
body: body
|
body: body
|
||||||
? body instanceof FormData
|
? body instanceof FormData
|
||||||
? body
|
? body
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue