New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.0.4 to 1.0.5

47

fetchJSON.js

@@ -20,28 +20,31 @@ /**@function fetchJSON

return function(path, functor){
if( (typeof functor == typeof (x=>x)) && (typeof path == typeof "42xyz") ){
const f = fetch(path);
return new Promise((resolve, reject)=>{
if( (typeof functor == typeof (x=>x)) && (typeof path == typeof "42xyz") ){
const f = fetch(path);
f.then((response)=>{
var contentType= response.headers.get("content-type");
f.then((response)=>{
var contentType= response.headers.get("content-type");
if(contentType && contentType.includes("application/json"))
return response.json().then( jsonData=>functor(jsonData) );
else{
//console.error("fetchJSON.js : Something went wrong during data inspection (data is not JSON or file is unreachable)");
throw new Error("Something went wrong during data inspection (data is not JSON or couldn't reach file)");
return null;
}
});
if(contentType && contentType.includes("application/json"))
return response.json().then( jsonData=>{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;
}
});
return f;
}
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 functor != typeof (x=>x))
throw new TypeError("The 2nd argument must be a function");
return null;
}
return f;
}
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");
reject("The 1st argument must be a string");
if(typeof functor != typeof (x=>x))
reject("The 2nd argument must be a function");
return null;
}
});
}
});
{
"name": "fetch_json",
"version": "1.0.4",
"version": "1.0.5",
"description": "A javasript library that makes it easy to retrieve JSON data from a JSON file",

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

@@ -20,3 +20,3 @@ # fetchJSON #

Therefore, here are two ways to install this library:
***Without NPM***
```html

@@ -34,4 +34,3 @@ <html>

----------
***With NPM***
```

@@ -60,3 +59,3 @@ npm install fetch_json

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

@@ -66,3 +65,3 @@ });

/*handle error*/
})
});

@@ -80,3 +79,48 @@ ```

## Error Handling ##
One of the most important part of retrieving data asynchronously is error handling.
Sometimes you have server issues, sometimes you used an incorrect path and this can be a pain in the ass with regular fetch.
Version 1.0.5 (NPM, it's 1.05 on github) brings a whole new layer of abstracted boilerplate for error handling. Here's a simple example with, and without, fetchJSON.
```javascript
//with fetchJSON
let config_data;
fetchJSON("../../someFolder/someFile.json", data=>config_data=data)
.then(/*some manipulations*/)
.then(/*some manipulations*/)
.catch(errorMsg=>{
/*handle errors here*/
});
//without fetchJSON
let config_data;
new Promise((resolve, reject)=>{
fetch("../../someFolder/someFile.json").then(response=>{
/*gather headers*/
if(/*there's json in there*/)
return response.json().then(data=>{
/*some manipulation*/
config_data = data;
//finally !
resolve(data);//important
});
else
reject(/*handle error*/)
});
})
.then(/*some manipulations*/)
.then(/*some manipulations*/)
.catch(errorMsg=>{
/*handle errors here*/
});
```
From version 1.0.5, fetchJSON is completely thenable and catchable just like any other good Promise-based library \o/ !
## Motivations ##
As you can see, fetchJSON really focuses on what's important : using the data. Whereas the regular fetch approach is mostly boilerplate and takes up a lot of space in your code, and most of that space is here solely to get the data not using it.
JSON is a precious resource, it would be a shame to spend more time on getting its data than using it.
## Questions/Suggestions ##
Please fill free to ask for help / post suggestions on my github repository, I'll be more than glad to take care of your problems/concerns.
Please fill free to ask for help or post suggestions on my github repository, I'll be more than glad to take care of your problems/concerns.
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