Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

Latest commit

 

History

History
42 lines (32 loc) · 1.05 KB

FETCH.md

File metadata and controls

42 lines (32 loc) · 1.05 KB

Enabling fetch on both the browser and the server

Adding fetch to the browser

Install whatwg-fetch and add it to polyfills.ts

Adding fetch to the server

Install node-fetch and import it in server.ts. Then set the global fetch to use this import as follows:

import fetch from 'node-fetch';
global['fetch'] = fetch;

Overriding fetch options on the server

In order to make sure that absolute URLs are used on the server, and also to add authentication cookies to all requests, you need to install fetch-intercept and follow usage instructions below:

import * as fetchIntercept from 'fetch-intercept';

app.get('*', (req, res) => {
  const unregister = fetchIntercept.register({
    request: function (url, config) {
      if (!url.startsWith('http')) {
        url = `${req.protocol}://${req.get('host')}${url}`;
      }
      if (!!req.headers.cookie) {
        config.headers['cookie'] = req.headers.cookie
      }
      return [url, config];
    }
  });
  
  // ... perform op ...
  
  unregister();
})