wonderful-fetch
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -23,3 +23,3 @@ (function (root, factory) { | ||
var SOURCE = 'library'; | ||
var VERSION = '0.0.2'; | ||
var VERSION = '0.0.3'; | ||
@@ -106,45 +106,47 @@ function WonderfulFetch(url, options) { | ||
res.text() | ||
.then(function (text) { | ||
if (res.ok) { | ||
if (options.raw) { | ||
return _resolve(res); | ||
} else if (options.json) { | ||
JSON5 = JSON5 || require('json5'); | ||
try { | ||
return _resolve(JSON5.parse(text)); | ||
} catch (e) { | ||
throw new Error(new Error('Response is not JSON: ' + e)) | ||
if (options.ok && options.download) { | ||
jetpack = jetpack || require('fs-jetpack'); | ||
if (!jetpack.exists(options.download)) { | ||
path = path || require('path'); | ||
var name = path.parse(options.download).name; | ||
var ext = path.parse(options.download).ext; | ||
var dir = options.download.replace(name + ext, ''); | ||
jetpack.dir(dir) | ||
} | ||
fileStream = jetpack.createWriteStream(options.download); | ||
res.body.pipe(fileStream); | ||
res.body.on('error', function (e) { | ||
throw new Error(new Error('Failed to download: ' + e)) | ||
}); | ||
fileStream.on('finish', function() { | ||
return _resolve({ | ||
path: options.download | ||
}); | ||
}); | ||
} else { | ||
res.text() | ||
.then(function (text) { | ||
if (res.ok) { | ||
if (options.raw) { | ||
return _resolve(res); | ||
} else if (options.json) { | ||
JSON5 = JSON5 || require('json5'); | ||
try { | ||
return _resolve(JSON5.parse(text)); | ||
} catch (e) { | ||
throw new Error(new Error('Response is not JSON: ' + e)) | ||
} | ||
} else { | ||
return _resolve(text); | ||
} | ||
} else if (options.download) { | ||
jetpack = jetpack || require('fs-jetpack'); | ||
if (!jetpack.exists(options.download)) { | ||
path = path || require('path'); | ||
var name = path.parse(options.download).name; | ||
var ext = path.parse(options.download).ext; | ||
var dir = options.download.replace(name + ext, ''); | ||
jetpack.dir(dir) | ||
} | ||
fileStream = jetpack.createWriteStream(options.download); | ||
res.body.pipe(fileStream); | ||
res.body.on('error', function (e) { | ||
throw new Error(new Error('Failed to download: ' + e)) | ||
}); | ||
fileStream.on('finish', function() { | ||
return _resolve({ | ||
path: options.download | ||
}); | ||
}); | ||
} else { | ||
return _resolve(text); | ||
var error = new Error(text || res.statusText || 'Unknown error'); | ||
Object.assign(error, { status: res.status }) | ||
throw error; | ||
} | ||
} else { | ||
var error = new Error(text || res.statusText || 'Unknown error'); | ||
Object.assign(error, { status: res.status }) | ||
throw error; | ||
} | ||
}) | ||
.catch(e => { | ||
return _reject(e); | ||
}) | ||
}) | ||
.catch(e => { | ||
return _reject(e); | ||
}) | ||
} | ||
}) | ||
@@ -151,0 +153,0 @@ .catch(function (e) { |
{ | ||
"name": "wonderful-fetch", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"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
13451
149