<commit_before>/* global location */

const $ = require('jquery');

const Keys = require('keys/dist/client');

module.exports = opt => $.ajax(Object.assign(
  {}, opt, {
    type: opt.type || 'post',

    beforeSend: () => Keys.clearAlerts(),

    success: data => {
      const { redirect } = opt;

      switch (redirect || 'auto') {
        case 'ignore':
          break;

        case 'refresh-instead':
          location.reload();
          break;

        case 'follow':
          if (!data.redirect) {
            console.error('Missing redirect property:', data);
            Keys.flagServerError();

            return;
          }

        case 'auto':
          if (data.redirect) {
            location.href = data.redirect;
          }

          break;

        default:
          throw new Error(`Unknown redirect policy: ${redirect}`);
      }

      if (opt.success) {
        opt.success(data);
      }

      if (opt.always) {
        opt.always(data);
      }
    },

    error: xhr => {
      const data = xhr.responseJSON;

      if (!data || !data.userMsg) {
        Keys.flagServerError();
        return;
      }

      Keys.pushAlert('red', data.userMsg);

      if (opt.error) {
        opt.error(data, xhr);
      }

      if (opt.always) {
        opt.always(data);
      }
    },
  }
));
<commit_msg>Implement 'pushAlert' stdAjax response functionality.<commit_after>/* global location */

const $ = require('jquery');

const Keys = require('keys/dist/client');

module.exports = opt => $.ajax(Object.assign(
  {}, opt, {
    type: opt.type || 'post',

    beforeSend: () => Keys.clearAlerts(),

    success: data => {
      const { redirect } = opt;

      switch (redirect || 'auto') {
        case 'ignore':
          break;

        case 'refresh-instead':
          location.reload();
          break;

        case 'follow':
          if (!data.redirect) {
            console.error('Missing redirect property:', data);
            Keys.flagServerError();

            return;
          }

        case 'auto':
          if (data.redirect) {
            location.href = data.redirect;
          }

          break;

        default:
          throw new Error(`Unknown redirect policy: ${redirect}`);
      }

      if (data.pushAlert) {
        const { color, message } = data.pushAlert;

        Keys.pushAlert(color || 'blue', message);
      }

      if (opt.success) {
        opt.success(data);
      }

      if (opt.always) {
        opt.always(data);
      }
    },

    error: xhr => {
      const data = xhr.responseJSON;

      if (!data || !data.userMsg) {
        Keys.flagServerError();
        return;
      }

      Keys.pushAlert('red', data.userMsg);

      if (opt.error) {
        opt.error(data, xhr);
      }

      if (opt.always) {
        opt.always(data);
      }
    },
  }
));

{'fraction_non_alphanumeric': 0.1487864931410482, 'fraction_numerical': 0.0, 'mean_word_length': 1.6187845303867403, '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': '474424', 'n_tokens_mistral': 868, 'n_tokens_neox': 823, 'n_words': 175}