xhr ({ method, url, data, done, timeout }) {
    let req = new XHR;
    const payload = qs.stringify(data, { encodeValuesOnly: true });

    if (method === 'get') {
      url += `?${payload}`;
    }

    req.open(method, url);
    req.timeout = timeout;
    req.ontimeout = () => done(errors('api-timeout'));
    req.onerror = () => done(errors('api-error'));
    req.onprogress = NOOP;
    req.onload = function () {
      let res;
      try {
        if (this.responseText) res = JSON.parse(this.responseText);
      } catch (e) {
        debug(e, this.responseText);
        return done(errors('api-error', {
          message: `There was a problem parsing the API response with: ${this.responseText}`
        }));
      }

      if (res && res.error) {
        done(errors('api-error', res.error));
      } else {
        done(null, res);
      }
    };

    // XDR requests will abort if too many are sent simultaneously
    setTimeout(() => {
      if (method === 'post') {
        // XDR cannot set Content-type
        if (req.setRequestHeader) {
          req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        }
        req.send(payload);
      } else {
        req.send();
      }
    }, 0);
  }
/**
   * Issues an API request over xhr.
   *
   * @param {String} method
   * @param {String} url
   * @param {Object} [data]
   * @param {Function} done
   * @private
   */
Issues an API request over xhr.
Issues an API request over xhr .
xhr ({ method, url, data, done, timeout }) {
    let req = new XHR;
    const payload = qs.stringify(data, { encodeValuesOnly: true });
    if (method === 'get') {
      url += `?${payload}`;
    }
    req.open(method, url);
    req.timeout = timeout;
    req.ontimeout = () => done(errors('api-timeout'));
    req.onerror = () => done(errors('api-error'));
    req.onprogress = NOOP;
    req.onload = function () {
      let res;
      try {
        if (this.responseText) res = JSON.parse(this.responseText);
      } catch (e) {
        debug(e, this.responseText);
        return done(errors('api-error', {
          message: `There was a problem parsing the API response with: ${this.responseText}`
        }));
      }
      if (res && res.error) {
        done(errors('api-error', res.error));
      } else {
        done(null, res);
      }
    };
    setTimeout(() => {
      if (method === 'post') {
        if (req.setRequestHeader) {
          req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        }
        req.send(payload);
      } else {
        req.send();
      }
    }, 0);
  }
xhr ( { method , url , data , done , timeout } ) { let req = new XHR ; const payload = qs . stringify ( data , { encodeValuesOnly : true } ) ; if ( method === 'get' ) { url += ` ${ payload } ` ; } req . open ( method , url ) ; req . timeout = timeout ; req . ontimeout = ( ) => done ( errors ( 'api-timeout' ) ) ; req . onerror = ( ) => done ( errors ( 'api-error' ) ) ; req . onprogress = NOOP ; req . onload = function ( ) { let res ; try { if ( this . responseText ) res = JSON . parse ( this . responseText ) ; } catch ( e ) { debug ( e , this . responseText ) ; return done ( errors ( 'api-error' , { message : ` ${ this . responseText } ` } ) ) ; } if ( res && res . error ) { done ( errors ( 'api-error' , res . error ) ) ; } else { done ( null , res ) ; } } ; setTimeout ( ( ) => { if ( method === 'post' ) { if ( req . setRequestHeader ) { req . setRequestHeader ( 'Content-type' , 'application/x-www-form-urlencoded' ) ; } req . send ( payload ) ; } else { req . send ( ) ; } } , 0 ) ; }
Issues an API request over xhr.
Issues an API request over xhr .
// XDR requests will abort if too many are sent simultaneously // XDR cannot set Content-type