mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Adding support for params
This commit is contained in:
Vendored
+135
-55
File diff suppressed because one or more lines are too long
Vendored
+135
-55
File diff suppressed because one or more lines are too long
Vendored
+135
-55
File diff suppressed because one or more lines are too long
Vendored
+135
-55
File diff suppressed because one or more lines are too long
+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;
|
||||
};
|
||||
@@ -88,6 +88,19 @@ describe('axios', function () {
|
||||
expect(request.requestHeaders['X-Requested-With']).toEqual('XMLHttpRequest');
|
||||
});
|
||||
|
||||
it('should accept params', function () {
|
||||
axios({
|
||||
url: '/foo',
|
||||
params: {
|
||||
foo: 123,
|
||||
bar: 456
|
||||
}
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.url).toBe('/foo?foo=123&bar=456');
|
||||
});
|
||||
|
||||
it('should allow overriding default headers', function () {
|
||||
axios({
|
||||
headers: {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
var buildUrl = require('../../lib/buildUrl');
|
||||
|
||||
module.exports = {
|
||||
testNullParams: function (test) {
|
||||
var url = buildUrl('/foo');
|
||||
|
||||
test.equals(url, '/foo');
|
||||
test.done();
|
||||
},
|
||||
|
||||
testParams: function (test) {
|
||||
var url = buildUrl('/foo', {
|
||||
foo: 'bar'
|
||||
});
|
||||
|
||||
test.equals(url, '/foo?foo=bar');
|
||||
test.done();
|
||||
},
|
||||
|
||||
testObjectParam: function (test) {
|
||||
var url = buildUrl('/foo', {
|
||||
foo: {
|
||||
bar: 'baz'
|
||||
}
|
||||
});
|
||||
|
||||
test.equals(url, '/foo?foo=' + encodeURI('{"bar":"baz"}'));
|
||||
test.done();
|
||||
},
|
||||
|
||||
testDateParam: function (test) {
|
||||
var date = new Date();
|
||||
var url = buildUrl('/foo', {
|
||||
date: date
|
||||
});
|
||||
|
||||
test.equals(url, '/foo?date=' + date.toISOString());
|
||||
test.done();
|
||||
},
|
||||
|
||||
testArrayParam: function (test) {
|
||||
var url = buildUrl('/foo', {
|
||||
foo: ['bar', 'baz']
|
||||
});
|
||||
|
||||
test.equals(url, '/foo?foo=bar&foo=baz');
|
||||
test.done();
|
||||
},
|
||||
|
||||
testSpecialChars: function (test) {
|
||||
var url = buildUrl('/foo', {
|
||||
foo: '@:$, '
|
||||
});
|
||||
|
||||
test.equals(url, '/foo?foo=@:$,+');
|
||||
test.done();
|
||||
},
|
||||
|
||||
testQuestionMark: function (test) {
|
||||
var url = buildUrl('/foo?foo=bar', {
|
||||
bar: 'baz'
|
||||
});
|
||||
|
||||
test.equals(url, '/foo?foo=bar&bar=baz');
|
||||
test.done();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user