mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
Merging master
This commit is contained in:
@@ -150,6 +150,7 @@ This is the available config options for making requests. Only the `url` is requ
|
|||||||
|
|
||||||
// `transformRequest` allows changes to the request data before it is sent to the server
|
// `transformRequest` allows changes to the request data before it is sent to the server
|
||||||
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||||
|
// The last function in the array must return a string or an ArrayBuffer
|
||||||
transformRequest: [function (data) {
|
transformRequest: [function (data) {
|
||||||
// Do whatever you want to transform the data
|
// Do whatever you want to transform the data
|
||||||
|
|
||||||
@@ -174,6 +175,7 @@ This is the available config options for making requests. Only the `url` is requ
|
|||||||
|
|
||||||
// `data` is the data to be sent as the request body
|
// `data` is the data to be sent as the request body
|
||||||
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||||
|
// When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash
|
||||||
data: {
|
data: {
|
||||||
firstName: 'Fred'
|
firstName: 'Fred'
|
||||||
},
|
},
|
||||||
|
|||||||
Vendored
+40
-4
@@ -161,10 +161,16 @@ var axios =
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transformRequest: [function (data) {
|
transformRequest: [function (data) {
|
||||||
return utils.isObject(data) &&
|
if (utils.isArrayBuffer(data)) {
|
||||||
!utils.isFile(data) &&
|
return data;
|
||||||
!utils.isBlob(data) ?
|
}
|
||||||
JSON.stringify(data) : data;
|
if (utils.isArrayBufferView(data)) {
|
||||||
|
return data.buffer;
|
||||||
|
}
|
||||||
|
if (utils.isObject(data) && !utils.isFile(data) && !utils.isBlob(data)) {
|
||||||
|
return JSON.stringify(data);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
}],
|
}],
|
||||||
|
|
||||||
transformResponse: [function (data) {
|
transformResponse: [function (data) {
|
||||||
@@ -208,6 +214,30 @@ var axios =
|
|||||||
return toString.call(val) === '[object Array]';
|
return toString.call(val) === '[object Array]';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is an ArrayBuffer
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
||||||
|
*/
|
||||||
|
function isArrayBuffer(val) {
|
||||||
|
return toString.call(val) === '[object ArrayBuffer]';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is a view on an ArrayBuffer
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
||||||
|
*/
|
||||||
|
function isArrayBufferView(val) {
|
||||||
|
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
||||||
|
return ArrayBuffer.isView(val);
|
||||||
|
} else {
|
||||||
|
return (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a value is a String
|
* Determine if a value is a String
|
||||||
*
|
*
|
||||||
@@ -349,6 +379,8 @@ var axios =
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isArray: isArray,
|
isArray: isArray,
|
||||||
|
isArrayBuffer: isArrayBuffer,
|
||||||
|
isArrayBufferView: isArrayBufferView,
|
||||||
isString: isString,
|
isString: isString,
|
||||||
isNumber: isNumber,
|
isNumber: isNumber,
|
||||||
isObject: isObject,
|
isObject: isObject,
|
||||||
@@ -483,6 +515,10 @@ var axios =
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (utils.isArrayBuffer(data)) {
|
||||||
|
data = new DataView(data);
|
||||||
|
}
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
request.send(data);
|
request.send(data);
|
||||||
};
|
};
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -4,6 +4,7 @@ var transformData = require('./../transformData');
|
|||||||
var utils = require('./../utils');
|
var utils = require('./../utils');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
var Buffer = require('buffer').Buffer;
|
||||||
|
|
||||||
module.exports = function httpAdapter(resolve, reject, config) {
|
module.exports = function httpAdapter(resolve, reject, config) {
|
||||||
// Transform request data
|
// Transform request data
|
||||||
@@ -13,6 +14,10 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
config.transformRequest
|
config.transformRequest
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (utils.isArrayBuffer(data)) {
|
||||||
|
data = new Buffer(new Uint8Array(data));
|
||||||
|
}
|
||||||
|
|
||||||
// Merge headers
|
// Merge headers
|
||||||
var headers = utils.merge(
|
var headers = utils.merge(
|
||||||
defaults.headers.common,
|
defaults.headers.common,
|
||||||
|
|||||||
@@ -87,6 +87,10 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (utils.isArrayBuffer(data)) {
|
||||||
|
data = new DataView(data);
|
||||||
|
}
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
request.send(data);
|
request.send(data);
|
||||||
};
|
};
|
||||||
+10
-4
@@ -11,10 +11,16 @@ var CONTENT_TYPE_APPLICATION_JSON = {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transformRequest: [function (data) {
|
transformRequest: [function (data) {
|
||||||
return utils.isObject(data) &&
|
if (utils.isArrayBuffer(data)) {
|
||||||
!utils.isFile(data) &&
|
return data;
|
||||||
!utils.isBlob(data) ?
|
}
|
||||||
JSON.stringify(data) : data;
|
if (utils.isArrayBufferView(data)) {
|
||||||
|
return data.buffer;
|
||||||
|
}
|
||||||
|
if (utils.isObject(data) && !utils.isFile(data) && !utils.isBlob(data)) {
|
||||||
|
return JSON.stringify(data);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
}],
|
}],
|
||||||
|
|
||||||
transformResponse: [function (data) {
|
transformResponse: [function (data) {
|
||||||
|
|||||||
@@ -12,6 +12,30 @@ function isArray(val) {
|
|||||||
return toString.call(val) === '[object Array]';
|
return toString.call(val) === '[object Array]';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is an ArrayBuffer
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
||||||
|
*/
|
||||||
|
function isArrayBuffer(val) {
|
||||||
|
return toString.call(val) === '[object ArrayBuffer]';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is a view on an ArrayBuffer
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
||||||
|
*/
|
||||||
|
function isArrayBufferView(val) {
|
||||||
|
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
||||||
|
return ArrayBuffer.isView(val);
|
||||||
|
} else {
|
||||||
|
return (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a value is a String
|
* Determine if a value is a String
|
||||||
*
|
*
|
||||||
@@ -153,6 +177,8 @@ function merge(obj1/*, obj2, obj3, ...*/) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isArray: isArray,
|
isArray: isArray,
|
||||||
|
isArrayBuffer: isArrayBuffer,
|
||||||
|
isArrayBufferView: isArrayBufferView,
|
||||||
isString: isString,
|
isString: isString,
|
||||||
isNumber: isNumber,
|
isNumber: isNumber,
|
||||||
isObject: isObject,
|
isObject: isObject,
|
||||||
|
|||||||
@@ -45,6 +45,42 @@ describe('wrapper', function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support binary data as array buffer', function () {
|
||||||
|
var input = new Int8Array(2);
|
||||||
|
input[0] = 1;
|
||||||
|
input[1] = 2;
|
||||||
|
|
||||||
|
axios({
|
||||||
|
method: 'post',
|
||||||
|
url: '/foo',
|
||||||
|
data: input.buffer
|
||||||
|
});
|
||||||
|
|
||||||
|
var request = jasmine.Ajax.requests.mostRecent();
|
||||||
|
var output = new Int8Array(request.params.buffer);
|
||||||
|
expect(output.length).toEqual(2);
|
||||||
|
expect(output[0]).toEqual(1);
|
||||||
|
expect(output[1]).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support binary data as array buffer view', function () {
|
||||||
|
var input = new Int8Array(2);
|
||||||
|
input[0] = 1;
|
||||||
|
input[1] = 2;
|
||||||
|
|
||||||
|
axios({
|
||||||
|
method: 'post',
|
||||||
|
url: '/foo',
|
||||||
|
data: input
|
||||||
|
});
|
||||||
|
|
||||||
|
var request = jasmine.Ajax.requests.mostRecent();
|
||||||
|
var output = new Int8Array(request.params.buffer);
|
||||||
|
expect(output.length).toEqual(2);
|
||||||
|
expect(output[0]).toEqual(1);
|
||||||
|
expect(output[1]).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
it('should remove content-type if data is empty', function () {
|
it('should remove content-type if data is empty', function () {
|
||||||
axios({
|
axios({
|
||||||
method: 'post',
|
method: 'post',
|
||||||
|
|||||||
Reference in New Issue
Block a user