Giter Site home page Giter Site logo

native-fetch's Introduction

native-fetch

Returns native fetch/Request/Headers if available or the undici module if not

An (almost) drop-in replacement for the undici module that returns the native fetch if available or the polyfill if not.

Why?

Some environments such as the Electron Renderer process straddle the node/browser divide with features from both APIs available. In these cases the webpack approach of always using the browser field in your package.json to override requires is too heavy-handed as sometimes you want to use the node version of an API.

Instead we can check for the availability of a given API and return it, rather than the node-polyfill for that API.

Why Undici and not node-fetch?

node-fetch is the OG fetch implementation for node, but it uses Node.js streams instead of WHATWG streams. This means the APIs are not the same which leads to all sorts of weird shenanigans with types.

undici is the new kid on the block and uses WHATWG streams so all of the APIs now live in glorious harmony.

If you are trying to write polymorphic code with strong typing this is a big deal.

Install

You must install a version of undici alongside this module to be used if a native implementation is not available.

$ npm install --save native-fetch undici

Usage

const { fetch } = require('native-fetch')

fetch('https://github.com/')
    .then(res => res.text())
    .then(body => console.log(body))

native-fetch's People

Contributors

achingbrain avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

native-fetch's Issues

dynamic import is empty

When importing native-fetch, it returns an empty module:

const fetch = await import('native-fetch')

console.log(fetch)
[Module: null prototype] {
  Headers: undefined,
  Request: undefined,
  Response: undefined,
  fetch: undefined
}

importing uncidi dynamically works properly, so the problem is with index-node.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.