Created
December 22, 2014 12:03
-
-
Save roman01la/57c3456ffb2601f45bb1 to your computer and use it in GitHub Desktop.
Calling REST API with ES6 Proxies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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