2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Fix: Don't add null values to query string. (#5108)

* feat: add  boolean flag to mimic pre 1.x behavior for paramsSerializer custom function

* chore: update ParamsSerializer Readme

* fix: dont slice hash off URL if not appending params

* Omit nulls from formData serialization

* fix: dont add nulls or undefined values to arrays either

* readme update

* fix test

* chore: documentation

* chore: do TS properly

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
ChronosMasterOfAllTime
2022-10-15 08:01:59 -05:00
committed by GitHub
parent ef1c48a476
commit b0ebf9fcac
6 changed files with 37 additions and 15 deletions
+23 -2
View File
@@ -8,10 +8,31 @@ describe('helpers::buildURL', function () {
it('should support params', function () {
expect(buildURL('/foo', {
foo: 'bar'
foo: 'bar',
isUndefined: undefined,
isNull: null
})).toEqual('/foo?foo=bar');
});
it('should support sending raw params to custom serializer func', function () {
const serializer = sinon.stub();
const params = { foo: "bar" };
serializer.returns("foo=bar");
expect(
buildURL(
"/foo",
{
foo: "bar",
},
{
serialize: serializer,
}
)
).toEqual("/foo?foo=bar");
expect(serializer.calledOnce).toBe(true);
expect(serializer.calledWith(params)).toBe(true);
});
it('should support object params', function () {
expect(buildURL('/foo', {
foo: {
@@ -30,7 +51,7 @@ describe('helpers::buildURL', function () {
it('should support array params', function () {
expect(buildURL('/foo', {
foo: ['bar', 'baz']
foo: ['bar', 'baz', null, undefined]
})).toEqual('/foo?foo[]=bar&foo[]=baz');
});
+3 -3
View File
@@ -6,7 +6,7 @@ import axios, {
AxiosAdapter,
Cancel,
CancelTokenSource,
Canceler, AxiosProgressEvent
Canceler, AxiosProgressEvent, ParamsSerializerOptions
} from 'axios';
const config: AxiosRequestConfig = {
@@ -21,8 +21,8 @@ const config: AxiosRequestConfig = {
params: { id: 12345 },
paramsSerializer: {
indexes: true,
encode: (value) => value,
serialize: (value, options) => String(value)
encode: (value: any) => value,
serialize: (value: Record<string, any>, options?: ParamsSerializerOptions) => String(value)
},
data: { foo: 'bar' },
timeout: 10000,