diff --git a/index.d.cts b/index.d.cts index 0aee7aa201..b1c47a39e4 100644 --- a/index.d.cts +++ b/index.d.cts @@ -370,7 +370,7 @@ declare namespace axios { transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[]; headers?: (RawAxiosRequestHeaders & MethodsHeaders) | AxiosHeaders; params?: any; - paramsSerializer?: ParamsSerializerOptions; + paramsSerializer?: ParamsSerializerOptions | CustomParamsSerializer; data?: D; timeout?: Milliseconds; timeoutErrorMessage?: string; diff --git a/index.d.ts b/index.d.ts index be5f182cc7..e9c9f825a3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -311,7 +311,7 @@ export interface AxiosRequestConfig { transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[]; headers?: (RawAxiosRequestHeaders & MethodsHeaders) | AxiosHeaders; params?: any; - paramsSerializer?: ParamsSerializerOptions; + paramsSerializer?: ParamsSerializerOptions | CustomParamsSerializer; data?: D; timeout?: Milliseconds; timeoutErrorMessage?: string; diff --git a/lib/core/Axios.js b/lib/core/Axios.js index ff602ba52c..7a6e6f5d44 100644 --- a/lib/core/Axios.js +++ b/lib/core/Axios.js @@ -57,11 +57,17 @@ class Axios { }, false); } - if (paramsSerializer !== undefined) { - validator.assertOptions(paramsSerializer, { - encode: validators.function, - serialize: validators.function - }, true); + if (paramsSerializer != null) { + if (utils.isFunction(paramsSerializer)) { + config.paramsSerializer = { + serialize: paramsSerializer + } + } else { + validator.assertOptions(paramsSerializer, { + encode: validators.function, + serialize: validators.function + }, true); + } } // Set config.method diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 5d6620abc3..769e4692da 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -2103,4 +2103,18 @@ describe('supports http with nodejs', function () { it('should properly handle synchronous errors inside the adapter', function () { return assert.rejects(() => axios.get('http://192.168.0.285'), /Invalid URL/); }); + + it('should support function as paramsSerializer value', async () => { + server = await startHTTPServer((req, res) => res.end(req.url)); + + const {data} = await axios.post(LOCAL_SERVER_URL, 'test', { + params: { + x: 1 + }, + paramsSerializer: () => 'foo', + maxRedirects: 0 + }); + + assert.strictEqual(data, '/?foo'); + }); });