got#ToughCookieJar TypeScript Examples

The following examples show how to use got#ToughCookieJar. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: request.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
export function newCookieJar(username: string): Got {
  const cookieFilename = getCookiePath(username);
  const fileExists = fs.existsSync(cookieFilename);
  if (fileExists) {
    let cookieData;
    try {
      cookieData = fs.readFileSync(cookieFilename, 'utf8');
      const cookieTest = JSON.parse(cookieData);
      if (Array.isArray(cookieTest)) {
        L.info(`Converting ${cookieFilename} cookie format`);
        const tcfsCookies = editThisCookieToToughCookieFileStore(cookieTest);
        fs.writeFileSync(cookieFilename, JSON.stringify(tcfsCookies), 'utf8');
      }
    } catch (err) {
      L.warn(err);
      L.warn({ cookieData }, `Could not parse ${cookieFilename}, deleting it`);
      fs.rmSync(cookieFilename, { force: true });
    }
  }

  return got.extend({
    cookieJar: getCookieJar(username) as ToughCookieJar,
    responseType: 'json',
  });
}