JSFiddle - React, Tailwind, and code Playground

by Oski Krawczyk

HTML

<script src="https://raw.github.com/michael/github/master/lib/base64.js"></script>

JavaScript

var options = {
    auth: 'basic',
    username: '[email protected]',
    password: 'makemylife'
};

var API_URL = 'https://api.github.com';

function _request(method, path, data, cb, raw) {
    function getURL() {
        var url = API_URL + path;
        return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
    }

    var xhr = new XMLHttpRequest();
    if (!raw) {
        xhr.dataType = "json";
    }

    xhr.open(method, getURL());
    xhr.onreadystatechange = function () {
        if (this.readyState == 4) {
            if (this.status >= 200 && this.status < 300 || this.status === 304) {
                cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
            } else {
                cb({
                    request: this,
                    error: this.status
                });
            }
        }
    };
    xhr.setRequestHeader('Accept', 'application/vnd.github.raw');
    xhr.setRequestHeader('Content-Type', 'application/json');
    if (
    (options.auth == 'oauth' && options.token) || (options.auth == 'basic' && options.username && options.password)) {
        var b64 = Base64.encode(options.username + ':' + options.password);
        xhr.setRequestHeader('Authorization', options.auth == 'oauth' ? 'token ' + options.token : 'Basic ' + b64);
    }
    data ? xhr.send(JSON.stringify(data)) : xhr.send();
}

_request(
    "GET",
    "/user/repos?type=all&per_page=1000&sort=updated",
null,
function (err, res) {
    console.log(err, res);
});