Skip to content

Commit

Permalink
chore(internal): add helper function for b64 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and RobertCraigie committed Jul 13, 2023
1 parent 42fc4a9 commit 04e303c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,19 @@ export const getHeader = (headers: HeadersLike, key: string): string | null | un
}
return value;
};

/**
* Encodes a string to Base64 format.
*/
export const toBase64 = (str: string | null | undefined): string => {
if (!str) return '';
if (typeof Buffer !== 'undefined') {
return Buffer.from(str).toString('base64');
}

if (typeof btoa !== 'undefined') {
return btoa(str);
}

throw new Error('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
};

0 comments on commit 04e303c

Please sign in to comment.