mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
Merge branch 'master' of github.com:mzabriskie/axios into xDomainRequestSupport
Conflicts: lib/adapters/xhr.js
This commit is contained in:
+22
-6
@@ -2,11 +2,12 @@
|
||||
|
||||
var defaults = require('./../defaults');
|
||||
var utils = require('./../utils');
|
||||
var buildUrl = require('./../helpers/buildUrl');
|
||||
var buildURL = require('./../helpers/buildURL');
|
||||
var transformData = require('./../helpers/transformData');
|
||||
var http = require('http');
|
||||
var https = require('https');
|
||||
var http = require('follow-redirects').http;
|
||||
var https = require('follow-redirects').https;
|
||||
var url = require('url');
|
||||
var zlib = require('zlib');
|
||||
var pkg = require('./../../package.json');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
|
||||
@@ -50,7 +51,7 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
var options = {
|
||||
host: parsed.hostname,
|
||||
port: parsed.port,
|
||||
path: buildUrl(parsed.path, config.params).replace(/^\?/, ''),
|
||||
path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
|
||||
method: config.method,
|
||||
headers: headers,
|
||||
agent: config.agent
|
||||
@@ -59,12 +60,27 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
// Create the request
|
||||
var transport = parsed.protocol === 'https:' ? https : http;
|
||||
var req = transport.request(options, function (res) {
|
||||
|
||||
// uncompress the response body transparently if required
|
||||
var stream = res;
|
||||
switch(res.headers['content-encoding']) {
|
||||
case 'gzip':
|
||||
case 'compress':
|
||||
case 'deflate': {
|
||||
// add the unzipper to the body stream processing pipeline
|
||||
stream = stream.pipe(zlib.createUnzip());
|
||||
|
||||
// remove the content-encoding in order to not confuse downstream operations
|
||||
delete res.headers['content-encoding'];
|
||||
}
|
||||
}
|
||||
|
||||
var responseBuffer = [];
|
||||
res.on('data', function (chunk) {
|
||||
stream.on('data', function (chunk) {
|
||||
responseBuffer.push(chunk);
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
stream.on('end', function () {
|
||||
var data = Buffer.concat(responseBuffer);
|
||||
if (config.responseType !== 'arraybuffer') {
|
||||
data = data.toString('utf8');
|
||||
|
||||
+8
-8
@@ -4,9 +4,10 @@
|
||||
|
||||
var defaults = require('./../defaults');
|
||||
var utils = require('./../utils');
|
||||
var buildUrl = require('./../helpers/buildUrl');
|
||||
var buildURL = require('./../helpers/buildURL');
|
||||
var parseHeaders = require('./../helpers/parseHeaders');
|
||||
var transformData = require('./../helpers/transformData');
|
||||
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
|
||||
|
||||
module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
// Transform request data
|
||||
@@ -27,12 +28,12 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
delete requestHeaders['Content-Type']; // Let the browser set it
|
||||
}
|
||||
|
||||
var adapter = (XMLHttpRequest || ActiveXObject),
|
||||
loadEvent = 'onreadystatechange',
|
||||
xDomain = false;
|
||||
var adapter = (XMLHttpRequest || ActiveXObject);
|
||||
var loadEvent = 'onreadystatechange';
|
||||
var xDomain = false;
|
||||
|
||||
// For IE 8/9 CORS support
|
||||
if(config.xDomain && window.XDomainRequest){
|
||||
if(!isURLSameOrigin(config.url) && window.XDomainRequest){
|
||||
adapter = window.XDomainRequest;
|
||||
loadEvent = 'onload';
|
||||
xDomain = true;
|
||||
@@ -40,7 +41,6 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
|
||||
// Create the request
|
||||
var request = new adapter('Microsoft.XMLHTTP');
|
||||
|
||||
request.open(config.method.toUpperCase(), buildUrl(config.url, config.params, config.paramsSerializer), true);
|
||||
|
||||
// Set the request timeout in MS
|
||||
@@ -78,10 +78,10 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
// Specifically not if we're in a web worker, or react-native.
|
||||
if (utils.isStandardBrowserEnv()) {
|
||||
var cookies = require('./../helpers/cookies');
|
||||
var urlIsSameOrigin = require('./../helpers/urlIsSameOrigin');
|
||||
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
|
||||
|
||||
// Add xsrf header
|
||||
var xsrfValue = urlIsSameOrigin(config.url) ?
|
||||
var xsrfValue = isURLSameOrigin(config.url) ?
|
||||
cookies.read(config.xsrfCookieName || defaults.xsrfCookieName) :
|
||||
undefined;
|
||||
|
||||
|
||||
+27
-32
@@ -71,40 +71,35 @@ axios.spread = require('./helpers/spread');
|
||||
// Expose interceptors
|
||||
axios.interceptors = defaultInstance.interceptors;
|
||||
|
||||
// Provide aliases for supported request methods
|
||||
(function () {
|
||||
function createShortMethods() {
|
||||
utils.forEach(arguments, function (method) {
|
||||
Axios.prototype[method] = function (url, config) {
|
||||
return this.request(utils.merge(config || {}, {
|
||||
method: method,
|
||||
url: url
|
||||
}));
|
||||
};
|
||||
axios[method] = bind(Axios.prototype[method], defaultInstance);
|
||||
});
|
||||
}
|
||||
|
||||
function createShortMethodsWithData() {
|
||||
utils.forEach(arguments, function (method) {
|
||||
Axios.prototype[method] = function (url, data, config) {
|
||||
return this.request(utils.merge(config || {}, {
|
||||
method: method,
|
||||
url: url,
|
||||
data: data
|
||||
}));
|
||||
};
|
||||
axios[method] = bind(Axios.prototype[method], defaultInstance);
|
||||
});
|
||||
}
|
||||
|
||||
createShortMethods('delete', 'get', 'head');
|
||||
createShortMethodsWithData('post', 'put', 'patch');
|
||||
})();
|
||||
|
||||
// Helpers
|
||||
function bind (fn, thisArg) {
|
||||
return function () {
|
||||
return fn.apply(thisArg, Array.prototype.slice.call(arguments));
|
||||
var args = new Array(arguments.length);
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
args[i] = arguments[i];
|
||||
}
|
||||
return fn.apply(thisArg, args);
|
||||
};
|
||||
}
|
||||
|
||||
// Provide aliases for supported request methods
|
||||
utils.forEach(['delete', 'get', 'head'], function (method) {
|
||||
Axios.prototype[method] = function (url, config) {
|
||||
return this.request(utils.merge(config || {}, {
|
||||
method: method,
|
||||
url: url
|
||||
}));
|
||||
};
|
||||
axios[method] = bind(Axios.prototype[method], defaultInstance);
|
||||
});
|
||||
|
||||
utils.forEach(['post', 'put', 'patch'], function (method) {
|
||||
Axios.prototype[method] = function (url, data, config) {
|
||||
return this.request(utils.merge(config || {}, {
|
||||
method: method,
|
||||
url: url,
|
||||
data: data
|
||||
}));
|
||||
};
|
||||
axios[method] = bind(Axios.prototype[method], defaultInstance);
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ function encode(val) {
|
||||
* @param {object} [params] The params to be appended
|
||||
* @returns {string} The formatted url
|
||||
*/
|
||||
module.exports = function buildUrl(url, params, paramsSerializer) {
|
||||
module.exports = function buildURL(url, params, paramsSerializer) {
|
||||
if (!params) {
|
||||
return url;
|
||||
}
|
||||
|
||||
+42
-32
@@ -1,43 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* WARNING:
|
||||
* This file makes references to objects that aren't safe in all environments.
|
||||
* Please see lib/utils.isStandardBrowserEnv before including this file.
|
||||
*/
|
||||
|
||||
var utils = require('./../utils');
|
||||
|
||||
module.exports = {
|
||||
write: function write(name, value, expires, path, domain, secure) {
|
||||
var cookie = [];
|
||||
cookie.push(name + '=' + encodeURIComponent(value));
|
||||
module.exports = (
|
||||
utils.isStandardBrowserEnv() ?
|
||||
|
||||
if (utils.isNumber(expires)) {
|
||||
cookie.push('expires=' + new Date(expires).toGMTString());
|
||||
}
|
||||
// Standard browser envs support document.cookie
|
||||
(function () {
|
||||
return {
|
||||
write: function write(name, value, expires, path, domain, secure) {
|
||||
var cookie = [];
|
||||
cookie.push(name + '=' + encodeURIComponent(value));
|
||||
|
||||
if (utils.isString(path)) {
|
||||
cookie.push('path=' + path);
|
||||
}
|
||||
if (utils.isNumber(expires)) {
|
||||
cookie.push('expires=' + new Date(expires).toGMTString());
|
||||
}
|
||||
|
||||
if (utils.isString(domain)) {
|
||||
cookie.push('domain=' + domain);
|
||||
}
|
||||
if (utils.isString(path)) {
|
||||
cookie.push('path=' + path);
|
||||
}
|
||||
|
||||
if (secure === true) {
|
||||
cookie.push('secure');
|
||||
}
|
||||
if (utils.isString(domain)) {
|
||||
cookie.push('domain=' + domain);
|
||||
}
|
||||
|
||||
document.cookie = cookie.join('; ');
|
||||
},
|
||||
if (secure === true) {
|
||||
cookie.push('secure');
|
||||
}
|
||||
|
||||
read: function read(name) {
|
||||
var match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
|
||||
return (match ? decodeURIComponent(match[3]) : null);
|
||||
},
|
||||
document.cookie = cookie.join('; ');
|
||||
},
|
||||
|
||||
remove: function remove(name) {
|
||||
this.write(name, '', Date.now() - 86400000);
|
||||
}
|
||||
};
|
||||
read: function read(name) {
|
||||
var match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
|
||||
return (match ? decodeURIComponent(match[3]) : null);
|
||||
},
|
||||
|
||||
remove: function remove(name) {
|
||||
this.write(name, '', Date.now() - 86400000);
|
||||
}
|
||||
};
|
||||
})() :
|
||||
|
||||
// Non standard browser env (web workers, react-native) lack needed support.
|
||||
(function () {
|
||||
return {
|
||||
write: function write() {},
|
||||
read: function read() { return null; },
|
||||
remove: function remove() {}
|
||||
};
|
||||
})()
|
||||
);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
|
||||
var utils = require('./../utils');
|
||||
|
||||
module.exports = (
|
||||
utils.isStandardBrowserEnv() ?
|
||||
|
||||
// Standard browser envs have full support of the APIs needed to test
|
||||
// whether the request URL is of the same origin as current location.
|
||||
(function () {
|
||||
var msie = /(msie|trident)/i.test(navigator.userAgent);
|
||||
var urlParsingNode = document.createElement('a');
|
||||
var originURL;
|
||||
|
||||
/**
|
||||
* Parse a URL to discover it's components
|
||||
*
|
||||
* @param {String} url The URL to be parsed
|
||||
* @returns {Object}
|
||||
*/
|
||||
function resolveURL(url) {
|
||||
var href = url;
|
||||
|
||||
if (msie) {
|
||||
// IE needs attribute set twice to normalize properties
|
||||
urlParsingNode.setAttribute('href', href);
|
||||
href = urlParsingNode.href;
|
||||
}
|
||||
|
||||
urlParsingNode.setAttribute('href', href);
|
||||
|
||||
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
|
||||
return {
|
||||
href: urlParsingNode.href,
|
||||
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
|
||||
host: urlParsingNode.host,
|
||||
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
|
||||
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
|
||||
hostname: urlParsingNode.hostname,
|
||||
port: urlParsingNode.port,
|
||||
pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
|
||||
urlParsingNode.pathname :
|
||||
'/' + urlParsingNode.pathname
|
||||
};
|
||||
}
|
||||
|
||||
originURL = resolveURL(window.location.href);
|
||||
|
||||
/**
|
||||
* Determine if a URL shares the same origin as the current location
|
||||
*
|
||||
* @param {String} requestURL The URL to test
|
||||
* @returns {boolean} True if URL shares the same origin, otherwise false
|
||||
*/
|
||||
return function isURLSameOrigin(requestURL) {
|
||||
var parsed = (utils.isString(requestURL)) ? resolveURL(requestURL) : requestURL;
|
||||
return (parsed.protocol === originURL.protocol &&
|
||||
parsed.host === originURL.host);
|
||||
};
|
||||
})() :
|
||||
|
||||
// Non standard browser envs (web workers, react-native) lack needed support.
|
||||
(function () {
|
||||
return function isURLSameOrigin() {
|
||||
return true;
|
||||
};
|
||||
})()
|
||||
);
|
||||
@@ -1,58 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* WARNING:
|
||||
* This file makes references to objects that aren't safe in all environments.
|
||||
* Please see lib/utils.isStandardBrowserEnv before including this file.
|
||||
*/
|
||||
|
||||
var utils = require('./../utils');
|
||||
var msie = /(msie|trident)/i.test(navigator.userAgent);
|
||||
var urlParsingNode = document.createElement('a');
|
||||
var originUrl;
|
||||
|
||||
/**
|
||||
* Parse a URL to discover it's components
|
||||
*
|
||||
* @param {String} url The URL to be parsed
|
||||
* @returns {Object}
|
||||
*/
|
||||
function urlResolve(url) {
|
||||
var href = url;
|
||||
|
||||
if (msie) {
|
||||
// IE needs attribute set twice to normalize properties
|
||||
urlParsingNode.setAttribute('href', href);
|
||||
href = urlParsingNode.href;
|
||||
}
|
||||
|
||||
urlParsingNode.setAttribute('href', href);
|
||||
|
||||
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
|
||||
return {
|
||||
href: urlParsingNode.href,
|
||||
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
|
||||
host: urlParsingNode.host,
|
||||
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
|
||||
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
|
||||
hostname: urlParsingNode.hostname,
|
||||
port: urlParsingNode.port,
|
||||
pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
|
||||
urlParsingNode.pathname :
|
||||
'/' + urlParsingNode.pathname
|
||||
};
|
||||
}
|
||||
|
||||
originUrl = urlResolve(window.location.href);
|
||||
|
||||
/**
|
||||
* Determine if a URL shares the same origin as the current location
|
||||
*
|
||||
* @param {String} requestUrl The URL to test
|
||||
* @returns {boolean} True if URL shares the same origin, otherwise false
|
||||
*/
|
||||
module.exports = function urlIsSameOrigin(requestUrl) {
|
||||
var parsed = (utils.isString(requestUrl)) ? urlResolve(requestUrl) : requestUrl;
|
||||
return (parsed.protocol === originUrl.protocol &&
|
||||
parsed.host === originUrl.host);
|
||||
};
|
||||
+9
-22
@@ -130,16 +130,6 @@ function trim(str) {
|
||||
return str.replace(/^\s*/, '').replace(/\s*$/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is an Arguments object
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is an Arguments object, otherwise false
|
||||
*/
|
||||
function isArguments(val) {
|
||||
return toString.call(val) === '[object Arguments]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if we're running in a standard browser environment
|
||||
*
|
||||
@@ -151,7 +141,7 @@ function isArguments(val) {
|
||||
* typeof document -> undefined
|
||||
*
|
||||
* react-native:
|
||||
* typeof document.createelement -> undefined
|
||||
* typeof document.createElement -> undefined
|
||||
*/
|
||||
function isStandardBrowserEnv() {
|
||||
return (
|
||||
@@ -164,7 +154,7 @@ function isStandardBrowserEnv() {
|
||||
/**
|
||||
* Iterate over an Array or an Object invoking a function for each item.
|
||||
*
|
||||
* If `obj` is an Array or arguments callback will be called passing
|
||||
* If `obj` is an Array callback will be called passing
|
||||
* the value, index, and complete array for each item.
|
||||
*
|
||||
* If 'obj' is an Object callback will be called passing
|
||||
@@ -179,16 +169,13 @@ function forEach(obj, fn) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if obj is array-like
|
||||
var isArrayLike = isArray(obj) || isArguments(obj);
|
||||
|
||||
// Force an array if not already something iterable
|
||||
if (typeof obj !== 'object' && !isArrayLike) {
|
||||
if (typeof obj !== 'object' && !isArray(obj)) {
|
||||
obj = [obj];
|
||||
}
|
||||
|
||||
// Iterate over array values
|
||||
if (isArrayLike) {
|
||||
if (isArray(obj)) {
|
||||
for (var i = 0, l = obj.length; i < l; i++) {
|
||||
fn.call(null, obj[i], i, obj);
|
||||
}
|
||||
@@ -222,11 +209,11 @@ function forEach(obj, fn) {
|
||||
*/
|
||||
function merge(/*obj1, obj2, obj3, ...*/) {
|
||||
var result = {};
|
||||
forEach(arguments, function (obj) {
|
||||
forEach(obj, function (val, key) {
|
||||
result[key] = val;
|
||||
});
|
||||
});
|
||||
var assignValue = function (val, key) { result[key] = val; };
|
||||
var length = arguments.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
forEach(arguments[i], assignValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user