Skip to content

Commit

Permalink
Fix custom content type issue
Browse files Browse the repository at this point in the history
  • Loading branch information
flevi29 committed Jan 10, 2025
1 parent 576231a commit 5e0b1c3
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions src/http-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ function getTimeoutFn(
export class HttpRequests {
#url: URL;
#requestInit: HttpRequestsRequestInit;
#defaultContentType: string | null;
#customRequestFn?: Config["httpClient"];
#requestTimeout?: Config["timeout"];

Expand All @@ -159,8 +158,6 @@ export class HttpRequests {
headers: getHeaders(config, config.requestInit?.headers),
};

this.#defaultContentType = this.#requestInit.headers.get("Content-Type");

this.#customRequestFn = config.httpClient;
this.#requestTimeout = config.timeout;
}
Expand All @@ -169,15 +166,10 @@ export class HttpRequests {
* Combines provided extra {@link RequestInit} headers, class instance
* RequestInit headers and provided headers, prioritizing them in this order.
*
* @returns A new Headers object and a boolean indicating whether custom
* content type is provided.
* @returns A new Headers object or the main headers of this class if no
* headers are provided
*/
#getHeaders(
headers?: HeadersInit,
extraHeaders?: HeadersInit,
): { finalHeaders: Headers; isCustomContentTypeProvided: boolean } {
let isCustomContentTypeProvided: boolean;

#getHeaders(headers?: HeadersInit, extraHeaders?: HeadersInit): Headers {
if (headers !== undefined || extraHeaders !== undefined) {
headers = new Headers(headers);

Expand All @@ -194,16 +186,9 @@ export class HttpRequests {
}
}
}

isCustomContentTypeProvided =
headers.get("Content-Type") !== this.#defaultContentType;
} else {
isCustomContentTypeProvided = false;
}

const finalHeaders = headers ?? this.#requestInit.headers;

return { finalHeaders, isCustomContentTypeProvided };
return headers ?? this.#requestInit.headers;
}

/**
Expand All @@ -225,16 +210,12 @@ export class HttpRequests {
appendRecordToURLSearchParams(url.searchParams, params);
}

const { finalHeaders, isCustomContentTypeProvided } = this.#getHeaders(
headers,
extraRequestInit?.headers,
);
const finalHeaders = this.#getHeaders(headers, extraRequestInit?.headers);

const requestInit: RequestInit = {
method,
body:
// in case a custom content-type is provided do not stringify body
typeof body !== "string" || !isCustomContentTypeProvided
typeof body !== "string"
? // this will throw an error for any value that is not serializable
JSON.stringify(body)
: body,
Expand Down

0 comments on commit 5e0b1c3

Please sign in to comment.