Skip to content

Commit 2f1e818

Browse files
committed
Merge branch 'cookieMr-master'
2 parents 1163588 + 95295f6 commit 2f1e818

File tree

11 files changed

+51
-50
lines changed

11 files changed

+51
-50
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
[![npm downloads](https://img.shields.io/npm/dm/axios.svg?style=flat-square)](http://npm-stat.com/charts.html?package=axios)
1010
[![gitter chat](https://img.shields.io/gitter/room/mzabriskie/axios.svg?style=flat-square)](https://gitter.im/mzabriskie/axios)
1111
[![code helpers](https://www.codetriage.com/axios/axios/badges/users.svg)](https://www.codetriage.com/axios/axios)
12+
[![Known Vulnerabilities](https://snyk.io/test/npm/axios/badge.svg)](https://snyk.io/test/npm/axios)
1213

1314
Promise based HTTP client for the browser and node.js
1415

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ export interface AxiosRequestConfig<D = any> {
107107
transitional?: TransitionalOptions;
108108
signal?: AbortSignal;
109109
insecureHTTPParser?: boolean;
110+
env?: {
111+
FormData?: new (...args: any[]) => object;
112+
};
110113
}
111114

112115
export interface HeadersDefaults {

lib/adapters/http.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ module.exports = function httpAdapter(config) {
8787
headers['User-Agent'] = 'axios/' + VERSION;
8888
}
8989

90-
if (data && !utils.isStream(data)) {
90+
if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
91+
Object.assign(headers, data.getHeaders());
92+
} else if (data && !utils.isStream(data)) {
9193
if (Buffer.isBuffer(data)) {
9294
// Nothing to do...
9395
} else if (utils.isArrayBuffer(data)) {

lib/core/Axios.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ Axios.prototype.request = function request(configOrUrl, config) {
3636
config = configOrUrl || {};
3737
}
3838

39-
if (!config.url) {
40-
throw new Error('Provided config url is not valid');
41-
}
42-
4339
config = mergeConfig(this.defaults, config);
4440

4541
// Set config.method
@@ -122,9 +118,6 @@ Axios.prototype.request = function request(configOrUrl, config) {
122118
};
123119

124120
Axios.prototype.getUri = function getUri(config) {
125-
if (!config.url) {
126-
throw new Error('Provided config url is not valid');
127-
}
128121
config = mergeConfig(this.defaults, config);
129122
return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
130123
};

lib/defaults.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var utils = require('./utils');
44
var normalizeHeaderName = require('./helpers/normalizeHeaderName');
55
var enhanceError = require('./core/enhanceError');
6+
var toFormData = require('./helpers/toFormData');
67

78
var DEFAULT_CONTENT_TYPE = {
89
'Content-Type': 'application/x-www-form-urlencoded'
@@ -71,10 +72,17 @@ var defaults = {
7172
setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
7273
return data.toString();
7374
}
74-
if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {
75+
76+
var isObjectPayload = utils.isObject(data);
77+
var contentType = headers && headers['Content-Type'];
78+
79+
if ( isObjectPayload && contentType === 'multipart/form-data' ) {
80+
return toFormData(data, new (this.env && this.env.FormData || FormData));
81+
} else if ( isObjectPayload || contentType === 'application/json' ) {
7582
setContentTypeIfUnset(headers, 'application/json');
7683
return stringifySafely(data);
7784
}
85+
7886
return data;
7987
}],
8088

@@ -112,6 +120,8 @@ var defaults = {
112120
maxContentLength: -1,
113121
maxBodyLength: -1,
114122

123+
env: {},
124+
115125
validateStatus: function validateStatus(status) {
116126
return status >= 200 && status < 300;
117127
},

lib/helpers/toFormData.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var utils = require('../utils');
4+
35
function combinedKey(parentKey, elKey) {
46
return parentKey + '.' + elKey;
57
}
@@ -11,7 +13,7 @@ function buildFormData(formData, data, parentKey) {
1113
});
1214
} else if (
1315
typeof data === 'object' &&
14-
!(data instanceof File || data === null)
16+
!(utils.isFile(data) || data === null)
1517
) {
1618
Object.keys(data).forEach(function buildObject(key) {
1719
buildFormData(
@@ -44,10 +46,12 @@ function buildFormData(formData, data, parentKey) {
4446
* type FormVal = FormDataNest | FormDataPrimitive
4547
*
4648
* @param {FormVal} data
49+
* @param {?Object} formData
4750
*/
4851

49-
module.exports = function getFormData(data) {
50-
var formData = new FormData();
52+
module.exports = function getFormData(data, formData) {
53+
// eslint-disable-next-line no-param-reassign
54+
formData = formData || new FormData();
5155

5256
buildFormData(formData, data);
5357

lib/utils.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@ function isArrayBuffer(val) {
4747
return toString.call(val) === '[object ArrayBuffer]';
4848
}
4949

50-
/**
51-
* Determine if a value is a FormData
52-
*
53-
* @param {Object} val The value to test
54-
* @returns {boolean} True if value is an FormData, otherwise false
55-
*/
56-
function isFormData(val) {
57-
return toString.call(val) === '[object FormData]';
58-
}
5950

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

162+
/**
163+
* Determine if a value is a FormData
164+
*
165+
* @param {Object} thing The value to test
166+
* @returns {boolean} True if value is an FormData, otherwise false
167+
*/
168+
function isFormData(thing) {
169+
var pattern = '[object FormData]';
170+
return thing && (
171+
(typeof FormData === 'function' && thing instanceof FormData) ||
172+
toString.call(thing) === pattern ||
173+
(isFunction(thing.toString) && thing.toString() === pattern)
174+
);
175+
}
176+
171177
/**
172178
* Determine if a value is a URLSearchParams object
173179
*

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"unpkg": "dist/axios.min.js",
7676
"typings": "./index.d.ts",
7777
"dependencies": {
78-
"follow-redirects": "^1.14.7"
78+
"follow-redirects": "^1.14.8"
7979
},
8080
"bundlesize": [
8181
{

test/specs/helpers/toFormData.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var toFormData = require("../../../lib/helpers/toFormData");
22

33
describe("toFormData", function () {
4-
it("Convert nested data object to FormDAta", function () {
4+
it("Convert nested data object to FormData", function () {
55
var o = {
66
val: 123,
77
nested: {

0 commit comments

Comments
 (0)