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

Fixed isFormData predicate; #4413

Merged
merged 2 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.d.ts
Expand Up @@ -107,6 +107,9 @@ export interface AxiosRequestConfig<D = any> {
transitional?: TransitionalOptions;
signal?: AbortSignal;
insecureHTTPParser?: boolean;
env?: {
FormData?: new (...args: any[]) => object;
};
}

export interface HeadersDefaults {
Expand Down
4 changes: 3 additions & 1 deletion lib/adapters/http.js
Expand Up @@ -87,7 +87,9 @@ module.exports = function httpAdapter(config) {
headers['User-Agent'] = 'axios/' + VERSION;
}

if (data && !utils.isStream(data)) {
if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
Object.assign(headers, data.getHeaders());
} else if (data && !utils.isStream(data)) {
if (Buffer.isBuffer(data)) {
// Nothing to do...
} else if (utils.isArrayBuffer(data)) {
Expand Down
12 changes: 11 additions & 1 deletion lib/defaults.js
Expand Up @@ -3,6 +3,7 @@
var utils = require('./utils');
var normalizeHeaderName = require('./helpers/normalizeHeaderName');
var enhanceError = require('./core/enhanceError');
var toFormData = require('./helpers/toFormData');

var DEFAULT_CONTENT_TYPE = {
'Content-Type': 'application/x-www-form-urlencoded'
Expand Down Expand Up @@ -71,10 +72,17 @@ var defaults = {
setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
return data.toString();
}
if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {

var isObjectPayload = utils.isObject(data);
var contentType = headers && headers['Content-Type'];

if ( isObjectPayload && contentType === 'multipart/form-data' ) {
return toFormData(data, new (this.env && this.env.FormData || FormData));
} else if ( isObjectPayload || contentType === 'application/json' ) {
setContentTypeIfUnset(headers, 'application/json');
return stringifySafely(data);
}

return data;
}],

Expand Down Expand Up @@ -112,6 +120,8 @@ var defaults = {
maxContentLength: -1,
maxBodyLength: -1,

env: {},

validateStatus: function validateStatus(status) {
return status >= 200 && status < 300;
},
Expand Down
10 changes: 7 additions & 3 deletions lib/helpers/toFormData.js
@@ -1,5 +1,7 @@
'use strict';

var utils = require('../utils');

function combinedKey(parentKey, elKey) {
return parentKey + '.' + elKey;
}
Expand All @@ -11,7 +13,7 @@ function buildFormData(formData, data, parentKey) {
});
} else if (
typeof data === 'object' &&
!(data instanceof File || data === null)
!(utils.isFile(data) || data === null)
) {
Object.keys(data).forEach(function buildObject(key) {
buildFormData(
Expand Down Expand Up @@ -44,10 +46,12 @@ function buildFormData(formData, data, parentKey) {
* type FormVal = FormDataNest | FormDataPrimitive
*
* @param {FormVal} data
* @param {?Object} formData
*/

module.exports = function getFormData(data) {
var formData = new FormData();
module.exports = function getFormData(data, formData) {
// eslint-disable-next-line no-param-reassign
formData = formData || new FormData();

buildFormData(formData, data);

Expand Down
24 changes: 15 additions & 9 deletions lib/utils.js
Expand Up @@ -47,15 +47,6 @@ function isArrayBuffer(val) {
return toString.call(val) === '[object ArrayBuffer]';
}

/**
* Determine if a value is a FormData
*
* @param {Object} val The value to test
* @returns {boolean} True if value is an FormData, otherwise false
*/
function isFormData(val) {
return toString.call(val) === '[object FormData]';
}

/**
* Determine if a value is a view on an ArrayBuffer
Expand Down Expand Up @@ -168,6 +159,21 @@ function isStream(val) {
return isObject(val) && isFunction(val.pipe);
}

/**
* Determine if a value is a FormData
*
* @param {Object} thing The value to test
* @returns {boolean} True if value is an FormData, otherwise false
*/
function isFormData(thing) {
var pattern = '[object FormData]';
return thing && (
(typeof FormData === 'function' && thing instanceof FormData) ||
toString.call(thing) === pattern ||
(isFunction(thing.toString) && thing.toString() === pattern)
);
}

/**
* Determine if a value is a URLSearchParams object
*
Expand Down
2 changes: 1 addition & 1 deletion test/specs/helpers/toFormData.spec.js
@@ -1,7 +1,7 @@
var toFormData = require("../../../lib/helpers/toFormData");

describe("toFormData", function () {
it("Convert nested data object to FormDAta", function () {
it("Convert nested data object to FormData", function () {
var o = {
val: 123,
nested: {
Expand Down