Skip to content

Commit

Permalink
fix(params): re-added the ability to set the function as `paramsSeria…
Browse files Browse the repository at this point in the history
…lizer` config; (#5633)
  • Loading branch information
DigitalBrainJS committed Apr 5, 2023
1 parent e7decef commit a56c866
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.d.cts
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -311,7 +311,7 @@ export interface AxiosRequestConfig<D = any> {
transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
headers?: (RawAxiosRequestHeaders & MethodsHeaders) | AxiosHeaders;
params?: any;
paramsSerializer?: ParamsSerializerOptions;
paramsSerializer?: ParamsSerializerOptions | CustomParamsSerializer;
data?: D;
timeout?: Milliseconds;
timeoutErrorMessage?: string;
Expand Down
16 changes: 11 additions & 5 deletions lib/core/Axios.js
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -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');
});
});

0 comments on commit a56c866

Please sign in to comment.