add a proper documentation
featuring an actually working usage example! also: change exports in index.mjs for easier import
This commit is contained in:
@ -35,7 +35,7 @@ export default class CookieJar {
|
||||
// only save cookies that haven't expired
|
||||
let cookiesToSave = new Map();
|
||||
this.forEach(cookie => {
|
||||
if(cookie.expiry && cookie.expiry > new Date())
|
||||
if(!cookie.hasExpired())
|
||||
cookiesToSave.set(cookie.name, cookie);
|
||||
});
|
||||
fs.writeFileSync(this.file, JSON.stringify([...cookiesToSave]));
|
||||
|
@ -90,8 +90,11 @@ export default class Cookie {
|
||||
serialize() {
|
||||
return this.name + "=" + this.value;
|
||||
}
|
||||
hasExpired() {
|
||||
return this.expiry && this.expiry < new Date();
|
||||
}
|
||||
isValidForRequest(url) {
|
||||
if(this.expiry && this.expiry < new Date())
|
||||
if(this.hasExpired())
|
||||
return false;
|
||||
const parsedURL = urlParser.parse(url);
|
||||
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:")
|
||||
|
@ -2,54 +2,52 @@ import fetch from "node-fetch";
|
||||
import CookieJar from "./cookie-jar";
|
||||
import Cookie from "./cookie";
|
||||
|
||||
export default {
|
||||
fetch: async (url, options, cookieJars) => {
|
||||
let cookies = "";
|
||||
if(Array.isArray(cookieJars) && cookieJars.every(c => c instanceof CookieJar)) {
|
||||
cookieJars.forEach(jar => {
|
||||
if(!jar.flags.includes("r"))
|
||||
return;
|
||||
jar.forEach(c => {
|
||||
if(c.isValidForRequest(url))
|
||||
cookies += c.serialize() + "; ";
|
||||
});
|
||||
});
|
||||
}
|
||||
else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("r")) {
|
||||
cookieJars.forEach(c => {
|
||||
async function cookieFetch(cookieJars, url, options) {
|
||||
let cookies = "";
|
||||
if(Array.isArray(cookieJars) && cookieJars.every(c => c instanceof CookieJar)) {
|
||||
cookieJars.forEach(jar => {
|
||||
if(!jar.flags.includes("r"))
|
||||
return;
|
||||
jar.forEach(c => {
|
||||
if(c.isValidForRequest(url))
|
||||
cookies += c.serialize() + "; ";
|
||||
});
|
||||
});
|
||||
}
|
||||
else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("r")) {
|
||||
cookieJars.forEach(c => {
|
||||
if(c.isValidForRequest(url))
|
||||
cookies += c.serialize() + "; ";
|
||||
});
|
||||
}
|
||||
else
|
||||
throw new TypeError("Third paramter is neither a cookie jar nor an array of cookie jars!");
|
||||
if(cookies.length !== 0) {
|
||||
if(!options) {
|
||||
options = {
|
||||
headers: {}
|
||||
};
|
||||
}
|
||||
else
|
||||
throw new TypeError("Third paramter is neither a cookie jar nor an array of cookie jars!");
|
||||
if(cookies.length !== 0) {
|
||||
if(!options) {
|
||||
options = {
|
||||
headers: {}
|
||||
};
|
||||
}
|
||||
if(!options.headers)
|
||||
options.headers = {};
|
||||
options.headers.cookie = cookies.slice(0, -2);
|
||||
else if(!options.headers)
|
||||
options.headers = {};
|
||||
options.headers.cookie = cookies.slice(0, -2);
|
||||
}
|
||||
const result = await fetch(url, options);
|
||||
// i cannot use headers.get() here because it joins the cookies to a string
|
||||
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
|
||||
if(cookies) {
|
||||
if(Array.isArray(cookieJars)) {
|
||||
cookieJars.forEach(jar => {
|
||||
if(!jar.flags.includes("w"))
|
||||
return;
|
||||
cookies.forEach(c => jar.addCookie(c, url));
|
||||
});
|
||||
}
|
||||
const result = await fetch(url, options);
|
||||
// i cannot use headers.get() here because it joins the cookies to a string
|
||||
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
|
||||
if(cookies) {
|
||||
if(Array.isArray(cookieJars)) {
|
||||
cookieJars.forEach(jar => {
|
||||
if(!jar.flags.includes("w"))
|
||||
return;
|
||||
cookies.forEach(c => jar.addCookie(c, url));
|
||||
});
|
||||
}
|
||||
else if(cookieJars.flags.includes("w")) {
|
||||
cookies.forEach(c => cookieJars.addCookie(c, url));
|
||||
}
|
||||
else if(cookieJars.flags.includes("w")) {
|
||||
cookies.forEach(c => cookieJars.addCookie(c, url));
|
||||
}
|
||||
return result;
|
||||
},
|
||||
CookieJar: CookieJar,
|
||||
Cookie: Cookie
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export {cookieFetch as fetch, CookieJar, Cookie};
|
||||
|
Reference in New Issue
Block a user