Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking up complexity of main axios file
- Loading branch information
Matt Zabriskie
committed
Aug 27, 2014
1 parent
0d0b837
commit 9096d34
Showing
12 changed files
with
1,233 additions
and
565 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
var merge = require('./merge'); | ||
|
||
var toString = Object.prototype.toString; | ||
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) { | ||
return data !== null && typeof data === 'object' && | ||
toString.call(data) !== '[object File]' && | ||
toString.call(data) !== '[object Blob]' ? | ||
JSON.stringify(data) : null; | ||
}], | ||
|
||
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: merge(CONTENT_TYPE_APPLICATION_JSON), | ||
post: merge(CONTENT_TYPE_APPLICATION_JSON), | ||
put: merge(CONTENT_TYPE_APPLICATION_JSON) | ||
}, | ||
|
||
xsrfCookiName: 'XSRF-TOKEN', | ||
xsrfHeaderName: 'X-XSRF-TOKEN' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
/** | ||
* 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) { | ||
if (typeof obj !== 'object') { | ||
return; | ||
} | ||
|
||
// Iterate over array values | ||
if (obj.constructor === Array || typeof obj.callee === 'function') { | ||
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); | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
var forEach = require('./forEach'); | ||
|
||
/** | ||
* Transform the data for a request or a response | ||
* | ||
* @param {Object|String} data The data to be transformed | ||
* @param {Array} headers The headers for the request or response | ||
* @param {Array|Function} fns A single function or Array of functions | ||
* @returns {*} The resulting transformed data | ||
*/ | ||
module.exports = function transformData(data, headers, fns) { | ||
if (typeof fns === 'function') { | ||
return fns(data, headers); | ||
} | ||
|
||
forEach(fns, function (fn) { | ||
data = fn(data, headers); | ||
}); | ||
|
||
return data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var forEach = require('../../lib/forEach'); | ||
|
||
module.exports = { | ||
testArray: function (test) { | ||
var sum = 0; | ||
|
||
forEach([1, 2, 3, 4, 5], function (val) { | ||
sum += val; | ||
}); | ||
|
||
test.equal(sum, 15); | ||
test.done(); | ||
}, | ||
|
||
testObject: function (test) { | ||
var keys = ''; | ||
var vals = 0; | ||
var obj = { | ||
b: 1, | ||
a: 2, | ||
r: 3 | ||
}; | ||
|
||
forEach(obj, function (v, k) { | ||
keys += k; | ||
vals += v; | ||
}); | ||
|
||
test.equal(keys, 'bar'); | ||
test.equal(vals, 6); | ||
test.done(); | ||
} | ||
}; |
Oops, something went wrong.