build/net.js
// Define the defaults for all requests
Define the defaults for all requests
Define the defaults for all requests
function NetRequest(method, address, data, headers) {
    var _this = this;
    _classCallCheck(this, NetRequest);
    this.request = new XMLHttpRequest();
    this.response = null;
    if (__NET_ALLOWED_METHODS.indexOf(method) < 0) {
        console.warn('Sorry, \'' + method + '\' is not a supported HTTP method');
        return false;
    }
    if (!(typeof address === 'string') && address.length > 0) {
        console.warn('Sorry, \'' + address + '\' is not a supported HTTP address');
        return false;
    }
    return new Promise(function (resolve, reject) {
        if ("withCredentials" in _this.request) {
            _this.request.open(method, address, true); 
        } else if (typeof XDomainRequest != "undefined") {
                _this.request = new XDomainRequest();
                _this.request.open(method, address);
            } else {
                _this.request = null;
            };
        if (!_this.request) {
            return;
        };
        if (typeof headers === 'object') {
            if ("withCredentials" in _this.request) {
                for (var header in headers) {
                    _this.request.setRequestHeader(header, headers[header]);
                }
            } else {
                console.warn("Custom headers cannot be added in IE9 and lower");
            }
        }
        _this.request.onload = function () {
            _this.response = new NetResponse(_this.request);
            resolve(_this.response);
        };
        _this.request.onerror = function () {
            _this.response = new NetResponse(_this.request);
            reject(_this.response);
        };
        try {
            if (typeof data === 'object') {
                _this.request.send(JSON.stringify(data));
            } else {
                _this.request.send();
            }
        } catch (e) {
            console.warn('Failed to send request', e);
        }
    });
}
function NetRequest ( method , address , data , headers ) { var _this = this ; _classCallCheck ( this , NetRequest ) ; this . request = new XMLHttpRequest ( ) ; this . response = null ; if ( __NET_ALLOWED_METHODS . indexOf ( method ) < 0 ) { console . warn ( 'Sorry, \'' + method + '\' is not a supported HTTP method' ) ; return false ; } if ( ! ( typeof address === 'string' ) && address . length > 0 ) { console . warn ( 'Sorry, \'' + address + '\' is not a supported HTTP address' ) ; return false ; } return new Promise ( function ( resolve , reject ) { if ( "withCredentials" in _this . request ) { _this . request . open ( method , address , true ) ; } else if ( typeof XDomainRequest != "undefined" ) { _this . request = new XDomainRequest ( ) ; _this . request . open ( method , address ) ; } else { _this . request = null ; } ; if ( ! _this . request ) { return ; } ; if ( typeof headers === 'object' ) { if ( "withCredentials" in _this . request ) { for ( var header in headers ) { _this . request . setRequestHeader ( header , headers [ header ] ) ; } } else { console . warn ( "Custom headers cannot be added in IE9 and lower" ) ; } } _this . request . onload = function ( ) { _this . response = new NetResponse ( _this . request ) ; resolve ( _this . response ) ; } ; _this . request . onerror = function ( ) { _this . response = new NetResponse ( _this . request ) ; reject ( _this . response ) ; } ; try { if ( typeof data === 'object' ) { _this . request . send ( JSON . stringify ( data ) ) ; } else { _this . request . send ( ) ; } } catch ( e ) { console . warn ( 'Failed to send request' , e ) ; } } ) ; }
Define the defaults for all requests
Define the defaults for all requests
// Make a new request with the data provided // Check method valid // Check valid URL // Return a Promise to the Caller // XMLHttpRequest for Chrome/Firefox/Opera/Safari. // NOTE: Do we want to provide synchronous support? // XDomainRequest for IE. // CORS not supported. // For all the headers, add them to the request // Define when to call resolve and when to call reject // Send the request!