Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added enhanced toFormData implementation with additional options #4704

Merged
merged 2 commits into from
May 11, 2022

Conversation

DigitalBrainJS
Copy link
Collaborator

@DigitalBrainJS DigitalBrainJS commented May 11, 2022

  • Used bracket notation instead of dot notation (breaking change, but I don't think there are already projects that use this feature)
  • Array-like objects will expand automatically. [] ending is not necessary.
  • Added config.formSerializer: object config to handle rare cases:
    • indexes option to control flat arrays serialization:
      • null - don't add brackets (arr: 1, arr: 2, arr: 3)
      • false(default) - add empty brackets (arr[]: 1, arr[]: 2, arr[]: 3)
      • true - add brackets with indexes (arr[0]: 1, arr[1]: 2, arr[2]: 3)
    • metaTokens: boolean = true option to control the special ending (e.g user{}: '{"name": "John"}')
    • visitor: Function - user-defined visitor function that will be called recursively to serialize the data object
    • dots: boolean = false - use dot notation instead of brackets
  • Added express.js/multer tests

The default config allows FormData to be processed by express.js out of the box;

const obj = {
  x: 1,
  arr: [1, 2, 3],
  arr2: [1, [2], 3],
  users: [{name: 'Peter', surname: 'Griffin'}, {name: 'Thomas', surname: 'Anderson'}],
  'obj2{}': [{x:1}]
};

The following steps will be executed by the Axios serializer internally:

const formData= new FormData();
formData.append('x', '1');
formData.append('arr[]', '1');
formData.append('arr[]', '2');
formData.append('arr[]', '3');
formData.append('arr2[0]', '1');
formData.append('arr2[1][0]', '2');
formData.append('arr2[2]', '3');
formData.append('users[0][name]', 'Peter');
formData.append('users[0][surname]', 'Griffin');
formData.append('users[1][name]', 'Thomas');
formData.append('users[1][surname]', 'Anderson');
formData.append('obj2{}', '[{"x":1}]');

Closes #4698

DigitalBrainJS and others added 2 commits May 11, 2022 19:50
…pport;

Updated default notation for arrays and objects to bracket style;
Added `multer/express.js` tests;
Updated README.md;
@jasonsaayman jasonsaayman merged commit 807918b into axios:master May 11, 2022
@jasonsaayman
Copy link
Member

Cool thanks, since it is breaking this will go into v1, which I will release to pre-release soonish

@plehnen
Copy link

plehnen commented May 16, 2022

Hey, "soonish" sounds like it might still take some time. Is it possible to opt-in to this already via the newly introduced config.formSerializer: object config? I would love to migrate to use this feature, but it's blocking me because I need the indexes option to be null. And via config this wouldn't be breaking.

@rannien
Copy link

rannien commented Sep 22, 2022

Hey, could you please give an approximate time when the v1.x.x will be stable and released? It would be really nice to use this feature.

@jasonsaayman
Copy link
Member

It will be released this weekend. You can always use it by just installing the pre-release via NPM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

toFormData should (optionally?) support array serialization (w/o [] suffix)
4 participants