How to use isomorphic-unfetch - 10 common examples

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 yosriady / serverless-stripe-frontend / components / PayButton.js View on Github external
async onToken(token) { // Token returned from Stripe
    const res = await fetch(config.stripe.apiUrl, { // Backend API url
      method: 'POST',
      body: JSON.stringify({
        token,
        charge: {
          amount: this.props.amount,
          currency: config.stripe.currency,
        },
      }),
    });
    const data = await res.json();
    console.log('onToken'); // Logs for ease of debugging
    console.log(data);
  }
github morrys / offline-examples / relay / nextjs / relay / createRelayEnvironment.ts View on Github external
function fetchQuery(operation, variables, cacheConfig, uploadables) {
  const endpoint = 'http://localhost:3000/graphql';
  return fetch(endpoint, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    }, // Add authentication and other headers here
    body: JSON.stringify({
      query: operation.text, // GraphQL text from input
      variables,
    }),
  })
    .then(response => response.json())
    .catch(error => console.log('errrrr', error));
}
github strues / boldr / packages / core / src / server.js View on Github external
// Polyfill for fetch() API in NodeJS based on code from
// https://github.com/matthew-andrews/isomorphic-fetch/blob/master/fetch-npm-node.js
if (!global.fetch) {
  const realFetch = require('isomorphic-unfetch');
  global.fetch = function fetch(url, options) {
    const normalized = /^\/\//.test(url) ? `https:${url}` : url;
    return realFetch.call(this, normalized, options);
  };
  global.Response = realFetch.Response;
  global.Headers = realFetch.Headers;
  global.Request = realFetch.Request;
}

export { default as serverRender } from './server/serverRender';
export * from './common';
github strues / boldr / packages / core / src / server.js View on Github external
// Polyfill for fetch() API in NodeJS based on code from
// https://github.com/matthew-andrews/isomorphic-fetch/blob/master/fetch-npm-node.js
if (!global.fetch) {
  const realFetch = require('isomorphic-unfetch');
  global.fetch = function fetch(url, options) {
    const normalized = /^\/\//.test(url) ? `https:${url}` : url;
    return realFetch.call(this, normalized, options);
  };
  global.Response = realFetch.Response;
  global.Headers = realFetch.Headers;
  global.Request = realFetch.Request;
}

export { default as serverRender } from './server/serverRender';
export * from './common';
github strues / boldr / packages / core / src / server.js View on Github external
// Polyfill for fetch() API in NodeJS based on code from
// https://github.com/matthew-andrews/isomorphic-fetch/blob/master/fetch-npm-node.js
if (!global.fetch) {
  const realFetch = require('isomorphic-unfetch');
  global.fetch = function fetch(url, options) {
    const normalized = /^\/\//.test(url) ? `https:${url}` : url;
    return realFetch.call(this, normalized, options);
  };
  global.Response = realFetch.Response;
  global.Headers = realFetch.Headers;
  global.Request = realFetch.Request;
}

export { default as serverRender } from './server/serverRender';
export * from './common';
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)
github zeit / next.js / examples / with-rematch / shared / models / github.js View on Github external
async fetchUsers() {
      try {
        this.requestUsers()
        const response = await fetch('https://api.github.com/users')
        const users = await response.json()
        this.receiveUsers(users)
      } catch (err) {
        console.log(err)
        this.receiveUsers([])
      }
    },
  },
github strues / boldr / packages / core / src / server.js View on Github external
global.fetch = function fetch(url, options) {
    const normalized = /^\/\//.test(url) ? `https:${url}` : url;
    return realFetch.call(this, normalized, options);
  };
  global.Response = realFetch.Response;
github skidding / flatris / utils / api.js View on Github external
function fetchJson(urlPath: string, options?: Object): Promise {
  return fetch(getApiUrl(urlPath), options).then(parseResponse);
}

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