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

Improved FormData support; #4448

Merged
merged 12 commits into from Mar 14, 2022

Conversation

DigitalBrainJS
Copy link
Contributor

@DigitalBrainJS DigitalBrainJS commented Feb 6, 2022

CODE

  • Fixed isFormData predicate;
  • Added form-data as a dependency for node.js build only. It is used by default to handle the FormData payload;
  • Added shortcut methods postForm, putForm, patchForm
  • Reworked toFormData serializer;
    • ArrayBuffer and/or TypedArray will be converted to a Blob|Buffer instance
    • Added support for special field endings:
      • [] - unwrap the array like object as separate fields with the same key
      • {} - serialize the value with JSON.stringify
axios.postForm('https://httpbin.org/post', {
  'myObj{}': {x: 1, s: "foo"},
  'files[]': document.querySelector('#fileInput').files 
};
  • You can pass the FileList object directly as the payload:
await axios.postForm('https://httpbin.org/post', document.querySelector('#fileInput').files)
  • Exposed toFormData helper as a static method;
  • Added kindOf, kindOfTest, endsWith, isTypedArray util;

TESTS

  • Added test for Fixed isFormData predicate; #4413;
  • Added test for automatic data object serialization to a FormData payload when the request Content-Type is multipart/form-data;
  • Added backend test for passing FormData as a payload;

README

  • Added FormData automatic serialization section to Readme.md;
  • Updated cancellation section;

DEV

  • Added cross-env package to run the build scripts on Windows systems;
  • Added formidable to test FormData payload;

Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment through `form-data` package;
Refactored cancellation section;
…rmData

� Conflicts:
�	lib/defaults.js
Copy link
Contributor

@carpben carpben left a comment

Added tests are excellent and valuable 🙌.

At present I suggest we don't recommend auto-serialization of form-data as the default method of choice. Instead, let's move it to be after the manual section and say that we support it now. After we have more tests, and after it is well accepted by the community, let's adapt and recommend it by default.
Currently, conversion of files in node will not work. This is solved by #4469 but tests and feedback are needed in this area.

This Pr goes way beyond adding tests to FormData payload as the title suggest. It includes FormData documentation, a whole section of documentation about AbortController, adding import from "form-data" to "Node" only, and what seems like unrelated changes to scripts (cross-env). I guess it is obvious why this is less than ideal.
While the contribution is great it would be even better if it will be separated to PRs that are scoped to a certain topic/domain.

package.json Show resolved Hide resolved
test/unit/adapters/http.js Show resolved Hide resolved
test/specs/defaults.spec.js Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
test/unit/utils/isFormData.js Show resolved Hide resolved
test/unit/adapters/http.js Outdated Show resolved Hide resolved
test/unit/adapters/http.js Show resolved Hide resolved
@jasonsaayman
Copy link
Member

jasonsaayman commented Feb 13, 2022

@DigitalBrainJS can you have a look at the review please?

Expose toFormData helper as a static method;
Refactored transform request;
Added kindOf, kindOfTest, endsWith, isTypedArray util;
Refactored utils.js to use kindOf for tests;
…ormData

� Conflicts:
�	lib/adapters/http.js
�	lib/defaults.js
�	lib/helpers/toFormData.js
�	package-lock.json
�	test/specs/helpers/toFormData.spec.js
# Conflicts:
#	package-lock.json
#	package.json
Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment using `form-data` package;

(cherry picked from commit 73e3bdb)
… Form;

Added ability to submit FileList object as a FormData;
Updated README.md;
@DigitalBrainJS DigitalBrainJS changed the title Added tests for FormData payload; Improved FormData support; Feb 15, 2022
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.

None yet

3 participants