Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support third-party library to serialize url params #121

Merged
merged 1 commit into from Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions axios.d.ts
Expand Up @@ -41,6 +41,7 @@ declare module axios {
responseType?: string;
xsrfCookieName?: string;
xsrfHeaderName?: string;
paramsSerializer?: (params: any) => string;
}
}

Expand Down
4 changes: 2 additions & 2 deletions karma.conf.js
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +52,7 @@ module.exports = function(config) {
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots', 'coverage'],


coverageReporter: {
type: 'lcov',
dir: 'coverage/',
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/xhr.js
Expand Up @@ -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;
Expand Down
54 changes: 31 additions & 23 deletions lib/helpers/buildUrl.js
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -46,6 +46,7 @@
"karma-jasmine": "0.3.6",
"karma-jasmine-ajax": "0.1.13",
"karma-phantomjs-launcher": "0.2.1",
"karma-sinon": "1.0.4",
"karma-sourcemap-loader": "0.3.5",
"karma-webpack": "1.7.0",
"load-grunt-tasks": "3.3.0",
Expand Down
9 changes: 9 additions & 0 deletions test/specs/helpers/buildUrl.spec.js
Expand Up @@ -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);
})
});