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

Added axios.formToJSON method; (#4735)

* Draft

* Added `formDataToJSON` helper;
Added `axios.formToJSON` method;
Added client tests;

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Dmitriy Mozgovoy
2022-05-25 09:21:40 +03:00
committed by GitHub
parent 934f390cc3
commit c008e57fe4
8 changed files with 195 additions and 10 deletions
+50
View File
@@ -0,0 +1,50 @@
var formDataToJSON = require('../../../lib/helpers/formDataToJSON');
describe('formDataToJSON', function () {
it('should convert a FormData Object to JSON Object', function () {
const formData = new FormData();
formData.append('foo[bar][baz]', '123');
expect(formDataToJSON(formData)).toEqual({
foo: {
bar: {
baz: '123'
}
}
});
});
it('should convert repeatable values as an array', function () {
const formData = new FormData();
formData.append('foo', '1');
formData.append('foo', '2');
expect(formDataToJSON(formData)).toEqual({
foo: ['1', '2']
});
});
it('should convert props with empty brackets to arrays', function () {
const formData = new FormData();
formData.append('foo[]', '1');
formData.append('foo[]', '2');
expect(formDataToJSON(formData)).toEqual({
foo: ['1', '2']
});
});
it('should supported indexed arrays', function () {
const formData = new FormData();
formData.append('foo[0]', '1');
formData.append('foo[1]', '2');
expect(formDataToJSON(formData)).toEqual({
foo: ['1', '2']
});
});
});
+2 -1
View File
@@ -25,7 +25,8 @@ describe('instance', function () {
'isAxiosError',
'VERSION',
'default',
'toFormData'
'toFormData',
'formToJSON'
].indexOf(prop) > -1) {
continue;
}