2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00
Files
axios/lib/defaults.js
T
Mathieu Bruyen 095a204c5b Allow ArrayBuffer and related views as data
In order to push binary data under the form of ArrayBuffer and
its related views (Int8Array, ...) one needs not to stringify
those.

For the XHR adapter there is nothing to do as it natively supports
ArrayBuffer in req.send().

Node's http adapter supports only string or Buffer thus a
transformation to Buffer is required before setting content length
header.
2014-09-18 09:17:46 +02:00

47 lines
1.1 KiB
JavaScript

'use strict';
var utils = require('./utils');
var JSON_START = /^\s*(\[|\{[^\{])/;
var JSON_END = /[\}\]]\s*$/;
var PROTECTION_PREFIX = /^\)\]\}',?\n/;
var CONTENT_TYPE_APPLICATION_JSON = {
'Content-Type': 'application/json;charset=utf-8'
};
module.exports = {
transformRequest: [function (data) {
if (utils.isArrayBuffer(data)) {
return 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) {
if (typeof data === 'string') {
data = data.replace(PROTECTION_PREFIX, '');
if (JSON_START.test(data) && JSON_END.test(data)) {
data = JSON.parse(data);
}
}
return data;
}],
headers: {
common: {
'Accept': 'application/json, text/plain, */*'
},
patch: utils.merge(CONTENT_TYPE_APPLICATION_JSON),
post: utils.merge(CONTENT_TYPE_APPLICATION_JSON),
put: utils.merge(CONTENT_TYPE_APPLICATION_JSON)
},
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN'
};