Skip to content

Commit

Permalink
feat: add set cookies to request
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Aug 17, 2024
1 parent d37fbe1 commit 992f55c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@ export const getCookiesFromResponse = (response: Response): string[] => {
export const getHeaderFromResponse = (response: Response, item: string): string | null => {
const headers = response.headers;

return isHeader(headers)
return isHeaderInstance(headers)
? headers.get(item)
: (headers as Record<string, string>)[item];
};

const isHeader = (headers: Response["headers"]): headers is Headers => {
const isHeaderInstance = (headers: Response["headers"]): headers is Headers => {
return typeof headers.get === "function";
}

export const setCookiesArrayToRequest = (request: Request, cookies: string[]): void => {
setHeaderToRequest(request, "Cookie", cookies.join("; "))
}

export const setCookiesObjectToRequest = (request: Request, cookies: Record<string, string>): void => {
const array = Object.entries(cookies).map(([key, value])=>`${key}=${value}`)
setCookiesArrayToRequest(request, array)
}

export const setHeaderToRequest = (request: Request, key: string, value: string): void => {
if (!request.headers) request.headers = {};
request.headers ||= {};

if (isHeader(request.headers)) {
if (isHeaderInstance(request.headers)) {
request.headers.set(key, value);
}
else {
Expand Down

0 comments on commit 992f55c

Please sign in to comment.