Socket
Socket
Sign inDemoInstall

isomorphic-unfetch

Package Overview
Dependencies
7
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    isomorphic-unfetch

Switches between unfetch & node-fetch for client & server.


Version published
Weekly downloads
2.9M
increased by0.86%
Maintainers
1
Install size
7.48 MB
Created
Weekly downloads
 

Package description

What is isomorphic-unfetch?

The isomorphic-unfetch npm package is a lightweight module that allows for making HTTP requests in both Node.js and browser environments. It's designed to provide a consistent API for fetch across these environments, making it easier to write isomorphic code that runs on both the server and the client.

What are isomorphic-unfetch's main functionalities?

Basic GET Request

This code sample demonstrates how to perform a basic GET request to retrieve data from an API and print it to the console. It uses the fetch API to make the request, parses the response as JSON, and handles any errors that may occur.

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

POST Request with JSON Body

This example shows how to send a POST request with a JSON body. It sets the method to POST, includes a Content-Type header to indicate the type of the request body, and uses JSON.stringify to convert a JavaScript object to a JSON string. The response is then parsed as JSON.

fetch('https://api.example.com/data', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    key: 'value'
  }),
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Other packages similar to isomorphic-unfetch

Readme

Source

Isomorphic Unfetch

Switches between unfetch & node-fetch for client & server.

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

Note: This module uses node-fetch 3.x, which is ES Module and requires Node >= 12.20.0.

$ npm i isomorphic-unfetch

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import fetch from "isomorphic-unfetch";

// using CommonJS modules
const fetch = require("isomorphic-unfetch");

Usage

As a ponyfill:

import fetch from "isomorphic-unfetch";

fetch("/foo.json")
  .then((r) => r.json())
  .then((data) => {
    console.log(data);
  });

Globally, as a polyfill:

import "isomorphic-unfetch";

// "fetch" is now installed globally if it wasn't already available

fetch("/foo.json")
  .then((r) => r.json())
  .then((data) => {
    console.log(data);
  });

License

MIT License © Jason Miller

FAQs

Last updated on 03 Jan 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc