1.1.4 • Published 5 months ago

axios-cacher v1.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Axios Cacher

Cache the response from endpoints and easily invalidate the cache

Install

npm install --save axios-cacher

Usage

Step 1

Import

import setup from "axios-cacher";

Step 2

This package exposes a setup function which can be used as follows:

const { axiosClient, invalidateThis } = setup({
  // base url for your axios config
  baseURL: baseURL,
  // your axios config headers
  headers: {
    Accept: "application/vnd.api+json; version=1.0"
  },
  // cache configuration
  cache: {
    // cache expires after 5 minutes
    maxAge: 5 * 60 * 1000,
    // function to invalidate cache
    invalidate: async (config, request) => {
      if (request.clearCacheEntry) {
        await config.store.removeItem(config.uuid);
      }
    },
  }
});

Step 3

You can use axiosClient in step 1 to make api calls

const response = await axiosClient.get(
      url,
      { userKey: "test" }
    );

Any request after this from the same url will be fetched from the cache for 5 minutes(for this example) unless you invalidate it before. Pass the userKey as shown above so that you can invalidate it using the invalidateThis function.

Use the invalidate function as follows:

invalidateThis(userKey);
1.1.1

5 months ago

1.0.19

5 months ago

1.1.0

5 months ago

1.0.18

5 months ago

1.1.4

5 months ago

1.1.3

5 months ago

1.1.2

5 months ago

1.0.21

5 months ago

1.0.20

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.17

5 months ago

1.0.16

5 months ago

1.0.9

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.11

5 months ago

1.0.10

5 months ago

1.0.15

5 months ago

1.0.14

5 months ago

1.0.13

5 months ago

1.0.12

5 months ago

1.0.0

5 months ago