<commit_before>var fetch = require("isomorphic-fetch");
var Util = require("./Util");

var uniqueCalls = [];

function removeCall(options) {
  uniqueCalls.splice(uniqueCalls.indexOf(options.url), 1);
}

var ajaxWrapper = function (opts = {}) {
  var defaults = {
    method: "GET",
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json"
    }
  };
  var options = Util.extendObject(defaults, opts);

  if (!options.concurrent) {
    if (uniqueCalls.indexOf(options.url) > -1) {
      return {
        error: function () { return this; },
        success: function () { return this; }
      };
    }
    uniqueCalls.push(options.url);
  }

  var response = {
    status: null,
    body: null
  };

  var makeRequest = function (options) {
    var fetchOptions = {
      method: options.method,
      headers: options.headers,
      credentials: "same-origin"
    };

    if (options.method !== "GET" && options.method !== "HEAD") {
      Object.assign(fetchOptions, {
        body: options.data != null
          ? JSON.stringify(options.data)
          : null
      });
    }

    return fetch(options.url, fetchOptions);
  };

  var parseResponse = function (xhr, callback) {
    response.status = xhr.status;
    xhr.json().then(
      function (body) {
        response.body = body;
      },
      function () {
        xhr.text().then(function (body) {
          response.body = body;
        });
      }
    ).then(function () {
      callback(response);
    });
  };

  var api = makeRequest(options);

  api.error = function (callback) {
    var promise = this;
    // Bind callback also for non 200 status
    promise.then(
      // not a 2* response<commit_msg>Test if xhr-object is an Error<commit_after>var fetch = require("isomorphic-fetch");
var Util = require("./Util");

var uniqueCalls = [];

function removeCall(options) {
  uniqueCalls.splice(uniqueCalls.indexOf(options.url), 1);
}

var ajaxWrapper = function (opts = {}) {
  var defaults = {
    method: "GET",
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json"
    }
  };
  var options = Util.extendObject(defaults, opts);

  if (!options.concurrent) {
    if (uniqueCalls.indexOf(options.url) > -1) {
      return {
        error: function () { return this; },
        success: function () { return this; }
      };
    }
    uniqueCalls.push(options.url);
  }

  var response = {
    status: null,
    body: null
  };

  var makeRequest = function (options) {
    var fetchOptions = {
      method: options.method,
      headers: options.headers,
      credentials: "same-origin"
    };

    if (options.method !== "GET" && options.method !== "HEAD") {
      Object.assign(fetchOptions, {
        body: options.data != null
          ? JSON.stringify(options.data)
          : null
      });
    }

    return fetch(options.url, fetchOptions);
  };

  var parseResponse = function (xhr, callback) {
    response.status = xhr.status;

    if (Util.isError(xhr)) {
      response.body = xhr.message;
      callback(response);
      return;
    }

    xhr.json().then(
      function (body) {
        response.body = body;
      },
      function () {
        xhr.text().then(function (body) {
          response.body = body;
        });
      }
    ).then(function () {
      callback(response);
    });
  };

  var api = makeRequest(options);

  api.error = function (callback) {
    var promise = this;
    // Bind callback also for non 200 status
    promise.then(
      // not a 2* response
{'fraction_non_alphanumeric': 0.1450747953711544, 'fraction_numerical': 0.003386960203217612, 'mean_word_length': 2.302889095992544, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '4307021', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1077, 'n_words': 242}