2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

feat: added custom params serializer support; (#5113)

* Added custom params serializer support;

* Added missed semicolon;
Fixed allowUnknown option;

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Dmitriy Mozgovoy
2022-10-13 23:12:10 +03:00
committed by GitHub
parent ed19414b33
commit 9d4b016dde
5 changed files with 44 additions and 7 deletions
Vendored
+5
View File
@@ -247,8 +247,13 @@ export interface ParamEncoder {
(value: any, defaultEncoder: (value: any) => any): any; (value: any, defaultEncoder: (value: any) => any): any;
} }
export interface CustomParamsSerializer {
(params: object, options?: ParamsSerializerOptions): string;
}
export interface ParamsSerializerOptions extends SerializerOptions { export interface ParamsSerializerOptions extends SerializerOptions {
encode?: ParamEncoder; encode?: ParamEncoder;
serialize?: CustomParamsSerializer;
} }
type MaxUploadRate = number; type MaxUploadRate = number;
+8 -1
View File
@@ -47,7 +47,7 @@ class Axios {
config = mergeConfig(this.defaults, config); config = mergeConfig(this.defaults, config);
const transitional = config.transitional; const {transitional, paramsSerializer} = config;
if (transitional !== undefined) { if (transitional !== undefined) {
validator.assertOptions(transitional, { validator.assertOptions(transitional, {
@@ -57,6 +57,13 @@ class Axios {
}, false); }, false);
} }
if (paramsSerializer !== undefined) {
validator.assertOptions(paramsSerializer, {
encode: validators.function,
serialize: validators.function
}, true);
}
// Set config.method // Set config.method
config.method = (config.method || this.defaults.method || 'get').toLowerCase(); config.method = (config.method || this.defaults.method || 'get').toLowerCase();
+13 -5
View File
@@ -44,12 +44,20 @@ export default function buildURL(url, params, options) {
const _encode = options && options.encode || encode; const _encode = options && options.encode || encode;
const serializerParams = utils.isURLSearchParams(params) ? const serializeFn = options && options.serialize;
params.toString() :
new AxiosURLSearchParams(params, options).toString(_encode);
if (serializerParams) { let serializedParams;
url += (url.indexOf('?') === -1 ? '?' : '&') + serializerParams;
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; return url;
+16
View File
@@ -63,4 +63,20 @@ describe('helpers::buildURL', function () {
it('should support URLSearchParams', function () { it('should support URLSearchParams', function () {
expect(buildURL('/foo', new URLSearchParams('bar=baz'))).toEqual('/foo?bar=baz'); 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');
});
}); });
+2 -1
View File
@@ -21,7 +21,8 @@ const config: AxiosRequestConfig = {
params: { id: 12345 }, params: { id: 12345 },
paramsSerializer: { paramsSerializer: {
indexes: true, indexes: true,
encode: (value) => value encode: (value) => value,
serialize: (value, options) => String(value)
}, },
data: { foo: 'bar' }, data: { foo: 'bar' },
timeout: 10000, timeout: 10000,