Created
November 3, 2016 12:30
-
-
Save MacKentoch/d003b9540d5eeac556df0efa64449f99 to your computer and use it in GitHub Desktop.
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 { | |
| getLocationOrigin | |
| } from '../fetchTools'; | |
| const BASE_URL = getLocationOrigin(); | |
| export const promisedHttpRequest = (endpoint, options, onProgressCallback) => { | |
| request('GET', `${BASE_URL}/${endpoint}`, options, onProgressCallback); | |
| }; | |
| function request(url, opts = {}, onProgress) { | |
| return new Promise( | |
| (res, rej) => { | |
| const xhr = new XMLHttpRequest(); | |
| xhr.open(opts.method || 'get', url); | |
| Object | |
| .keys(opts.headers || {}) | |
| .forEach( | |
| headerKey => { | |
| xhr.setRequestHeader(headerKey, opts.headers[headerKey]); | |
| } | |
| ); | |
| xhr.onload = e => res(e.target.responseText); | |
| xhr.onerror = rej; | |
| if (xhr.upload && onProgress) { | |
| xhr.upload.onprogress = onProgress; // event.loaded / event.total * 100 ; //event.lengthComputable | |
| } | |
| xhr.send(opts.body); | |
| } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is typescript version: