wonderful-fetch
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -23,3 +23,3 @@ (function (root, factory) { | ||
var SOURCE = 'library'; | ||
var VERSION = '0.0.6'; | ||
var VERSION = '0.0.7'; | ||
@@ -35,2 +35,14 @@ function WonderfulFetch(url, options) { | ||
options.cacheBreaker = typeof options.cacheBreaker === 'undefined' ? true : options.cacheBreaker; | ||
options.contentType = typeof options.contentType === 'undefined' ? '' : options.contentType; | ||
options.responseFormat = typeof options.responseFormat === 'undefined' ? 'raw' : options.responseFormat; | ||
// Legacy | ||
if (options.raw) { | ||
options.responseFormat = 'raw' | ||
} else if (options.json) { | ||
options.responseFormat = 'json' | ||
} else if (options.text) { | ||
options.responseFormat = 'text' | ||
} | ||
url = url || options.url; | ||
@@ -42,2 +54,5 @@ | ||
var bodyIsFormData = typeof options.body === 'function' && options.body.append; | ||
var bodyIsObject = typeof options.body === 'object'; | ||
if (!url) { | ||
@@ -54,8 +69,29 @@ return reject(new Error('No URL provided.')) | ||
if (options.body) { | ||
config.body = typeof options.body !== 'string' ? JSON.stringify(options.body) : options.body; | ||
if (bodyIsFormData) { | ||
config.body = options.body; | ||
} else if (bodyIsObject) { | ||
config.body = JSON.stringify(options.body); | ||
} else { | ||
config.body = options.body; | ||
} | ||
} | ||
if (options.json && options.body && config.method === 'post') { | ||
// if (options.json && options.body && config.method === 'post') { | ||
// config.headers['Content-Type'] = 'application/json'; | ||
// } | ||
if ( | ||
(bodyIsObject && !bodyIsFormData) | ||
|| (options.contentType === 'json') | ||
) { | ||
config.headers['Content-Type'] = 'application/json'; | ||
} | ||
if (config.method === 'get') { | ||
delete config.body; | ||
} | ||
if (options.log) { | ||
console.log('Fetch configuration:', 'bodyIsFormData=' + bodyIsFormData, 'bodyIsObject=' + bodyIsObject, options, config); | ||
} | ||
var timeoutHolder; | ||
@@ -76,3 +112,3 @@ | ||
if (options.log) { | ||
console.log('Fetch (' + tries + '/' + options.tries + ', ' + ms + 'ms): ' + url, options); | ||
console.log('Fetch (' + tries + '/' + options.tries + ', ' + ms + 'ms): ', url); | ||
} | ||
@@ -131,3 +167,3 @@ | ||
if (res.ok) { | ||
if (options.raw) { | ||
if (options.responseFormat === 'raw') { | ||
return _resolve(res); | ||
@@ -137,3 +173,3 @@ } else { | ||
.then(function (text) { | ||
if (options.json) { | ||
if (options.responseFormat === 'json') { | ||
JSON5 = JSON5 || require('json5'); | ||
@@ -149,2 +185,5 @@ try { | ||
}) | ||
.catch(e => { | ||
return _reject(e); | ||
}) | ||
} | ||
@@ -151,0 +190,0 @@ } else { |
{ | ||
"name": "wonderful-fetch", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A wrapper around fetch.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14948
187