Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fetch_json

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch_json - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

91

fetchJSON.js
/**@function fetchJSON
*use the Fetch API to retrieve data from a JSON file
*@param {string} path - the complete path to the file
*@param {function} functor - the function to which the JSON data will be passed
*@param {object} data - an object of data to be converted into a query string
*

@@ -19,21 +19,72 @@ *@return the Promise object of the fetch request

})(this, function(){
return function(path, functor){
return function(path, data){
data = data || {};
if(typeof data != "object" || data === null)
throw new TypeError("'data' must be an Object");
var check = function(elem){
var innerCheck = function(elem){
if(!(typeof elem == "string" || typeof elem == "number"))
throw new TypeError("Invalid data object (elements are not all arrays of strings/numbers or strings or numbers)");
}
if(elem instanceof Array){
elem.forEach(innerCheck);
return;
}
innerCheck(elem);
};
Object.values(data).forEach(check);
var primitiveToQstring = function(e){
return encodeURIComponent(e.key) + "=" + encodeURIComponent(e.value);
}
var qstring = Object.entries(data)
.map(function(e){ return {key: e[0], value: e[1]}; })
.map(function(e){
if(e.value instanceof Array){
return e.value.map(function(val){
return {key: e.key, value: val};
}).map(primitiveToQstring).join("&");
}
return primitiveToQstring(e);
}).join("&");
if(qstring !== ""){
if(!/\?/.test(path))//has a '?'
qstring = "?"+qstring;
else if(!/\?$/.test(path))//doesn't end by '?'
qstring = /&$/.test(path) ? qstring : "&"+qstring;
}
return new Promise((resolve, reject)=>{
if(typeof path == typeof "42xyz"){
const f = fetch(path);
if(typeof path == "string"){
// console.log("url: ", path+qstring);
var f = fetch(path + qstring);
f.then((response)=>{
var contentType= response.headers.get("content-type");
if(contentType && contentType.includes("application/json"))
return response.json().then(jsonData=>{
if(typeof functor == "function")
functor(jsonData);
resolve(jsonData);
});
else{
//throw new Error("Something went wrong during data inspection (data is not JSON or couldn't reach file)");
reject("Something went wrong during data inspection (data is not JSON or couldn't reach file)");
return null;
}
f.then(function(response){
// var contentType= response.headers.get("content-type");
//
// if(contentType && contentType.includes("application/json"))
// return response.json().then(jsonData=>{
// if(typeof functor == "function")
// functor(jsonData);
// resolve(jsonData);
// });
// else{
// reject("Something went wrong during data inspection (data is not JSON or couldn't reach file)");
// return null;
// }
return response.json()
.then(resolve)
.catch(function(){
var error = "Something went wrong during data inspection (data is not JSON or couldn't reach file)";
reject(error);
return Promise.reject(error);
});
});

@@ -44,5 +95,3 @@

else{
//console.error("fetchJSON.js : The first argument must be a string, the second argument must be a function");
if(typeof path != typeof "42xyz")
//throw new TypeError("The 1st argument must be a string");
if(typeof path != "string")
reject("The 1st argument must be a string");

@@ -49,0 +98,0 @@ return null;

{
"name": "fetch_json",
"version": "1.1.0",
"version": "2.0.0",
"description": "A javasript library that makes it easy to retrieve JSON data from a JSON file",

@@ -5,0 +5,0 @@ "main": "fetchJSON.js",

@@ -55,3 +55,3 @@ # fetchJSON #

let config_data;
fetchJSON("../../someFolder/someFile.json", data=>config_data=data);
fetchJSON("../../someFolder/someFile.json").then(data=>config_data=data);

@@ -76,6 +76,4 @@ //Without fetchJSON

```javascript
let config_data;
fetchJSON("../../someFolder/someFile.json", data=>config_data=data)
.then(()=>{
/* use config_data to configure your application*/
fetchJSON("../../someFolder/someFile.json").then(config_data=>{
/* use data to configure your application*/
});

@@ -92,4 +90,3 @@ ```

//with fetchJSON
let config_data;
fetchJSON("../../someFolder/someFile.json", data=>config_data=data)
fetchJSON("../../someFolder/someFile.json")
.then(/*some manipulations*/)

@@ -102,3 +99,2 @@ .then(/*some manipulations*/)

//without fetchJSON
let config_data;
new Promise((resolve, reject)=>{

@@ -110,3 +106,2 @@ fetch("../../someFolder/someFile.json").then(response=>{

/*some manipulation*/
config_data = data;
//finally !

@@ -152,1 +147,62 @@ resolve(data);//important

## Complete 180°
Since v2.0.0 the second argument, which was a callback function (same behavior as a simple `then`), has been changed to an object of data :
This object of data must be:
* Not provided (defaulted to `{}`)
* an empty object
* an object of numbers and/or strings
* an object of numbers and/or strings and/or arrays (that only contains numbers and/or strings)
This allows you to construct the query string easily :
```javascript
fetchJSON("/api/user", {
id: 42,
item: "player_profile"
props: [
"rank",
"ratio"
]
}); //Will conduct a GET request to /api/user?id=42&item=player_profile&props=rank&props=ratio
///OR
fetchJSON("/api/user?", {
id: 42,
item: "player_profile"
props: [
"rank",
"ratio"
]
}); //Will conduct a GET request to /api/user?id=42&item=player_profile&props=rank&props=ratio
///OR
fetchJSON("/api/user?test=1", {
id: 42,
item: "player_profile"
props: [
"rank",
"ratio"
]
}); //Will conduct a GET request to /api/user?test=1&id=42&item=player_profile&props=rank&props=ratio
///OR
fetchJSON("/api/user?test=1&", {
id: 42,
item: "player_profile"
props: [
"rank",
"ratio"
]
}); //Will conduct a GET request to /api/user?test=1&id=42&item=player_profile&props=rank&props=ratio
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc