-
-
Save scottgonzalez/cc85abedbca0ae4e9f47 to your computer and use it in GitHub Desktop.
This file contains 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
simpleXhr({ | |
url: "data.php", | |
timeout: 5000 | |
}) | |
.setRequestHeader( "foo", "bar" ) | |
.send() | |
.then(function( xhr ) { | |
console.log( "Success" ); | |
console.log( typeof xhr.response, xhr.response ); | |
}, function( error ) { | |
console.log( "Error" ); | |
console.log( error ); | |
}); | |
//======================================== | |
xhr = simpleXhr({ | |
url: "data.php", | |
timeout: 5000, | |
headers: { | |
foo: "bar" | |
} | |
}) | |
.send(); | |
xhr.then(function( xhr ) { | |
console.log( "Success" ); | |
console.log( typeof xhr.response, xhr.response ); | |
}, function( error ) { | |
console.log( "Error" ); | |
console.log( error ); | |
}); | |
setTimeout(function() { | |
xhr.abort(); | |
}, 500 ); |
This file contains 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
// method | |
// username | |
// password | |
// url | |
function cleanXhr( options ) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open( options.method || "GET", options.url, true, options.username, options.password ); | |
// Set proxied properties directly on the XHR object | |
cleanXhr.proxyProperties.forEach(function( property ) { | |
if ( options.hasOwnProperty( property ) ) { | |
xhr[ property ] = options[ property ]; | |
} | |
}); | |
// Create a promise that resolves with the XHR | |
var promise = new Promise(function( resolve, reject ) { | |
xhr.onload = function() { | |
resolve( xhr ); | |
}; | |
[ "abort", "error", "timeout" ].forEach(function( action ) { | |
xhr[ "on" + action ] = function() { | |
var error = new Error( action ); | |
error.xhr = xhr; | |
error.options = options; | |
reject( error ); | |
}; | |
}); | |
}); | |
// Expose the raw XHR object | |
promise.xhr = xhr; | |
// Proxy common methods for convenience and chaining | |
cleanXhr.proxyMethods.forEach(function( method ) { | |
promise[ method ] = function() { | |
xhr[ method ].apply( xhr, arguments ); | |
return promise; | |
}; | |
}); | |
return promise; | |
} | |
cleanXhr.proxyMethods = [ | |
"abort", | |
"overrideMimeType", | |
"send", | |
"setRequestHeader" | |
]; | |
cleanXhr.proxyProperties = [ | |
"timeout", | |
"upload", | |
"withCredentials" | |
]; | |
// headers | |
// TODO: response parsing | |
function simpleXhr( options ) { | |
var promise = cleanXhr( options ); | |
if ( !options.headers ) { | |
options.headers = {}; | |
} | |
if ( !options.headers[ "X-Requested-With" ] ) { | |
options.headers[ "X-Requested-With" ] = "XMLHttpRequest"; | |
} | |
Object.keys( options.headers ).forEach(function( header ) { | |
promise.setRequestHeader( header, options.headers[ header ] ); | |
}); | |
return promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment