1.0.0 • Published 5 years ago

api-data-store v1.0.0

Weekly downloads
15
License
ISC
Repository
github
Last release
5 years ago

api-data-store

API Data store

A Data Store that will run the same API endpoint once, cache results, and expire after a timeout.

Basic Usage

import axios from 'axios';
import ApiDataStore from 'api-data-store';

const users = await ApiDataStore.fetch('/api/v1/users', {
  fetchMethod: axios.get,
});

Setting a global fetch method

import axios from 'axios';
import ApiDataStore from 'api-data-store';

ApiDataStore.setFetchMethod(axois.get);

const users = await ApiDataStore.fetch('/api/v1/users');

Caching results in the default localStorage driver

import axios from 'axios';
import ApiDataStore from 'api-data-store';

ApiDataStore.setFetchMethod(axois.get);

const users = await ApiDataStore.fetch('/api/v1/users', {
  cache: true,
});

Setting a prefix for local storage

ApiDataStore.setPrefix('your-api-name:');

// local storage:
// your-api-name:/api/v1/users = { ...data }

Setting a timeout on the cache in milliseconds

const users = await ApiDataStore.fetch('/api/v1/users', {
  cache: true,
  timeout: 1000 * 60 * 5 // 5 minutes
});

Using an in memory custom storage driver

The API Data Store uses the localStorage interface. If you need to create your own custom storage driver use the same methods as localStorage.

import axios from 'axios';
import ApiDataStore from 'api-data-store';
import MemoryDataStore from 'api-data-store/memoryDataStore';

ApiDataStore.setFetchMethod(axois.get);
ApiDataStore.setStorageDriver(MemoryDataStore);

const users = await ApiDataStore.fetch('/api/v1/users', {
  cache: true,
  timeout: 1000 * 60 * 5 // 5 minutes
});
1.0.0

5 years ago

0.2.0

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago