JavaScript API Client Class: Queue, Fetch and Cache

An ES6 class to queue API requests, fetch results and cache for performances. See the script on gist.github.com.

How to Use

I’m glad you’re asking. Let’s take the Movie Database for instance.

import API from './api';

const theMovieDB = new API('https://api.themoviedb.org/3', {
    api_key: 'YOUR-API-KEY'
}, 1000 / 4);

We instanciate the API to request with an api_key parameter, and set the rate limit to 4 requests per second.

const topRatedRequest = theMovieDB
    .get(`movie/top_rated`, {}, 12 * 60 * 60);

Then we queue a GET request and set it to cache for twelve hours.

topRatedRequest.done(data => console.log('Top Rated Movies', data.results));

When response is available, we log the results.

topRatedRequest.cancel();

But we could also cancel this request.