提交 11c12b2c 编写于 作者: M Matt Zabriskie

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

Add support third-party library to serialize url params
......@@ -169,6 +169,12 @@ This is the available config options for making requests. Only the `url` is requ
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
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash
......
......@@ -48,6 +48,7 @@ declare module axios {
responseType?: string;
xsrfCookieName?: string;
xsrfHeaderName?: string;
paramsSerializer?: (params: any) => string;
}
interface RequestOptions extends InstanceOptions {
......
......@@ -10,7 +10,7 @@ module.exports = function(config) {
// frameworks to use
// 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
......@@ -62,7 +62,7 @@ module.exports = function(config) {
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots', 'coverage'],
coverageReporter: {
type: 'lcov',
dir: 'coverage/',
......
......@@ -29,7 +29,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
// Create the request
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
request.timeout = config.timeout;
......
......@@ -20,39 +20,47 @@ function encode(val) {
* @param {object} [params] The params to be appended
* @returns {string} The formatted url
*/
module.exports = function buildUrl(url, params) {
module.exports = function buildUrl(url, params, paramsSerializer) {
if (!params) {
return url;
}
var parts = [];
utils.forEach(params, function (val, key) {
if (val === null || typeof val === 'undefined') {
return;
}
if (utils.isArray(val)) {
key = key + '[]';
}
var serializedParams;
if (paramsSerializer) {
serializedParams = paramsSerializer(params);
}
else {
var parts = [];
if (!utils.isArray(val)) {
val = [val];
}
utils.forEach(params, function (val, key) {
if (val === null || typeof val === 'undefined') {
return;
}
utils.forEach(val, function (v) {
if (utils.isDate(v)) {
v = v.toISOString();
if (utils.isArray(val)) {
key = key + '[]';
}
else if (utils.isObject(v)) {
v = JSON.stringify(v);
if (!utils.isArray(val)) {
val = [val];
}
parts.push(encode(key) + '=' + encode(v));
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) {
url += (url.indexOf('?') === -1 ? '?' : '&') + parts.join('&');
serializedParams = parts.join('&');
}
if (serializedParams) {
url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
}
return url;
......
......@@ -52,5 +52,14 @@ describe('helpers::buildUrl', function () {
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);
})
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册