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

backport: custom params serializer support for v0.x #6263

Merged
merged 4 commits into from Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions lib/core/Axios.js
Expand Up @@ -61,6 +61,13 @@ Axios.prototype.request = function request(configOrUrl, config) {

var paramsSerializer = config.paramsSerializer;

if (paramsSerializer !== undefined) {
validator.assertOptions(paramsSerializer, {
encode: validators.function,
serialize: validators.function
}, true);
}

utils.isFunction(paramsSerializer) && (config.paramsSerializer = {serialize: paramsSerializer});

// filter out skipped interceptors
Expand Down
18 changes: 13 additions & 5 deletions lib/helpers/buildURL.js
Expand Up @@ -35,12 +35,20 @@ module.exports = function buildURL(url, params, options) {

var _encode = options && options.encode || encode;

var serializerParams = utils.isURLSearchParams(params) ?
params.toString() :
new AxiosURLSearchParams(params, options).toString(_encode);
var serializeFn = options && options.serialize;

if (serializerParams) {
url += (url.indexOf('?') === -1 ? '?' : '&') + serializerParams;
var serializedParams;

if (serializeFn) {
serializedParams = serializeFn(params, options);
} else {
serializedParams = utils.isURLSearchParams(params) ?
params.toString() :
new AxiosURLSearchParams(params, options).toString(_encode);
}

if (serializedParams) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid defining hashmarkIndex also when serializedParams in a falsy value, these line

  var hashmarkIndex = url.indexOf('#');
  if (hashmarkIndex !== -1) {
    url = url.slice(0, hashmarkIndex);
  }

can be moved inside the if

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JadeMugs I disagree. I don't think it matters much since the hashMark part of the code is only to strip out the URL. Besides, I'm following the version in #5113 here:

https://github.com/DigitalBrainJS/axios/blob/92f0758429541b05c614dc25bc712379d8a41dbd/lib/helpers/buildURL.js#L39-L57

url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
}

return url;
Expand Down
16 changes: 16 additions & 0 deletions test/specs/helpers/buildURL.spec.js
Expand Up @@ -63,4 +63,20 @@ describe('helpers::buildURL', function () {
it('should support URLSearchParams', function () {
expect(buildURL('/foo', new URLSearchParams('bar=baz'))).toEqual('/foo?bar=baz');
});

it('should support custom serialize function', function () {
var params = {
x: 1
};

var options = {
serialize: function (thisParams, thisOptions) {
expect(thisParams).toEqual(params);
expect(thisOptions).toEqual(options);
return 'rendered';
}
};

expect(buildURL('/foo', params, options)).toEqual('/foo?rendered');
});
});
3 changes: 2 additions & 1 deletion test/typescript/axios.ts
Expand Up @@ -22,7 +22,8 @@ const config: AxiosRequestConfig = {
params: { id: 12345 },
paramsSerializer: {
indexes: true,
encode: (value) => value
encode: (value) => value,
serialize: (value, options) => String(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @briwa, I noticed the ParamsSerializerOptions type does not include the serialize prop.
The type should be aligned in the index.d.ts file.

Note: In v1.x, the ParamsSerializerOptions interface is defined like this:

export interface ParamsSerializerOptions extends SerializerOptions {
  encode?: ParamEncoder;
  serialize?: CustomParamsSerializer;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JadeMugs Thanks, I missed it. I just added them

},
data: { foo: 'bar' },
timeout: 10000,
Expand Down