Socket
Socket
Sign inDemoInstall

unfetch

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unfetch

Bare minimum fetch polyfill in 500 bytes


Version published
Weekly downloads
4.8M
decreased by-0.05%
Maintainers
1
Weekly downloads
 
Created

What is unfetch?

The 'unfetch' npm package is a lightweight polyfill for the Fetch API, which allows you to make HTTP requests in environments where the Fetch API is not natively available, such as older browsers or certain JavaScript environments. It is designed to be minimal and efficient, providing the essential functionality of the Fetch API without any additional overhead.

What are unfetch's main functionalities?

Basic Fetch Request

This feature demonstrates how to make a basic HTTP GET request using 'unfetch'. The response is then converted to JSON and logged to the console.

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

POST Request

This feature demonstrates how to make an HTTP POST request using 'unfetch'. The request includes headers and a JSON body, and the response is processed similarly to the GET request.

const fetch = require('unfetch');
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));

Handling Errors

This feature demonstrates how to handle errors in a fetch request using 'unfetch'. It checks if the response is not ok and throws an error, which is then caught and logged.

const fetch = require('unfetch');
fetch('https://api.example.com/data')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return response.json();
  })
  .then(data => console.log(data))
  .catch(error => console.error('Fetch error:', error));

Other packages similar to unfetch

Keywords

FAQs

Package last updated on 31 Dec 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc