Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created December 22, 2014 12:03
Show Gist options
  • Select an option

  • Save roman01la/57c3456ffb2601f45bb1 to your computer and use it in GitHub Desktop.

Select an option

Save roman01la/57c3456ffb2601f45bb1 to your computer and use it in GitHub Desktop.
Calling REST API with ES6 Proxies
var EndPoint = function (baseUrl) {
var request = function (type, url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(type, url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onload = function() {
if (xhr.status === 200) {
resolve(xhr.response);
} else {
reject(xhr.status);
}
};
});
};
return new Proxy({}, {
get (target, prop, receiver) {
return request(baseUrl + '/' + prop);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment