Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bivrost-fetch-adapter

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bivrost-fetch-adapter

Bivrost adapter using browser's native fetch function

  • 3.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Bivrost fetch adapter

Adapter for browser's native fetch function.

yarn add bivrost-fetch-adapter

Usage

With Bivrost:

import DataSource from 'bivrost/data/source';
import bivrostApi from 'bivrost/http/api';
import fetchAdapter from 'bivrost-fetch-adapter';

const api = bivrostApi({
  host: 'localhost',
  adapter: fetchAdapter(),
});

class UsersDataSource extends DataSource {
  static api = {
    loadAll: api('GET /users'),
  };

  loadUsers(filters) {
    return this.invoke('loadAll', filters);
  }
}

Direct calls:

import fetchAdapter from 'bivrost-fetch-adapter';

const api = fetchAdapter();

const options = {
  method: 'GET',
  query: {
    groupId: 10,
  },
  headers: {
    'Content-Type': 'application/json',
  },
};

api('/users', options) // /users?groupId=10
  .then(json => {
    // ...
  })
  .catch(response => {
    // ...
  });

const options = {
  method: 'POST',
  body: {
    name: 'kek',
  },
  headers: {
    'Content-Type': 'application/json',
  },
};

api('/user/1', options)
  .then(json => {
    // ...
  })
  .catch(response => {
    // ...
  });

Configuration

import fetchAdapter from 'bivrost-fetch-adapter';

const api = fetchAdapter({
  // default options for each request with created adapter
  options: {
    mode: 'cors',
    redirect: 'follow',
  },
});

Available options:

  • headers - associated Headers object
  • referrer - referrer of the request
  • mode - cors, no-cors, same-origin
  • credentials - should cookies go with the request? omit, same-origin
  • redirect - follow, error, manual
  • integrity - subresource integrity value
  • cache - cache mode (default, reload, no-cache)

Interceptors

import fetchAdapter from 'bivrost-fetch-adapter';

const api = fetchAdapter({
  interceptors: {
    // takes Request instance as argument
    request: request => {
      // ...
    },

    error: error => {
      // ...
    },

    // takes Response instance as argument
    response: response => {
      // ...
    },
  },
});

Interceptor example:

import fetchAdapter from 'bivrost-fetch-adapter';

const api = fetchAdapter({
  interceptors: {
    request: request => {
      request.headers.set('Content-Type', 'application/json');
      request.headers.set('access_token', Auth.accessToken);

      return request;
    },

    error: error => Promise.reject(error);
    response: response => Promise.resolve(response)
  }
});

Fetch polyfill

Github whatwg-fetch - https://github.com/github/fetch


Bivrost allows to organize a simple interface to asyncronous APIs.

Other adapters

Keywords

FAQs

Package last updated on 14 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc