wonderful-fetch
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -23,3 +23,3 @@ (function (root, factory) { | ||
var SOURCE = 'library'; | ||
var VERSION = '1.0.1'; | ||
var VERSION = '1.1.0'; | ||
@@ -30,3 +30,5 @@ function WonderfulFetch(url, options) { | ||
// Fix options | ||
options = options || {}; | ||
url = url || options.url; | ||
options.timeout = options.timeout || 60000; | ||
@@ -38,14 +40,13 @@ options.tries = typeof options.tries === 'undefined' ? 1 : options.tries; | ||
options.response = (typeof options.response === 'undefined' ? 'raw' : options.response).toLowerCase(); | ||
options.output = typeof options.output === 'undefined' ? 'body' : options.output; | ||
// Legacy | ||
if (options.raw) { | ||
options.response = 'raw' | ||
options.response = 'raw'; | ||
} else if (options.json) { | ||
options.response = 'json' | ||
options.response = 'json'; | ||
} else if (options.text) { | ||
options.response = 'text' | ||
options.response = 'text'; | ||
} | ||
url = url || options.url; | ||
var tries = 1; | ||
@@ -113,11 +114,50 @@ var maxTries = options.tries - 1; | ||
function _resolve(r) { | ||
function _output(res, r) { | ||
var headers = {}; | ||
var isError = r instanceof Error; | ||
// Iterate over headers and add them to the object | ||
if (res && res.headers) { | ||
res.headers.forEach(function (value, key) { | ||
// Parse JSON headers | ||
try { | ||
headers[key] = JSON.parse(value); | ||
} catch (e) { | ||
headers[key] = value; | ||
} | ||
// Add bm-properties to error object | ||
if (key === 'bm-properties' && isError) { | ||
try { | ||
Object.keys(headers[key]).forEach(function (k) { | ||
r[k] = headers[key][k]; | ||
}) | ||
} catch (e) { | ||
console.warn('Failed to add bm-properties to error object', e); | ||
} | ||
} | ||
}); | ||
} | ||
// Format output | ||
if (isError || options.output === 'body') { | ||
return r; | ||
} else { | ||
return { | ||
status: res.status, | ||
headers: headers, | ||
body: r, | ||
} | ||
} | ||
} | ||
function _resolve(res, r) { | ||
clearTimeout(timeoutHolder); | ||
return resolve(r); | ||
return resolve(_output(res, r)); | ||
} | ||
function _reject(e) { | ||
function _reject(res, e) { | ||
clearTimeout(timeoutHolder); | ||
if (tries > maxTries && !infinite) { | ||
return reject(e); | ||
return reject(_output(res, e)); | ||
} else { | ||
@@ -131,3 +171,3 @@ return _fetch(tries++); | ||
timeoutHolder = setTimeout(function () { | ||
return _reject(new Error('Request timed out')) | ||
return _reject(undefined, new Error('Request timed out')) | ||
}, options.timeout); | ||
@@ -160,3 +200,3 @@ } | ||
fileStream.on('finish', function() { | ||
return _resolve({ | ||
return _resolve(res, { | ||
path: options.download | ||
@@ -168,3 +208,3 @@ }); | ||
if (options.response === 'raw') { | ||
return _resolve(res); | ||
return _resolve(res, res); | ||
} else { | ||
@@ -182,3 +222,3 @@ res.text() | ||
try { | ||
return _resolve(JSONParser.parse(text)); | ||
return _resolve(res, JSONParser.parse(text)); | ||
} catch (e) { | ||
@@ -188,7 +228,7 @@ throw new Error(new Error('Response is not JSON: ' + e)) | ||
} else { | ||
return _resolve(text); | ||
return _resolve(res, text); | ||
} | ||
}) | ||
.catch(e => { | ||
return _reject(e); | ||
.catch(function (e) { | ||
return _reject(res, e); | ||
}) | ||
@@ -203,4 +243,4 @@ } | ||
}) | ||
.catch(e => { | ||
return _reject(e); | ||
.catch(function (e) { | ||
return _reject(res, e); | ||
}) | ||
@@ -211,3 +251,3 @@ } | ||
.catch(function (e) { | ||
return _reject(e) | ||
return _reject(res, e) | ||
}) | ||
@@ -214,0 +254,0 @@ }, ms); |
{ | ||
"name": "wonderful-fetch", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A wrapper around fetch.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
16565
227