Skip to content

Commit

Permalink
feat: add header to request
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Aug 5, 2024
1 parent 2e42ff3 commit 5686858
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@ export const getCookiesFromResponse = (response: Response): string[] => {
export const getHeaderFromResponse = (response: Response, item: string): string | null => {
const headers = response.headers;

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

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

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

if (isHeader(request.headers)) {
request.headers.set(key, value);
}
else {
request.headers[key] = value;
}
}

export interface Request {
url: string

Expand Down

0 comments on commit 5686858

Please sign in to comment.