mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
Refactor setting cookie header into own method
This commit is contained in:
parent
5009f9d3f2
commit
0a5cdfc57a
@ -5,13 +5,17 @@ import { Cookie, CookieJar } from 'tough-cookie';
|
|||||||
|
|
||||||
const cookieJar = new CookieJar();
|
const cookieJar = new CookieJar();
|
||||||
|
|
||||||
function addCookieHandler(url, params) {
|
function setCookieHeader(url, params) {
|
||||||
// add cookie header, if we have one in the jar
|
// add cookie header, if we have one in the jar
|
||||||
const existingCookie = cookieJar.getCookieStringSync(url.toString());
|
const existingCookie = cookieJar.getCookieStringSync(url.toString());
|
||||||
if (existingCookie) {
|
if (existingCookie) {
|
||||||
params.headers = params.headers ?? {};
|
params.headers = params.headers ?? {};
|
||||||
params.headers.Cookie = existingCookie;
|
params.headers.Cookie = existingCookie;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addCookieHandler(url, params) {
|
||||||
|
setCookieHeader(url, params);
|
||||||
|
|
||||||
// handle cookies during redirects
|
// handle cookies during redirects
|
||||||
params.beforeRedirect = (options, responseInfo) => {
|
params.beforeRedirect = (options, responseInfo) => {
|
||||||
@ -30,9 +34,7 @@ function addCookieHandler(url, params) {
|
|||||||
cookieJar.setCookieSync(cookies[i], options.href);
|
cookieJar.setCookieSync(cookies[i], options.href);
|
||||||
}
|
}
|
||||||
|
|
||||||
const cookie = cookieJar.getCookieStringSync(options.href);
|
setCookieHeader(options.href, options);
|
||||||
options.headers = options.headers ?? {};
|
|
||||||
options.headers.Cookie = cookie;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user