Created
March 22, 2015 01:38
-
-
Save Alxandr/d8d54afc7a2bf0e562fa to your computer and use it in GitHub Desktop.
Http client
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
| import 'whatwg-fetch'; | |
| let uuid = 0; | |
| function getCallbackName() { | |
| // Note: This does not run in node without a DOM | |
| // But, if you're in node, why are you using JSONP??? | |
| let name; | |
| do { | |
| name = `jsonp_callback_${uuid++}`; | |
| } while (window[name]); | |
| } | |
| export class MessageHandler { | |
| send(uri, opts) { | |
| throw new Error('MessageHandler.handle must be implemented'); | |
| } | |
| } | |
| export class DefaultMessageHandler { | |
| send(uri, opts) { | |
| return fetch(uri, opts); | |
| } | |
| } | |
| export class DelegatingMessageHandler extends MessageHandler { | |
| constructor(next) { | |
| this.next = next; | |
| } | |
| send(uri, opts) { | |
| return this.next(uri, opts); | |
| } | |
| } | |
| export class SimpleClient extends MessageHandler { | |
| constructor(handler = new DefaultMessageHandler()) { | |
| this.handler = handler; | |
| // TODO: Add default values to stuff like common headers etc. | |
| } | |
| send(uri, opts = {}) { | |
| // TODO: Apply default values for headers and base URI etc. | |
| return this.handler.send(uri, opts); | |
| } | |
| /** | |
| * Sends an HTTP DELETE request. | |
| * | |
| * @method delete | |
| * @param {String} uri The target URI. | |
| * @param {Object} opts Request options | |
| * @return {Promise} A promise. | |
| */ | |
| delete(uri, opts = {}) { | |
| return this.send(uri, {...opts, method: 'delete'}); | |
| } | |
| /** | |
| * Sends an HTTP GET request. | |
| * | |
| * @method get | |
| * @param {String} uri The target URI. | |
| * @param {Object} opts Request options | |
| * @return {Promise} A promise. | |
| */ | |
| get(uri, opts = {}) { | |
| return this.send(uri, {...opts, method: 'get'}); | |
| } | |
| /** | |
| * Sends an HTTP HEAD request. | |
| * | |
| * @method head | |
| * @param {String} uri The target URI. | |
| * @param {Object} opts Request options | |
| * @return {Promise} A promise. | |
| */ | |
| head(uri, opts = {}) { | |
| return this.send(uri, {...opts, method: 'head'}); | |
| } | |
| /** | |
| * Sends an HTTP OPTIONS request. | |
| * | |
| * @method options | |
| * @param {String} uri The target URI. | |
| * @param {Object} opts Request options | |
| * @return {Promise} A promise. | |
| */ | |
| options(uri, opts = {}) { | |
| return this.send(uri, {...opts, method: 'options'}); | |
| } | |
| /** | |
| * Sends an HTTP PUT request. | |
| * | |
| * @method put | |
| * @param {String} uri The target URI. | |
| * @param {Object} opts Request put | |
| * @return {Promise} A promise. | |
| */ | |
| put(uri, opts = {}) { | |
| return this.send(uri, {...opts, method: 'put'}); | |
| } | |
| /** | |
| * Sends an HTTP PATCH request. | |
| * | |
| * @method patch | |
| * @param {String} uri The target URI. | |
| * @param {Object} opts Request patch | |
| * @return {Promise} A promise. | |
| */ | |
| patch(uri, opts = {}) { | |
| return this.send(uri, {...opts, method: 'patch'}); | |
| } | |
| /** | |
| * Sends an HTTP POST request. | |
| * | |
| * @method post | |
| * @param {String} uri The target URI. | |
| * @param {Object} opts Request post | |
| * @return {Promise} A promise. | |
| */ | |
| post(uri, opts = {}) { | |
| return this.send(uri, {...opts, method: 'post'}); | |
| } | |
| jsonp(uri, callbackParameterName = 'jsoncallback') { | |
| return new Promise((resolve, reject) => { | |
| let resolved = false; | |
| const callbackName = makeCallbackName(); | |
| window[callbackName] = (data) => { | |
| resolved = true; | |
| resolve(data); | |
| delete window[callbackName]; | |
| }; | |
| const script = document.createElement('script'); | |
| script.src = uri; | |
| document.body.appendChild(script); | |
| if (this.timeout && this.timeout > 0) { | |
| setTimeout(() => { | |
| reject(new Error('timeout')); | |
| }, this.timeout); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment