diff --git a/index.d.ts b/index.d.ts index 3be6df6f5b..d125d0bb80 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; diff --git a/lib/core/Axios.js b/lib/core/Axios.js index a7cf75c959..31f8b531dc 100644 --- a/lib/core/Axios.js +++ b/lib/core/Axios.js @@ -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, { @@ -57,6 +57,13 @@ class Axios { }, false); } + if (paramsSerializer !== undefined) { + validator.assertOptions(paramsSerializer, { + encode: validators.function, + serialize: validators.function + }, true); + } + // Set config.method config.method = (config.method || this.defaults.method || 'get').toLowerCase(); diff --git a/lib/helpers/buildURL.js b/lib/helpers/buildURL.js index 693b85f8b6..5256194170 100644 --- a/lib/helpers/buildURL.js +++ b/lib/helpers/buildURL.js @@ -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; diff --git a/test/specs/helpers/buildURL.spec.js b/test/specs/helpers/buildURL.spec.js index 7e33417ea3..b6ea22d0b7 100644 --- a/test/specs/helpers/buildURL.spec.js +++ b/test/specs/helpers/buildURL.spec.js @@ -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'); + }); }); diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index 859adfa0a5..1ba5a955e2 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -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,