Comparing version 2.0.0 to 2.1.0
/**@function fetchJSON | ||
*use the Fetch API to retrieve data from a JSON file | ||
*@param {string} path - the complete path to the file | ||
*@param {object} data - an object of data to be converted into a query string | ||
*@param {object|undefined} data - an object of data to be converted into a query string | ||
*@param {object|undefined} options - the options for fetch | ||
* | ||
@@ -10,3 +11,3 @@ *@return the Promise object of the fetch request | ||
(function UniversalModuleDefinition(root, factory){ | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
if(typeof module === 'object') | ||
module.exports = factory(); | ||
@@ -19,5 +20,6 @@ else if(typeof define === 'function' && define.amd) | ||
root["fetchJSON"] = factory(); | ||
})(this, function(){ | ||
return function(path, data){ | ||
data = data || {}; | ||
})(window || this, function(){ | ||
var fetchJSON = function(path, data, options){ | ||
data = Object.assign({}, fetchJSON.defaults.qs, data || {}); | ||
options = options || {}; | ||
@@ -66,6 +68,8 @@ if(typeof data != "object" || data === null) | ||
return new Promise((resolve, reject)=>{ | ||
return new Promise(function(resolve, reject){ | ||
if(typeof path == "string"){ | ||
// console.log("url: ", path+qstring); | ||
var f = fetch(path + qstring); | ||
var fetchOptions = Object.assign({}, fetchJSON.defaults.options, options, {method: "GET"}); | ||
fetchOptions.headers = Object.assign({}, fetchJSON.defaults.headers, fetchOptions.headers || {}); | ||
var f = fetch(path + qstring, fetchOptions); | ||
@@ -102,3 +106,13 @@ f.then(function(response){ | ||
}); | ||
} | ||
}; | ||
fetchJSON.defaults = { | ||
qs: {}, | ||
options: {}, | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}; | ||
return fetchJSON; | ||
}); |
{ | ||
"name": "fetch_json", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "A javasript library that makes it easy to retrieve JSON data from a JSON file", | ||
@@ -5,0 +5,0 @@ "main": "fetchJSON.js", |
@@ -203,1 +203,13 @@ # fetchJSON # | ||
## Changes | ||
### v2.1.0 | ||
In an effort to provide more customization, `fetchJSON` now exposes a third argument : `options`. This is an object following the same interface as `fetch`'s `init` [argument](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters), with the only enforcement being that headers come as an object and not an object or an array. | ||
It also exposes `fetchJSON.defaults` with three properties : `qs`, `options` and `headers` : | ||
* `qs` is merged w/ the `data` argument of `fetchJSON` | ||
* `headers` is merged w/ the `headers` property of the `options` argument of `fetchJSON` | ||
* `options` is merged w/ the `options` argument of `fetchJSON` |
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
11912
98
214