<commit_before>import 'babel-polyfill';

const fetch = typeof window === 'object' ? window.fetch : global.fetch;

if (!fetch)
  throw new Error(
    'A global `fetch` method is required as either `window.fetch` '
    + 'for browsers or `global.fetch` for node runtime environments. '
    + 'Please add `require(\'isomorphic-fetch\')` before importing `frisbee`. '
    + 'You may optionally `require(\'es6-promise\').polyfill()` before you '
    + 'require `isomorphic-fetch` if you want to support older browsers.'
    + '\n\nFor more info: https://github.com/niftylettuce/frisbee#usage'
  );

const productionHost = 'buy.itunes.apple.com';
const sandboxHost = 'sandbox.itunes.apple.com';

const statusCodes = {
  [0]:     { message:'Active', valid: true, error: false },
  [21000]: { message:'App store could not read', valid: false, error: true },
  [21002]: { message:'Data was malformed', valid: false, error: true },
  [21003]: { message:'Receipt not authenticated', valid: false, error: true },
  [21004]: { message:'Shared secret does not match', valid: false, error: true },
  [21005]: { message:'Receipt server unavailable', valid: false, error: true },
  [21006]: { message:'Receipt valid but sub expired', valid: false, error: false },
  [21007]: { message:'Sandbox receipt sent to Production environment', valid: false, error: true, redirect: true }, // special case for app review handling - forward any request that is intended for the Sandbox but was sent to Production, this is what the app review team does
  [21008]: { message:'Production receipt sent to Sandbox environment', valid: false, error: true },
};

function VerificationError(error) {
  ['message', 'valid', 'error', 'redirect'].map(prop => this[prop] = error[prop]);
}
VerificationError.prototype = Object.create(Error.prototype);

export default function(password, production=true) {
  const endpoint = production ? productionHost : sandboxHost;
  const verifyUrl = `https://${endpoint}/verifyReceipt`;

  return async (receipt) => {
    const payload = {
      'receipt-data': receipt,
      password,
    };

    const options = {
      body: JSON.stringify(payload),
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    };

    const res = await fetch(verifyUrl, options);
    const body = await res.json();

    if (body.status !== 0) {
      throw new VerificationError(statusCodes[body.status]);
    }

    return receipt;
  }
}
<commit_msg>Return response body of request to apple<commit_after>import 'babel-polyfill';

const fetch = typeof window === 'object' ? window.fetch : global.fetch;

if (!fetch)
  throw new Error(
    'A global `fetch` method is required as either `window.fetch` '
    + 'for browsers or `global.fetch` for node runtime environments. '
    + 'Please add `require(\'isomorphic-fetch\')` before importing `frisbee`. '
    + 'You may optionally `require(\'es6-promise\').polyfill()` before you '
    + 'require `isomorphic-fetch` if you want to support older browsers.'
    + '\n\nFor more info: https://github.com/niftylettuce/frisbee#usage'
  );

const productionHost = 'buy.itunes.apple.com';
const sandboxHost = 'sandbox.itunes.apple.com';

const statusCodes = {
  [0]:     { message:'Active', valid: true, error: false },
  [21000]: { message:'App store could not read', valid: false, error: true },
  [21002]: { message:'Data was malformed', valid: false, error: true },
  [21003]: { message:'Receipt not authenticated', valid: false, error: true },
  [21004]: { message:'Shared secret does not match', valid: false, error: true },
  [21005]: { message:'Receipt server unavailable', valid: false, error: true },
  [21006]: { message:'Receipt valid but sub expired', valid: false, error: false },
  [21007]: { message:'Sandbox receipt sent to Production environment', valid: false, error: true, redirect: true }, // special case for app review handling - forward any request that is intended for the Sandbox but was sent to Production, this is what the app review team does
  [21008]: { message:'Production receipt sent to Sandbox environment', valid: false, error: true },
};

function VerificationError(error) {
  ['message', 'valid', 'error', 'redirect'].map(prop => this[prop] = error[prop]);
}
VerificationError.prototype = Object.create(Error.prototype);

export default function(password, production=true) {
  const endpoint = production ? productionHost : sandboxHost;
  const verifyUrl = `https://${endpoint}/verifyReceipt`;

  return async (receipt) => {
    const payload = {
      'receipt-data': receipt,
      password,
    };

    const options = {
      body: JSON.stringify(payload),
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    };

    const res = await fetch(verifyUrl, options);
    const body = await res.json();

    if (body.status !== 0) {
      throw new VerificationError(statusCodes[body.status]);
    }

    return body;
  }
}

{'fraction_non_alphanumeric': 0.1416, 'fraction_numerical': 0.0172, 'mean_word_length': 3.9270935960591133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '2072566', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1438, 'n_words': 470}