How to use the isomorphic-unfetch.bind function in isomorphic-unfetch

To help you get started, we’ve selected a few isomorphic-unfetch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github iotexproject / iotex-explorer / src / shared / common / apollo-client.ts View on Github external
// @ts-ignore
import JsonGlobal from "safe-json-globals/get";

const state = isBrowser && JsonGlobal("state");
const apolloState = isBrowser && state.apolloState;
const apolloAnalyticsState = isBrowser && state.apolloAnalyticsState;
const apiGatewayUrl = isBrowser && state.base.apiGatewayUrl;
const webBpApiGatewayUrl = isBrowser && state.base.webBpApiGatewayUrl;
const analyticsApiGatewayUrl = isBrowser && state.base.analyticsApiGatewayUrl;
const csrfToken = isBrowser && state.base.csrfToken;

const apolloClientConfig = {
  uri: apiGatewayUrl
};

const fetch = unfetch.bind(window);

const MAX_CONCURRENT_REQUEST = 5;

let availableCap = MAX_CONCURRENT_REQUEST;
setInterval(() => (availableCap = MAX_CONCURRENT_REQUEST), 1000); // Rate cap to 5 requests per second

const limitRateFetch = async (
  req: RequestInfo,
  opt?: RequestInit
): Promise => {
  if (availableCap > 0) {
    availableCap--;
    return fetch(req, opt);
  }
  return new Promise(resolve =>
    setTimeout(() => resolve(limitRateFetch(req, opt)), 100)

isomorphic-unfetch

Switches between unfetch & node-fetch for client & server.

MIT
Latest version published 1 year ago

Package Health Score

73 / 100
Full package analysis