28 lines
785 B
TypeScript
28 lines
785 B
TypeScript
import nodeFetch, { RequestInit, Response } from 'node-fetch';
|
|
import { Cookie, CookieJar } from 'tough-cookie';
|
|
|
|
const cookieJar: CookieJar = new CookieJar();
|
|
|
|
function fetch(url: string, init: RequestInit = {}): Promise<Response> {
|
|
const headers: HeadersInit = {};
|
|
cookieJar.getCookiesSync(url).forEach((cookie: Cookie) => {
|
|
headers[cookie.key] = cookie.value;
|
|
});
|
|
const cookiedInit = {
|
|
...init,
|
|
...{ headers: { ...init.headers, ...headers } },
|
|
};
|
|
return nodeFetch(url, cookiedInit).then((res: Response) => {
|
|
setCookies(res.headers.raw()['set-cookie'], url);
|
|
return res;
|
|
});
|
|
}
|
|
|
|
function setCookies(header: string[], url: string): void {
|
|
header.forEach((cookie: string) => {
|
|
cookieJar.setCookieSync(cookie, url);
|
|
});
|
|
}
|
|
|
|
export default fetch;
|