Skip to content

Commit

Permalink
only set content-type header with content (#1390)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-phylum authored Apr 8, 2024
1 parent c9d2df5 commit 9fa177b
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions extensions/phylum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export type RejectionSource = {
reason: string | null;
};

async function requestHeaders(headersInit?: HeadersInit): Promise<Headers> {
const headers = new Headers(headersInit);
async function ensureRequestHeaders(init: RequestInit) {
const headers = init.headers = new Headers(init.headers);

// Set Authorization header if it is missing.
if (!headers.has("Authorization")) {
Expand All @@ -74,11 +74,9 @@ async function requestHeaders(headersInit?: HeadersInit): Promise<Headers> {
}

// Set Content-Type header if it is missing.
if (!headers.has("Content-Type")) {
if (init.body && !headers.has("Content-Type")) {
headers.set("Content-Type", "application/json");
}

return headers;
}

export class PhylumApi {
Expand All @@ -98,7 +96,7 @@ export class PhylumApi {
// Ensure header object is initialized.
const fetchInit = init ?? {};

fetchInit.headers = await requestHeaders(fetchInit.headers);
await ensureRequestHeaders(fetchInit);

// Get API base URI without version.
const baseUrl = await PhylumApi.apiBaseUrl();
Expand Down

0 comments on commit 9fa177b

Please sign in to comment.