mirror of
https://github.com/tenrok/axios.git
synced 2026-06-05 16:42:32 +03:00
Adding support for params
This commit is contained in:
+38
-8
@@ -1,4 +1,5 @@
|
||||
var Promise = require('es6-promise').Promise;
|
||||
var buildUrl = require('./buildUrl');
|
||||
var defaults = require('./defaults');
|
||||
var forEach = require('./forEach');
|
||||
var merge = require('./merge');
|
||||
@@ -11,17 +12,31 @@ var axios = module.exports = function axios(options) {
|
||||
transformResponse: defaults.transformResponse
|
||||
}, options);
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
var request = new(XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP');
|
||||
var data = transformData(options.data, options.headers, options.transformRequest);
|
||||
// Don't allow overriding defaults.withCredentials
|
||||
options.withCredentials = options.withCredentials || defaults.withCredentials;
|
||||
|
||||
// Open request and listen for ready state
|
||||
request.open(options.method, options.url, true);
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
// Create the request
|
||||
var request = new(XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP');
|
||||
var data = transformData(
|
||||
options.data,
|
||||
options.headers,
|
||||
options.transformRequest
|
||||
);
|
||||
|
||||
// Open the request
|
||||
request.open(options.method, buildUrl(options.url, options.params), true);
|
||||
|
||||
// Listen for ready state
|
||||
request.onreadystatechange = function () {
|
||||
if (request && request.readyState === 4) {
|
||||
// Prepare the response
|
||||
var response = {
|
||||
data: transformData(request.responseText, options.headers, options.transformResponse),
|
||||
data: transformData(
|
||||
request.responseText,
|
||||
options.headers,
|
||||
options.transformResponse
|
||||
),
|
||||
status: request.status,
|
||||
headers: headers,
|
||||
config: options
|
||||
@@ -48,8 +63,7 @@ var axios = module.exports = function axios(options) {
|
||||
|
||||
forEach(headers, function (val, key) {
|
||||
// Remove Content-Type if data is undefined
|
||||
if (typeof data === 'undefined' &&
|
||||
key.toLowerCase() === 'content-type') {
|
||||
if (typeof data === 'undefined' && key.toLowerCase() === 'content-type') {
|
||||
delete headers[key];
|
||||
}
|
||||
// Otherwise add header to the request
|
||||
@@ -58,6 +72,22 @@ var axios = module.exports = function axios(options) {
|
||||
}
|
||||
});
|
||||
|
||||
// Add withCredentials to request if needed
|
||||
if (options.withCredentials) {
|
||||
request.withCredentials = true;
|
||||
}
|
||||
|
||||
// Add responseType to request if needed
|
||||
if (options.responseType) {
|
||||
try {
|
||||
request.responseType = options.responseType;
|
||||
} catch (e) {
|
||||
if (request.responseType !== 'json') {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send the request
|
||||
request.send(data);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
var forEach = require('./forEach');
|
||||
|
||||
function encode(val) {
|
||||
return encodeURIComponent(val).
|
||||
replace(/%40/gi, '@').
|
||||
replace(/%3A/gi, ':').
|
||||
replace(/%24/g, '$').
|
||||
replace(/%2C/gi, ',').
|
||||
replace(/%20/g, '+');
|
||||
}
|
||||
|
||||
module.exports = function buildUrl(url, params) {
|
||||
if (!params) {
|
||||
return url;
|
||||
}
|
||||
|
||||
var parts = [];
|
||||
|
||||
forEach(params, function (val, key) {
|
||||
if (val === null || typeof val === 'undefined') {
|
||||
return;
|
||||
}
|
||||
if (Object.prototype.toString.call(val) !== '[object Array]') {
|
||||
val = [val];
|
||||
}
|
||||
|
||||
forEach(val, function (v) {
|
||||
if (Object.prototype.toString.call(v) === '[object Date]') {
|
||||
v = v.toISOString();
|
||||
}
|
||||
else if (typeof v === 'object') {
|
||||
v = JSON.stringify(v);
|
||||
}
|
||||
parts.push(encode(key) + '=' + encode(v));
|
||||
});
|
||||
});
|
||||
|
||||
if (parts.length > 0) {
|
||||
url += (url.indexOf('?') === -1 ? '?' : '&') + parts.join('&');
|
||||
}
|
||||
|
||||
return url;
|
||||
};
|
||||
Reference in New Issue
Block a user