<commit_before>require('es6-promise').polyfill();
require('isomorphic-fetch');
import getPort from '../../server/utils/getPort';
import reactCookie from 'react-cookie';

const BASE = process.env.BROWSER? '': `http://localhost:${getPort()}`;

const getUnAuthPromise = () => new Promise((resolve, reject) => {
  reject(new Error(
    'Calling authenticated required api without providing token'));
});

const wrapErrorHandler = (fetchPromise) => {
  return fetchPromise
    .then((json) => {
      if (json.isError) {
        console.error('[API Error]', json.errors);
        return Promise.reject(json.errors);
      }
      return Promise.resolve(json);
    });
};

export default {
  get: (path) => {
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'GET',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    }).then((res) => res.json()));
  },
  post: (path, param = {}) => {
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'POST',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(param),
    }).then((res) => res.json()));
  },
  delete: (path) => {
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'DELETE',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    }).then((res) => res.json()));
  },
  getAuth: (path) => {
    const token = reactCookie.load('token');
    if (!token) {
      return getUnAuthPromise();
    }
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'GET',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    }).then((res) => res.json()));
  },
  postAuth: (path, param = {}) => {
    const token = reactCookie.load('token');
    if (!token) {
      return getUnAuthPromise();
    }
    return wrapErrorHandler(fetch(path, {
      method: 'POST',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(param),
    }).then((res) => res.json()));
  },
};<commit_msg>Improve webAPIUtils<commit_after>require('es6-promise').polyfill();
require('isomorphic-fetch');
import getPort from '../../server/utils/getPort';
import reactCookie from 'react-cookie';

const BASE = process.env.BROWSER? '': `http://localhost:${getPort()}`;

const getUnAuthPromise = () => new Promise((resolve, reject) => {
  reject(new Error(
    'Calling authenticated required api without providing token'));
});

const wrapErrorHandler = (fetchPromise) => {
  return fetchPromise
    .then((json) => {
      if (json.isError) {
        console.error('[API Error]', json.errors);
        return Promise.reject(json.errors);
      }
      return Promise.resolve(json);
    });
};

export default {
  get: (path) => {
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'GET',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    }).then((res) => res.json()));
  },
  post: (path, param = {}) => {
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'POST',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(param),
    }).then((res) => res.json()));
  },
  delete: (path) => {
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'DELETE',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    }).then((res) => res.json()));
  },
  getAuth: (path) => {
    const token = reactCookie.load('token');
    const user = JSON.stringify(reactCookie.load('user'));
    if (!token) {
      return getUnAuthPromise();
    }
    return wrapErrorHandler(fetch(BASE + path, {
      method: 'GET',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Cookie: `token=${token};user=${user}`,
      },
    }).then((res) => res.json()));
  },
  postAuth: (path, param = {}) => {
    const token = reactCookie.load('token');
    const user = JSON.stringify(reactCookie.load('user'));
    if (!token) {
      return getUnAuthPromise();
    }
    return wrapErrorHandler(fetch(path, {
      method: 'POST',
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Cookie: `token=${token};user=${user}`,
      },
      body: JSON.stringify(param),
    }).then((res) => res.json()));
  },
};
{'fraction_non_alphanumeric': 0.18447785453067858, 'fraction_numerical': 0.0004063388866314506, 'mean_word_length': 2.78110599078341, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '5874589', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1494, 'n_words': 299}