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.
Install the unified-fetch package from npm.
npm install unified-fetch
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();
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 |
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 |