Skip to content

rmuchall/unified-fetch

Repository files navigation

GitHub npm bundle size npm

What is unified-fetch?

unified-fetch is an isomorphic fetch library. It's a wrapper around two libraries. node-fetch for NodeJs and whatwg-fetch for the browser. Depending on the context on which it's executed the correct library is automatically loaded. If you're using a modern browser then unified-fetch uses the built-in fetch implementation and neither library is used.

Installation

Install the unified-fetch package from npm.
npm install unified-fetch

Usage

Use unified-fetch in the same way you use built-in fetch.

const response = await unifiedFetch.fetch("http://localhost/basic", {method: HttpMethod.GET});
const result = await response.json();

Instance Options

The following instance initialization options are available.

const unifiedFetch: UnifiedFetch = new UnifiedFetch({
    // ... instance options
});
Option Description
prefixUrl Prefix all requests with the specified URL
jwtToken Include the specified JWT token (using appropriate auth header)
beforeRequestHook Hook into the request before it's been made
afterRequestHook Hook into the request after it's been made
headers Apply additional headers

Request Options

The following request options are available.

const result = await unifiedFetch.fetch("http://localhost/my-request", {
    // ... request options
});
Option Description
json Add the specified object to the request as a JSON message body
queryString Add the specified object to the request as a URL encoded query string