For libraries, I found a rather nice pattern to allow users to only install node-fetch if they need it:
// package.json{"peerDependencies":{"node-fetch":"*"},"peerDependenciesMeta":{"node-fetch":{"optional":true}}}// Run before you use fetch:if (!globalThis.fetch){Object.assign(globalThis,{fetch:(...args:any[])=>{try{constfetch=require("node-fetch");Object.assign(globalThis,{fetch});returnfetch(...args);}catch (e){console.warn('"\x1b[33m⚠️ package missing to run fetch on the server.\n Please run:\x1b[0m\n\nnpm i node-fetch\n"');Object.assign(globalThis,{fetch:()=>Promise.reject()});returnPromise.reject();}}});}
This way, users get a helpful message if they forget to install node-fetch and you can avoid superfluous dependencies.
Hi 👋
This is Suchintan a Full Stack Developer with MERN stack.
Want to know more about me?
Connect me on - https://www.linkedin.com/in/suchintan-das-b698bb1b8/
One suggestion 💭. Can you explain the changes that are being introduced on the new version of fetch api compared to the previous version ✍.
I think it would make the blog a full packed information about the topic and would also increase the understanding of people who can't have time to look on the documentation to know about the information ℹ.
Hi 👋
This is Suchintan a Full Stack Developer with MERN stack.
Want to know more about me?
Connect me on - https://www.linkedin.com/in/suchintan-das-b698bb1b8/
For libraries, I found a rather nice pattern to allow users to only install node-fetch if they need it:
This way, users get a helpful message if they forget to install node-fetch and you can avoid superfluous dependencies.
Thanks Andrew 👨, for sharing this information.
Can't wait to try it out on the system 👍.
One suggestion 💭. Can you explain the changes that are being introduced on the new version of fetch api compared to the previous version ✍.
I think it would make the blog a full packed information about the topic and would also increase the understanding of people who can't have time to look on the documentation to know about the information ℹ.
Hmm the official documentation is probably the best place to learn about it developer.mozilla.org/en-US/docs/W....
I believe that the NodeJS implemented of the fetch API is the same as the one we already use in the browser.
Thanks Andrew 👍,
Let me go through the documentation to know what amazing stuffs we have on this new version of
Node JS🤠Glad I came across this.
Clean and Concise.👍👍👍
Thanks much appreciated.
Nice🔥, Now we don't have to depend on any other library for fetching data. Btw great article , Andrew.
Very detailed, thanks.
Well described post
Wawu that was timely. You have saved me a bunch of time. Thank you!
Sure no problem.
Great article but it would have been nice if you also showed POST requests, not entirely simple GET
Hello. cool stuff & article
but how manage headers with this api ?