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

fetch-ponyfill

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-ponyfill

A ponyfill (doesn't overwrite the native fetch) for the Fetch specification https://fetch.spec.whatwg.org.

  • 7.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
177K
decreased by-22.15%
Maintainers
1
Weekly downloads
 
Created

What is fetch-ponyfill?

The fetch-ponyfill npm package provides a ponyfill for the Fetch API, which means it offers a way to use the Fetch API in environments where it is not natively available, such as older browsers or Node.js. Unlike a polyfill, a ponyfill does not modify the global environment but instead provides the functionality as a module.

What are fetch-ponyfill's main functionalities?

Basic Fetch Request

This code demonstrates how to perform a basic fetch request using fetch-ponyfill. It fetches data from a placeholder API and logs the response.

const fetch = require('fetch-ponyfill')().fetch;

fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Custom Headers

This code demonstrates how to make a POST request with custom headers using fetch-ponyfill. It sends a JSON payload to the server and logs the response.

const fetch = require('fetch-ponyfill')().fetch;

fetch('https://jsonplaceholder.typicode.com/posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer token'
  },
  body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 })
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Handling Errors

This code demonstrates how to handle errors in fetch requests using fetch-ponyfill. It checks if the response is not ok and throws an error if the fetch fails.

const fetch = require('fetch-ponyfill')().fetch;

fetch('https://jsonplaceholder.typicode.com/invalid-url')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok ' + response.statusText);
    }
    return response.json();
  })
  .then(data => console.log(data))
  .catch(error => console.error('Fetch error:', error));

Other packages similar to fetch-ponyfill

Keywords

FAQs

Package last updated on 19 Jan 2021

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