Skip to content

Commit

Permalink
add withCredetials support
Browse files Browse the repository at this point in the history
  • Loading branch information
udivankin committed Aug 13, 2018
1 parent e775dd6 commit 770002a
Show file tree
Hide file tree
Showing 4 changed files with 1,451 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pico-ajax",
"version": "0.2.1",
"version": "0.2.2",
"description": "Very tiny yet functional ajax library written in ES6",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
21 changes: 16 additions & 5 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function handleBrowserResponse(xhr) {
/**
* Make a request
*
* @param {string} url Url
* @param {string} method HTTP Method
* @param {Object} options request options
* @returns {Promise}
*/
Expand All @@ -43,9 +43,20 @@ export function browserRequest(method, originalUrl, options) {
// Open XMLHttpRequest using given options
xhr.open(method, originalUrl, options.async, options.username, options.password);

// Override default timeout and responseType for XMLHttpRequest
xhr.responseType = options.responseType;
xhr.timeout = options.timeout;
// Override default timeout, responseType and withCredentials for XMLHttpRequest
const { responseType, timeout, withCredentials } = options;

if (typeof responseType !== 'undefined') {
xhr.responseType = options.responseType;
}

if (typeof timeout !== 'undefined') {
xhr.timeout = options.timeout;
}

if (typeof withCredentials !== 'undefined') {
xhr.withCredentials = options.withCredentials;
}

// Set request headers one by one using XMLHttpRequest.setRequestHeader method
Object.keys(options.headers).forEach(headerKey => {
Expand All @@ -62,7 +73,7 @@ export function browserRequest(method, originalUrl, options) {
// Define onload callback
xhr.onload = () => {
if (xhr.status !== 200) {
reject(new Error(`[${xhr.status}]`, xhr.response, xhr.statusText));
reject(new Error(`[${xhr.status}] ${xhr.statusText} ${xhr.response}`));
} else {
resolve(handleBrowserResponse(xhr));
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const DEFAULT_OPTIONS = {
responseType: '',
timeout: 0,
username: undefined,
withCredetials: undefined,
};

/**
Expand Down
Loading

0 comments on commit 770002a

Please sign in to comment.