<commit_before>import fetch from 'isomorphic-fetch';

import local from '../utilities/local';

const REQUEST_METHODS = {
  DELETE: 'DELETE',
  GET: 'GET',
  PATCH: 'PATCH',
  POST: 'POST',
};

class Base {
  constructor () {
    const { host, origin } = global.window.location;

    this.baseURL = `${origin}/api`;
    this.websocketBaseURL = `wss://${host}/api`;
    this.bearerToken = local.getItem('auth_token');
  }

  static _request (method, endpoint, body, overrideHeaders) {
    const credentials = 'same-origin';
    const { GET } = REQUEST_METHODS;
    const headers = {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      ...overrideHeaders,
    };
    const requestAttrs = method === GET
      ? { credentials, method, headers }
      : { credentials, method, body, headers };

    return fetch(endpoint, requestAttrs)
      .then((response) => {
        return response.json()
          .then((jsonResponse) => {
            if (response.ok) {
              return jsonResponse;
            }

            const error = new Error(response.statusText);
            error.response = jsonResponse;
            error.message = jsonResponse;
            error.error = jsonResponse.error;

            throw error;
          });
      });
  }

  static post (endpoint, body = {}, overrideHeaders = {}) {
    const { POST } = REQUEST_METHODS;

    return Base._request(POST, endpoint, body, overrideHeaders);
  }

  endpoint (pathname) {
    return this.baseURL + pathname;
  }

  setBearerToken (bearerToken) {
    this.bearerToken = bearerToken;
  }

  authenticatedDelete (endpoint, overrideHeaders = {}) {
    const { DELETE } = REQUEST_METHODS;

    return this._authenticatedRequest(DELETE, endpoint, {}, overrideHeaders);
  }

  authenticatedGet (endpoint, overrideHeaders = {}) {
    const { GET } = REQUEST_METHODS;

    return this._authenticatedRequest(GET, endpoint, {}, overrideHeaders);
  }

  authenticatedPatch (endpoint, body = {}, overrideHeaders = {}) {
    const { PATCH } = REQUEST_METHODS;

    return this._authenticatedRequest(PATCH, endpoint, body, overrideHeaders);
  }

  authenticatedPost (endpoint, body = {}, overrideHeaders = {}) {
    const { POST } = REQUEST_METHODS;

    return this._authenticatedRequest(POST, endpoint, body, overrideHeaders);
  }

  _authenticatedHeaders = (headers) => {
    return {
      ...headers,
      Authorization: `Bearer ${this.bearerToken}`,
    };
  }

  _authenticatedRequest(method, endpoint, body, overrideHeaders) {
    const headers = this._authenticatedHeaders(overrideHeaders);

    return Base._request(method, endpoint, body, headers);
  }
}

export default Base;

<commit_msg>Fix logout bug on Safari (#787)<commit_after>import fetch from 'isomorphic-fetch';

import local from '../utilities/local';

const REQUEST_METHODS = {
  DELETE: 'DELETE',
  GET: 'GET',
  PATCH: 'PATCH',
  POST: 'POST',
};

class Base {
  constructor () {
    const { host, origin } = global.window.location;

    this.baseURL = `${origin}/api`;
    this.websocketBaseURL = `wss://${host}/api`;
    this.bearerToken = local.getItem('auth_token');
  }

  static _request (method, endpoint, body, overrideHeaders) {
    const credentials = 'same-origin';
    const { GET } = REQUEST_METHODS;
    const headers = {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      ...overrideHeaders,
    };
    const requestAttrs = method === GET
      ? { credentials, method, headers }
      : { credentials, method, body, headers };

    return fetch(endpoint, requestAttrs)
      .then((response) => {
        return response.json()
          .then((jsonResponse) => {
            if (response.ok) {
              return jsonResponse;
            }

            const error = new Error(response.statusText);
            error.response = jsonResponse;
            error.message = jsonResponse;
            error.error = jsonResponse.error;

            throw error;
          });
      });
  }

  static post (endpoint, body = {}, overrideHeaders = {}) {
    const { POST } = REQUEST_METHODS;

    return Base._request(POST, endpoint, body, overrideHeaders);
  }

  endpoint (pathname) {
    return this.baseURL + pathname;
  }

  setBearerToken (bearerToken) {
    this.bearerToken = bearerToken;
  }

  authenticatedDelete (endpoint, overrideHeaders = {}) {
    const { DELETE } = REQUEST_METHODS;

    return this._authenticatedRequest(DELETE, endpoint, {}, overrideHeaders);
  }

  authenticatedGet (endpoint, overrideHeaders = {}) {
    const { GET } = REQUEST_METHODS;

    return this._authenticatedRequest(GET, endpoint, {}, overrideHeaders);
  }

  authenticatedPatch (endpoint, body = {}, overrideHeaders = {}) {
    const { PATCH } = REQUEST_METHODS;

    return this._authenticatedRequest(PATCH, endpoint, body, overrideHeaders);
  }

  authenticatedPost (endpoint, body = JSON.stringify({}), overrideHeaders = {}) {
    const { POST } = REQUEST_METHODS;

    return this._authenticatedRequest(POST, endpoint, body, overrideHeaders);
  }

  _authenticatedHeaders = (headers) => {
    return {
      ...headers,
      Authorization: `Bearer ${this.bearerToken}`,
    };
  }

  _authenticatedRequest(method, endpoint, body, overrideHeaders) {
    const headers = this._authenticatedHeaders(overrideHeaders);

    return Base._request(method, endpoint, body, headers);
  }
}

export default Base;


{'fraction_non_alphanumeric': 0.12823355506282336, 'fraction_numerical': 0.0005543237250554324, 'mean_word_length': 2.9713866471019807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6402021', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1539, 'n_words': 350}