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

Added custom params serializer support; #5113

Merged
merged 4 commits into from Oct 13, 2022
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
5 changes: 5 additions & 0 deletions index.d.ts
Expand Up @@ -247,8 +247,13 @@ export interface ParamEncoder {
(value: any, defaultEncoder: (value: any) => any): any;
}

export interface CustomParamsSerializer {
(params: object, options?: ParamsSerializerOptions): string;
}

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

type MaxUploadRate = number;
Expand Down
9 changes: 8 additions & 1 deletion lib/core/Axios.js
Expand Up @@ -47,7 +47,7 @@ class Axios {

config = mergeConfig(this.defaults, config);

const transitional = config.transitional;
const {transitional, paramsSerializer} = config;

if (transitional !== undefined) {
validator.assertOptions(transitional, {
Expand All @@ -57,6 +57,13 @@ class Axios {
}, false);
}

if (paramsSerializer !== undefined) {
validator.assertOptions(paramsSerializer, {
Copy link

@Roriz Roriz Oct 17, 2022

Choose a reason for hiding this comment

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

I believe that this new validations creates a breaking change

encode: validators.function,
serialize: validators.function
}, false)
Fixed Show fixed Hide fixed
}

// Set config.method
config.method = (config.method || this.defaults.method || 'get').toLowerCase();

Expand Down
18 changes: 13 additions & 5 deletions lib/helpers/buildURL.js
Expand Up @@ -44,12 +44,20 @@ export default function buildURL(url, params, options) {

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

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

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

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

if (serializedParams) {
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 () {
const params = {
x: 1
}

const options = {
serialize: (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 @@ -21,7 +21,8 @@ const config: AxiosRequestConfig = {
params: { id: 12345 },
paramsSerializer: {
indexes: true,
encode: (value) => value
encode: (value) => value,
serialize: (value, options) => String(value)
},
data: { foo: 'bar' },
timeout: 10000,
Expand Down