mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
Moving utility functions into utils
This commit is contained in:
+1
-1
@@ -34,7 +34,7 @@ module.exports = function(grunt) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
nodeunit: {
|
nodeunit: {
|
||||||
all: ['test/unit/*.js']
|
all: ['test/unit/**/*.js']
|
||||||
},
|
},
|
||||||
|
|
||||||
webpack: generateWebpackConfig(),
|
webpack: generateWebpackConfig(),
|
||||||
|
|||||||
Vendored
+191
-134
File diff suppressed because one or more lines are too long
Vendored
+191
-134
File diff suppressed because one or more lines are too long
Vendored
+191
-134
File diff suppressed because one or more lines are too long
Vendored
+191
-134
File diff suppressed because one or more lines are too long
+8
-9
@@ -1,13 +1,12 @@
|
|||||||
var Promise = require('es6-promise').Promise;
|
var Promise = require('es6-promise').Promise;
|
||||||
var buildUrl = require('./buildUrl');
|
var buildUrl = require('./buildUrl');
|
||||||
var defaults = require('./defaults');
|
var defaults = require('./defaults');
|
||||||
var forEach = require('./forEach');
|
|
||||||
var merge = require('./merge');
|
|
||||||
var parseHeaders = require('./parseHeaders');
|
var parseHeaders = require('./parseHeaders');
|
||||||
var transformData = require('./transformData');
|
var transformData = require('./transformData');
|
||||||
|
var utils = require('./utils');
|
||||||
|
|
||||||
var axios = module.exports = function axios(options) {
|
var axios = module.exports = function axios(options) {
|
||||||
options = merge({
|
options = utils.merge({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
transformRequest: defaults.transformRequest,
|
transformRequest: defaults.transformRequest,
|
||||||
transformResponse: defaults.transformResponse
|
transformResponse: defaults.transformResponse
|
||||||
@@ -57,13 +56,13 @@ var axios = module.exports = function axios(options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Merge headers and add to request
|
// Merge headers and add to request
|
||||||
var headers = merge(
|
var headers = utils.merge(
|
||||||
defaults.headers.common,
|
defaults.headers.common,
|
||||||
defaults.headers[options.method] || {},
|
defaults.headers[options.method] || {},
|
||||||
options.headers || {}
|
options.headers || {}
|
||||||
);
|
);
|
||||||
|
|
||||||
forEach(headers, function (val, key) {
|
utils.forEach(headers, function (val, key) {
|
||||||
// Remove Content-Type if data is undefined
|
// Remove Content-Type if data is undefined
|
||||||
if (typeof data === 'undefined' && key.toLowerCase() === 'content-type') {
|
if (typeof data === 'undefined' && key.toLowerCase() === 'content-type') {
|
||||||
delete headers[key];
|
delete headers[key];
|
||||||
@@ -121,9 +120,9 @@ createShortMethods('delete', 'get', 'head');
|
|||||||
createShortMethodsWithData('post', 'put', 'patch');
|
createShortMethodsWithData('post', 'put', 'patch');
|
||||||
|
|
||||||
function createShortMethods() {
|
function createShortMethods() {
|
||||||
forEach(arguments, function (method) {
|
utils.forEach(arguments, function (method) {
|
||||||
axios[method] = function (url, options) {
|
axios[method] = function (url, options) {
|
||||||
return axios(merge(options || {}, {
|
return axios(utils.merge(options || {}, {
|
||||||
method: method,
|
method: method,
|
||||||
url: url
|
url: url
|
||||||
}));
|
}));
|
||||||
@@ -132,9 +131,9 @@ function createShortMethods() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createShortMethodsWithData() {
|
function createShortMethodsWithData() {
|
||||||
forEach(arguments, function (method) {
|
utils.forEach(arguments, function (method) {
|
||||||
axios[method] = function (url, data, options) {
|
axios[method] = function (url, data, options) {
|
||||||
return axios(merge(options || {}, {
|
return axios(utils.merge(options || {}, {
|
||||||
method: method,
|
method: method,
|
||||||
url: url,
|
url: url,
|
||||||
data: data
|
data: data
|
||||||
|
|||||||
+6
-6
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var forEach = require('./forEach');
|
var utils = require('./utils');
|
||||||
|
|
||||||
function encode(val) {
|
function encode(val) {
|
||||||
return encodeURIComponent(val).
|
return encodeURIComponent(val).
|
||||||
@@ -18,19 +18,19 @@ module.exports = function buildUrl(url, params) {
|
|||||||
|
|
||||||
var parts = [];
|
var parts = [];
|
||||||
|
|
||||||
forEach(params, function (val, key) {
|
utils.forEach(params, function (val, key) {
|
||||||
if (val === null || typeof val === 'undefined') {
|
if (val === null || typeof val === 'undefined') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Object.prototype.toString.call(val) !== '[object Array]') {
|
if (!utils.isArray(val)) {
|
||||||
val = [val];
|
val = [val];
|
||||||
}
|
}
|
||||||
|
|
||||||
forEach(val, function (v) {
|
utils.forEach(val, function (v) {
|
||||||
if (Object.prototype.toString.call(v) === '[object Date]') {
|
if (utils.isDate(v)) {
|
||||||
v = v.toISOString();
|
v = v.toISOString();
|
||||||
}
|
}
|
||||||
else if (typeof v === 'object') {
|
else if (utils.isObject(v)) {
|
||||||
v = JSON.stringify(v);
|
v = JSON.stringify(v);
|
||||||
}
|
}
|
||||||
parts.push(encode(key) + '=' + encode(v));
|
parts.push(encode(key) + '=' + encode(v));
|
||||||
|
|||||||
+7
-8
@@ -1,8 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var merge = require('./merge');
|
var utils = require('./utils');
|
||||||
|
|
||||||
var toString = Object.prototype.toString;
|
|
||||||
var JSON_START = /^\s*(\[|\{[^\{])/;
|
var JSON_START = /^\s*(\[|\{[^\{])/;
|
||||||
var JSON_END = /[\}\]]\s*$/;
|
var JSON_END = /[\}\]]\s*$/;
|
||||||
var PROTECTION_PREFIX = /^\)\]\}',?\n/;
|
var PROTECTION_PREFIX = /^\)\]\}',?\n/;
|
||||||
@@ -12,9 +11,9 @@ var CONTENT_TYPE_APPLICATION_JSON = {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transformRequest: [function (data) {
|
transformRequest: [function (data) {
|
||||||
return data !== null && typeof data === 'object' &&
|
return utils.isObject(data) &&
|
||||||
toString.call(data) !== '[object File]' &&
|
!utils.isFile(data) &&
|
||||||
toString.call(data) !== '[object Blob]' ?
|
!utils.isBlob(data) ?
|
||||||
JSON.stringify(data) : null;
|
JSON.stringify(data) : null;
|
||||||
}],
|
}],
|
||||||
|
|
||||||
@@ -32,9 +31,9 @@ module.exports = {
|
|||||||
common: {
|
common: {
|
||||||
'Accept': 'application/json, text/plain, */*'
|
'Accept': 'application/json, text/plain, */*'
|
||||||
},
|
},
|
||||||
patch: merge(CONTENT_TYPE_APPLICATION_JSON),
|
patch: utils.merge(CONTENT_TYPE_APPLICATION_JSON),
|
||||||
post: merge(CONTENT_TYPE_APPLICATION_JSON),
|
post: utils.merge(CONTENT_TYPE_APPLICATION_JSON),
|
||||||
put: merge(CONTENT_TYPE_APPLICATION_JSON)
|
put: utils.merge(CONTENT_TYPE_APPLICATION_JSON)
|
||||||
},
|
},
|
||||||
|
|
||||||
xsrfCookiName: 'XSRF-TOKEN',
|
xsrfCookiName: 'XSRF-TOKEN',
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
|
|
||||||
function isArrayLike(obj) {
|
|
||||||
return obj.constructor === Array || typeof obj.callee === 'function';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Iterate over an Array or an Object invoking a function for each item.
|
|
||||||
*
|
|
||||||
* If `obj` is an Array or arguments callback will be called passing
|
|
||||||
* the value, index, and complete array for each item.
|
|
||||||
*
|
|
||||||
* If 'obj' is an Object callback will be called passing
|
|
||||||
* the value, key, and complete object for each property.
|
|
||||||
*
|
|
||||||
* @param {Object|Array} obj The object to iterate
|
|
||||||
* @param {Function} fn The callback to invoke for each item
|
|
||||||
*/
|
|
||||||
module.exports = function forEach(obj, fn) {
|
|
||||||
// Don't bother if no value provided
|
|
||||||
if (obj === null || typeof obj === 'undefined') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var isArray = isArrayLike(obj);
|
|
||||||
|
|
||||||
// Force an array if not already something iterable
|
|
||||||
if (typeof obj !== 'object' && !isArray) {
|
|
||||||
obj = [obj];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate over array values
|
|
||||||
if (isArray) {
|
|
||||||
for (var i=0, l=obj.length; i<l; i++) {
|
|
||||||
fn.call(null, obj[i], i, obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Iterate over object keys
|
|
||||||
else {
|
|
||||||
for (var key in obj) {
|
|
||||||
if (obj.hasOwnProperty(key)) {
|
|
||||||
fn.call(null, obj[key], key, obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
var forEach = require('./forEach');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Accepts varargs expecting each argument to be an object, then
|
|
||||||
* immutably merges the properties of each object and returns result.
|
|
||||||
*
|
|
||||||
* When multiple objects contain the same key the later object in
|
|
||||||
* the arguments list will take precedence.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
*
|
|
||||||
* ```js
|
|
||||||
* var result = merge({foo: 123}, {foo: 456});
|
|
||||||
* console.log(result.foo); // outputs 456
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @param {Object} obj1 Object to merge
|
|
||||||
* @returns {Object} Result of all merge properties
|
|
||||||
*/
|
|
||||||
module.exports = function merge(obj1/*, obj2, obj3, ...*/) {
|
|
||||||
var result = {};
|
|
||||||
forEach(arguments, function (obj) {
|
|
||||||
forEach(obj, function (val, key) {
|
|
||||||
result[key] = val;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
+4
-8
@@ -1,10 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var forEach = require('./forEach');
|
var utils = require('./utils');
|
||||||
|
|
||||||
function trim(str) {
|
|
||||||
return str.replace(/^\s*/, '').replace(/\s*$/, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse headers into an object
|
* Parse headers into an object
|
||||||
@@ -24,10 +20,10 @@ module.exports = function parseHeaders(headers) {
|
|||||||
|
|
||||||
if (!headers) return parsed;
|
if (!headers) return parsed;
|
||||||
|
|
||||||
forEach(headers.split('\n'), function(line) {
|
utils.forEach(headers.split('\n'), function(line) {
|
||||||
i = line.indexOf(':');
|
i = line.indexOf(':');
|
||||||
key = trim(line.substr(0, i)).toLowerCase();
|
key = utils.trim(line.substr(0, i)).toLowerCase();
|
||||||
val = trim(line.substr(i + 1));
|
val = utils.trim(line.substr(i + 1));
|
||||||
|
|
||||||
if (key) {
|
if (key) {
|
||||||
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
|
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var forEach = require('./forEach');
|
var utils = require('./utils');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform the data for a request or a response
|
* Transform the data for a request or a response
|
||||||
@@ -11,7 +11,7 @@ var forEach = require('./forEach');
|
|||||||
* @returns {*} The resulting transformed data
|
* @returns {*} The resulting transformed data
|
||||||
*/
|
*/
|
||||||
module.exports = function transformData(data, headers, fns) {
|
module.exports = function transformData(data, headers, fns) {
|
||||||
forEach(fns, function (fn) {
|
utils.forEach(fns, function (fn) {
|
||||||
data = fn(data, headers);
|
data = fn(data, headers);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+143
@@ -0,0 +1,143 @@
|
|||||||
|
// utils is a library of generic helper functions non-specific to axios
|
||||||
|
|
||||||
|
var toString = Object.prototype.toString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is an Array
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is an Array, otherwise false
|
||||||
|
*/
|
||||||
|
function isArray(val) {
|
||||||
|
return toString.call(val) === '[object Array]';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is an Object
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is an Object, otherwise false
|
||||||
|
*/
|
||||||
|
function isObject(val) {
|
||||||
|
return val !== null && typeof val === 'object';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is a Date
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is a Date, otherwise false
|
||||||
|
*/
|
||||||
|
function isDate(val) {
|
||||||
|
return toString.call(val) === '[object Date]';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is a File
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is a File, otherwise false
|
||||||
|
*/
|
||||||
|
function isFile(val) {
|
||||||
|
return toString.call(val) === '[object File]';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a value is a Blob
|
||||||
|
*
|
||||||
|
* @param {Object} val The value to test
|
||||||
|
* @returns {boolean} True if value is a Blob, otherwise false
|
||||||
|
*/
|
||||||
|
function isBlob(val) {
|
||||||
|
return toString.call(val) !== '[object Blob]';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trim excess whitespace off the beginning and end of a string
|
||||||
|
*
|
||||||
|
* @param {String} str The String to trim
|
||||||
|
* @returns {String} The String freed of excess whitespace
|
||||||
|
*/
|
||||||
|
function trim(str) {
|
||||||
|
return str.replace(/^\s*/, '').replace(/\s*$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterate over an Array or an Object invoking a function for each item.
|
||||||
|
*
|
||||||
|
* If `obj` is an Array or arguments callback will be called passing
|
||||||
|
* the value, index, and complete array for each item.
|
||||||
|
*
|
||||||
|
* If 'obj' is an Object callback will be called passing
|
||||||
|
* the value, key, and complete object for each property.
|
||||||
|
*
|
||||||
|
* @param {Object|Array} obj The object to iterate
|
||||||
|
* @param {Function} fn The callback to invoke for each item
|
||||||
|
*/
|
||||||
|
function forEach(obj, fn) {
|
||||||
|
// Don't bother if no value provided
|
||||||
|
if (obj === null || typeof obj === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if obj is array-like
|
||||||
|
var isArray = obj.constructor === Array || typeof obj.callee === 'function';
|
||||||
|
|
||||||
|
// Force an array if not already something iterable
|
||||||
|
if (typeof obj !== 'object' && !isArray) {
|
||||||
|
obj = [obj];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate over array values
|
||||||
|
if (isArray) {
|
||||||
|
for (var i=0, l=obj.length; i<l; i++) {
|
||||||
|
fn.call(null, obj[i], i, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Iterate over object keys
|
||||||
|
else {
|
||||||
|
for (var key in obj) {
|
||||||
|
if (obj.hasOwnProperty(key)) {
|
||||||
|
fn.call(null, obj[key], key, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts varargs expecting each argument to be an object, then
|
||||||
|
* immutably merges the properties of each object and returns result.
|
||||||
|
*
|
||||||
|
* When multiple objects contain the same key the later object in
|
||||||
|
* the arguments list will take precedence.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* ```js
|
||||||
|
* var result = merge({foo: 123}, {foo: 456});
|
||||||
|
* console.log(result.foo); // outputs 456
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @param {Object} obj1 Object to merge
|
||||||
|
* @returns {Object} Result of all merge properties
|
||||||
|
*/
|
||||||
|
function merge(obj1/*, obj2, obj3, ...*/) {
|
||||||
|
var result = {};
|
||||||
|
forEach(arguments, function (obj) {
|
||||||
|
forEach(obj, function (val, key) {
|
||||||
|
result[key] = val;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
isArray: isArray,
|
||||||
|
isObject: isObject,
|
||||||
|
isDate: isDate,
|
||||||
|
isFile: isFile,
|
||||||
|
isBlob: isBlob,
|
||||||
|
forEach: forEach,
|
||||||
|
merge: merge,
|
||||||
|
trim: trim
|
||||||
|
};
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
var forEach = require('../../lib/forEach');
|
var forEach = require('../../../lib/utils').forEach;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
testArray: function (test) {
|
testArray: function (test) {
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
var utils = require('../../../lib/utils');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
testIsArray: function (test) {
|
||||||
|
test.equals(utils.isArray([]), true);
|
||||||
|
test.equals(utils.isArray({length: 5}), false);
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
|
||||||
|
testIsObject: function (test) {
|
||||||
|
test.equals(utils.isObject({}), true);
|
||||||
|
test.equals(utils.isObject(null), false);
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
|
||||||
|
testIsDate: function (test) {
|
||||||
|
test.equals(utils.isDate(new Date()), true);
|
||||||
|
test.equals(utils.isDate(Date.now()), false);
|
||||||
|
test.done();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
var merge = require('../../lib/merge');
|
var merge = require('../../../lib/utils').merge;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
testImmutability: function (test) {
|
testImmutability: function (test) {
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
var trim = require('../../../lib/utils').trim;
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
testTrim: function (test) {
|
||||||
|
test.equals(trim(' foo '), 'foo');
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
|
||||||
|
testTrimTab: function (test) {
|
||||||
|
test.equals(trim('\tfoo'), 'foo');
|
||||||
|
test.done();
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user