2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

Merge pull request #121 from azendoo/custom-encode-params-method

Add support third-party library to serialize url params
This commit is contained in:
Matt Zabriskie
2015-10-27 14:48:02 -06:00
7 changed files with 52 additions and 27 deletions
+6
View File
@@ -169,6 +169,12 @@ This is the available config options for making requests. Only the `url` is requ
ID: 12345 ID: 12345
}, },
// `paramsSerializer` is an optional function in charge of serializing `params`
// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
paramsSerializer: function(params) {
return Qs.stringify(params, {arrayFormat: 'brackets'})
},
// `data` is the data to be sent as the request body // `data` is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', and 'PATCH' // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash // When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash
Vendored
+1
View File
@@ -48,6 +48,7 @@ declare module axios {
responseType?: string; responseType?: string;
xsrfCookieName?: string; xsrfCookieName?: string;
xsrfHeaderName?: string; xsrfHeaderName?: string;
paramsSerializer?: (params: any) => string;
} }
interface RequestOptions extends InstanceOptions { interface RequestOptions extends InstanceOptions {
+2 -2
View File
@@ -10,7 +10,7 @@ module.exports = function(config) {
// frameworks to use // frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine-ajax', 'jasmine'], frameworks: ['jasmine-ajax', 'jasmine', 'sinon'],
// list of files / patterns to load in the browser // list of files / patterns to load in the browser
@@ -62,7 +62,7 @@ module.exports = function(config) {
// available reporters: https://npmjs.org/browse/keyword/karma-reporter // available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots', 'coverage'], reporters: ['dots', 'coverage'],
coverageReporter: { coverageReporter: {
type: 'lcov', type: 'lcov',
dir: 'coverage/', dir: 'coverage/',
+1 -1
View File
@@ -29,7 +29,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
// Create the request // Create the request
var request = new (XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP'); var request = new (XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP');
request.open(config.method.toUpperCase(), buildUrl(config.url, config.params), true); request.open(config.method.toUpperCase(), buildUrl(config.url, config.params, config.paramsSerializer), true);
// Set the request timeout in MS // Set the request timeout in MS
request.timeout = config.timeout; request.timeout = config.timeout;
+32 -24
View File
@@ -20,39 +20,47 @@ function encode(val) {
* @param {object} [params] The params to be appended * @param {object} [params] The params to be appended
* @returns {string} The formatted url * @returns {string} The formatted url
*/ */
module.exports = function buildUrl(url, params) { module.exports = function buildUrl(url, params, paramsSerializer) {
if (!params) { if (!params) {
return url; return url;
} }
var parts = []; var serializedParams;
if (paramsSerializer) {
serializedParams = paramsSerializer(params);
}
else {
var parts = [];
utils.forEach(params, function (val, key) { utils.forEach(params, function (val, key) {
if (val === null || typeof val === 'undefined') { if (val === null || typeof val === 'undefined') {
return; return;
}
if (utils.isArray(val)) {
key = key + '[]';
}
if (!utils.isArray(val)) {
val = [val];
}
utils.forEach(val, function (v) {
if (utils.isDate(v)) {
v = v.toISOString();
} }
else if (utils.isObject(v)) {
v = JSON.stringify(v); if (utils.isArray(val)) {
key = key + '[]';
} }
parts.push(encode(key) + '=' + encode(v));
if (!utils.isArray(val)) {
val = [val];
}
utils.forEach(val, function (v) {
if (utils.isDate(v)) {
v = v.toISOString();
}
else if (utils.isObject(v)) {
v = JSON.stringify(v);
}
parts.push(encode(key) + '=' + encode(v));
});
}); });
});
if (parts.length > 0) { serializedParams = parts.join('&');
url += (url.indexOf('?') === -1 ? '?' : '&') + parts.join('&'); }
if (serializedParams) {
url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
} }
return url; return url;
+1
View File
@@ -47,6 +47,7 @@
"karma-jasmine": "0.3.6", "karma-jasmine": "0.3.6",
"karma-jasmine-ajax": "0.1.13", "karma-jasmine-ajax": "0.1.13",
"karma-phantomjs-launcher": "0.2.1", "karma-phantomjs-launcher": "0.2.1",
"karma-sinon": "1.0.4",
"karma-sourcemap-loader": "0.3.5", "karma-sourcemap-loader": "0.3.5",
"karma-webpack": "1.7.0", "karma-webpack": "1.7.0",
"load-grunt-tasks": "3.3.0", "load-grunt-tasks": "3.3.0",
+9
View File
@@ -52,5 +52,14 @@ describe('helpers::buildUrl', function () {
length: 5 length: 5
})).toEqual('/foo?query=bar&start=0&length=5'); })).toEqual('/foo?query=bar&start=0&length=5');
}); });
it('should use serializer if provided', function () {
serializer = sinon.stub();
params = {foo: 'bar'};
serializer.returns('foo=bar');
expect(buildUrl('/foo', params, serializer)).toEqual('/foo?foo=bar');
expect(serializer.calledOnce).toBe(true);
expect(serializer.calledWith(params)).toBe(true);
})
}); });