Demo entry 2294987

XHR ES6 Class with Promise

   

Submitted by anonymous on Jul 21, 2015 at 10:04
Language: JavaScript. Code size: 2.1 kB.

export class XHR  {

    constructor (method) {
        this.method = method;
    }

    static get (URL, opt = {}) {
        let xhr = new XHR('GET');
        return xhr.request(URL, opt);
    }

    static post (URL, opt = {}) {
        let xhr = new XHR('POST');
        return xhr.request(URL, opt);
    }

    static put (URL, opt = {}) {
        let xhr = new XHR('put');
        return xhr.request(URL, opt);
    }

    static delete (URL, opt = {}) {
        let xhr = new XHR('DELETE');
        return xhr.request(URL, opt);
    }

    static format_params (params) {
        let pairs = Object.keys(params).map( (k) => {
            return `${k}=${params[k]}`;
        });
        return `?${pairs.join('&')}`;
    }

    request (URL, {params = null, data = null, auth = null, headers = null}) {
        return new Promise( (OK, ERR) => {

            let XHR = new XMLHttpRequest();

            XHR.onload = () => {
                if(XHR.status === 200){
                    OK(XHR.responseText);
                }else{
                    ERR(XHR.statusText);
                }
            };

            XHR.onerror = (err) => { ERR(err); };

            if (headers instanceof Object) {
                Object.keys(headers).forEach( (k) => {
                    XHR.setRequestHeader(k, headers[k]);
                });
            } else {
                XHR.setRequestHeader('Content-Type', 'text/plain');
                XHR.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            }

            if (params instanceof Object) {
                URL += this.format_params(params);
            }

            let opt = [this.method, URL, true];

            if (auth !== null) {
                opt.push(auth.user);
                opt.push(auth.password);
            }

            if (data instanceof Object) {
                data = JSON.stringify(data);
            }

            XHR.open(this.method, URL, true);
            XHR.send(data);

        });
    }

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).