Skip to content

Commit

Permalink
refactor: split out falsy header filter func
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta committed Dec 12, 2024
1 parent beb81aa commit ff89afd
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/lib/readmeAPIFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ export async function handleAPIv1Res(res: Response, rejectOnJsonError = true) {
return Promise.reject(body);
}

/**
* If you supply `undefined` or `null` to the `Headers` API,
* it'll convert those to a string by default,
* so we instead filter those out here.
*/
function filterOutFalsyHeaders(inputHeaders: Headers) {
const headers = new Headers();

for (const header of inputHeaders.entries()) {
if (header[1] !== 'null' && header[1] !== 'undefined' && header[1].length > 0) {
headers.set(header[0], header[1]);
}
}

return headers;
}

/**
* Returns the basic auth header and any other defined headers for use in `fetch` calls against ReadMe API v1.
*
Expand All @@ -252,24 +269,14 @@ export function cleanAPIv1Headers(
key: string,
/** used for `x-readme-header` */
version?: string,
inputHeaders: Headers = new Headers(),
headers: Headers = new Headers(),
) {
const encodedKey = Buffer.from(`${key}:`).toString('base64');
const headers = new Headers({
Authorization: `Basic ${encodedKey}`,
});
headers.set('Authorization', `Basic ${encodedKey}`);

if (version) {
headers.set('x-readme-version', version);
}

for (const header of inputHeaders.entries()) {
// If you supply `undefined` or `null` to the `Headers` API it'll convert those to a string by default,
// so we instead filter those out here.
if (header[1] !== 'null' && header[1] !== 'undefined' && header[1].length > 0) {
headers.set(header[0], header[1]);
}
}

return headers;
return filterOutFalsyHeaders(headers);
}

0 comments on commit ff89afd

Please sign in to comment.