Switches between unfetch & node-fetch for client & server.
npx @tessl/cli install tessl/npm-isomorphic-unfetch@4.0.0Isomorphic Unfetch is a universal fetch implementation that automatically switches between unfetch for browser environments and node-fetch for Node.js environments. It provides a single import that works across all JavaScript runtime environments.
npm install isomorphic-unfetchimport fetch from "isomorphic-unfetch";For CommonJS:
const fetch = require("isomorphic-unfetch");Browser-specific import:
import fetch from "isomorphic-unfetch/browser";import fetch from "isomorphic-unfetch";
// Works in both browser and Node.js
const response = await fetch("https://api.example.com/data");
const data = await response.json();
console.log(data);
// POST request
await fetch("/api/users", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ name: "John" })
});Isomorphic Unfetch uses environment detection to automatically load the appropriate fetch implementation:
unfetch packagenode-fetch package//example.com) to https://example.comglobal.fetch if not already presentThe main export provides a fetch function that automatically detects the runtime environment and uses the appropriate implementation.
declare function fetch(url: string | URL, opts?: RequestInit): Promise<Response>;Available via the /browser subpath, this provides a browser-only implementation that uses unfetch or falls back to the native browser fetch.
declare function fetch(url: string | URL, opts?: RequestInit): Promise<Response>;declare namespace unfetch {
/** Union type for Headers that works across environments */
export type IsomorphicHeaders = Headers | NodeHeaders;
/** Union type for Body that works across environments */
export type IsomorphicBody = Body | NodeBody;
/** Union type for Response that works across environments */
export type IsomorphicResponse = Response | NodeResponse;
/** Union type for Request that works across environments */
export type IsomorphicRequest = Request | NodeRequest;
/** Union type for RequestInit that works across environments */
export type IsomorphicRequestInit = RequestInit | NodeRequestInit;
}
declare const unfetch: typeof fetch;unfetch package/browser export directly imports unfetch and installs it as self.fetch if not presentnode-fetch package//example.com) to HTTPS (https://example.com)global.fetch if not already presentself.fetch if not already presentDependencies are loaded dynamically based on the runtime environment.