axios-cache-interceptor/docs/src/guide/getting-started.md
2023-02-07 16:10:59 -03:00

5.5 KiB

Getting Started

Looking for axios v0?

Install Axios Cache Interceptor

Add Axios Cache Interceptor and Axios to your project using your favorite package manager:

::: code-group

yarn add axios@1 axios-cache-interceptor@1
npm install axios@1 axios-cache-interceptor@1
<!-- Development UMD build for ES2017+ (~13.3 KiB) -->
<script
  src="https://cdn.jsdelivr.net/npm/axios-cache-interceptor@1.0.1/dev/index.bundle.js"
  integrity="sha256-+VuTj9uoKtkaK4W5NhpOzIa5i2c1c8U4KtGeKT05Xng="
  crossorigin="anonymous"
></script>

<!-- Production UMD build for ES5+ (~14.6 KiB) -->
<script
  src="https://cdn.jsdelivr.net/npm/axios-cache-interceptor@1.0.1/dist/index.bundle.js"
  integrity="sha256-oMsCHuvRgFCk0HWYVIYEB/M1XjRAglYjU+lMQEi3zjY="
  crossorigin="anonymous"
></script>
import Axios from 'https://cdn.skypack.dev/axios';
import { setupCache } from 'https://cdn.skypack.dev/axios-cache-interceptor';

:::

Setup Axios Cache Interceptor

After installing, you can import the package and apply the interceptor to your axios instance, as shown below:

::: code-group

import Axios from 'axios';
import { setupCache } from 'axios-cache-interceptor';

// same object, but with updated typings.
const axios = setupCache(Axios); // [!code focus]

const req1 = axios.get('https://api.example.com/'); // [!code focus]
const req2 = axios.get('https://api.example.com/'); // [!code focus]

const [res1, res2] = await Promise.all([req1, req2]);

res1.cached; // false // [!code focus]
res2.cached; // true // [!code focus]
const Axios = require('axios');
const { setupCache } = require('axios-cache-interceptor');

// same object, but with updated typings.
const axios = setupCache(Axios); // [!code focus]

const req1 = axios.get('https://api.example.com/'); // [!code focus]
const req2 = axios.get('https://api.example.com/'); // [!code focus]

const [res1, res2] = await Promise.all([req1, req2]);

res1.cached; // false // [!code focus]
res2.cached; // true // [!code focus]
const Axios = window.axios;
const { setupCache } = window.AxiosCacheInterceptor;

// same object, but with updated typings.
const axios = setupCache(Axios); // [!code focus]

const req1 = axios.get('https://api.example.com/'); // [!code focus]
const req2 = axios.get('https://api.example.com/'); // [!code focus]

const [res1, res2] = await Promise.all([req1, req2]);

res1.cached; // false // [!code focus]
res2.cached; // true // [!code focus]
import Axios from 'https://cdn.skypack.dev/axios';
import { setupCache } from 'https://cdn.skypack.dev/axios-cache-interceptor';

// same object, but with updated typings.
const axios = setupCache(Axios); // [!code focus]

const req1 = axios.get('https://api.example.com/'); // [!code focus]
const req2 = axios.get('https://api.example.com/'); // [!code focus]

const [res1, res2] = await Promise.all([req1, req2]);

res1.cached; // false // [!code focus]
res2.cached; // true // [!code focus]

:::

Just the above is sufficient for most use cases. However, you can also customize each cache behavior by passing a configuration object to the setupCache function. And you can also customize some behaviors each request by using the cache option in the request config.

Support Table

Most of axios v0 breaking changes were about typing issues, so your version may work with one outside of this table. Axios and Axios Cache Interceptor v0 are not compatible with Axios and Axios Cache Interceptor v1

Note

: Axios was not defined as a peerDependency for all v0 versions, because it had a non-stable semver version. See #145 (Comment)

Axios Axios Cache Interceptor
>= v1.3.1 >= v1
>= v0.27 >= v0.10.3
>= v0.26 >= v0.8.4
~ v0.25 ~ v0.8.4
~ v0.24 >= v0.5 && <= 0.8.3
~ v0.23 ~ v0.4
~ v0.22 ~ v0.3
v0.21 <= v0.2

Read More

Some useful links to get you more familiar with the library: