<commit_before>import * as fetch from 'node-fetch';
import * as querystring from 'querystring';
import {CookieJar} from 'tough-cookie';

interface fetchOpt {
  qs?: any;
  method?: 'GET'|'POST'|'PUT'|'DELETE';
  headers?: any;
  body?: ArrayBuffer | ArrayBufferView | Blob | FormData | string;
  mode?: 'same-origin' | 'no-cors' | 'cors';
  redirect?: 'follow' | 'error' | 'manual';
  credentials?: 'omit' | 'same-origin' | 'include';
  cache?: 'default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached';
}

export class FetchClient {
  private cookie = new CookieJar();
  constructor() {}

  private getCookie(currentUrl: string) {
    return new Promise((resolve, reject) => {
      this.cookie.getCookieString(currentUrl, (err, cookie) => {
        if (err) return reject(err);
        return resolve(cookie);
      })
    });
  }

  private setCookie(cookie: string, currentUrl: string) {
    return new Promise((resolve, reject) => {
      this.cookie.setCookie(cookie, currentUrl, (err, cookie) => {
        if (err) return reject(err);
        this.cookie = cookie;
        return resolve(cookie);
      });
    });
  }

  async fetch(url: string, opts: fetchOpt = {}) {
    if (!opts.headers) {
      opts.headers = {};
    }
    opts.headers.cookie = await this.getCookie(url);
    if (opts.qs) {
      url += `?${querystring.stringify(opts.qs)}`;
      opts.qs = undefined;
    }
    let res = await fetch(url, opts);
    await this.setCookie(await res.headers.get('Set-Cookie'), res.url);
    return res;
  }
}<commit_msg>FIX: fetch-client not working as intended<commit_after>import * as fetch from 'node-fetch';
import * as querystring from 'querystring';
import {CookieJar} from 'tough-cookie';

interface fetchOpt {
  qs?: any;
  method?: 'GET'|'POST'|'PUT'|'DELETE';
  headers?: any;
  body?: ArrayBuffer | ArrayBufferView | Blob | FormData | string;
  mode?: 'same-origin' | 'no-cors' | 'cors';
  redirect?: 'follow' | 'error' | 'manual';
  credentials?: 'omit' | 'same-origin' | 'include';
  cache?: 'default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached';
}

export class FetchClient {
  private cookie = new CookieJar();
  constructor() {}

  private getCookie(currentUrl: string) {
    return new Promise((resolve, reject) => {
      this.cookie.getCookieString(currentUrl, (err, cookie) => {
        if (err) return reject(err);
        return resolve(cookie);
      })
    });
  }

  private setCookie(cookie: string, currentUrl: string) {
    return new Promise((resolve, reject) => {
      this.cookie.setCookie(cookie, currentUrl, err => {
        if (err) return reject(err);
        return resolve();
      });
    });
  }

  async fetch(url: string, opts: fetchOpt = {}) {
    opts.headers = opts.headers || {};
    opts.headers.cookie = await this.getCookie(url);
    if (opts.qs) {
      url += `?${querystring.stringify(opts.qs)}`;
      opts.qs = undefined;
    }
    let res = await fetch(url, opts);
    let cookies = res.headers.getAll('set-cookie');
    for (let cookie of cookies) {
      await this.setCookie(cookie, url);
    }
    return res;
  }
}
{'fraction_non_alphanumeric': 0.16698534098151688, 'fraction_numerical': 0.0, 'mean_word_length': 3.224764468371467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4474329', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1016, 'n_words': 248}