Skip to content
This repository has been archived by the owner on Mar 5, 2019. It is now read-only.
/ js-fetcher Public archive

A simple fetcher class to better window.fetch interface

License

Notifications You must be signed in to change notification settings

DVS-devtools/js-fetcher

Repository files navigation

js-fetcher

[!!!] The source code of this package is on https://github.com/docomodigital/js-utils, this repository will be removed asap

Build Status Coverage Status npm version Greenkeeper badge

A simple fetcher class to better window.fetch interface

Usage

import Fetcher from '@docomodigital/js-fetcher';

const fetcher = new Fetcher({
    baseURL: 'http://api.com/api/v1',
    options: {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }
})

 fetcher.get('/v01/users').then(res => res.json());
 fetcher.post('/v01/user', {}, { credentials: 'include', body: JSON.stringify({foo: 'bar'}) });
 
 // Using the token generator:
 const generator = new TokenGenerator({
    url: 'http://api.com/oauth/token',
    clientId: 123,
    clientSecret: 'secret123456789'
 });
 fetcher.setTokenGenerator(generator);
 // Now, before every fetch requests, the library will check if the current Bearer token (if present) is valid and will try to get a new one if necessary.
 // The new token will be used as Authentication: Bearer <Token> header

Installation

NPM

npm install --save @docomodigital/js-fetcher

Documentation

To read documentation, go to:

http://docomodigital.github.io/js-fetcher/latest

or run the following command inside the js-fetcher folder:

npm run doc:open