axios-cache-interceptor/docs/pages/usage-examples.md
2022-01-06 19:23:08 -03:00

1.4 KiB

Interceptor

Applying

This library is based on axios interceptors, so, under the hood, it uses axios.interceptors.use() to apply the interceptors. But you don't. All you have to do is call setupCache and you are ready to go!

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

setupCache(axios);

How to get the axios instance

There are two types of axios instances, the AxiosStatic and the AxiosInstance. The AxiosStatic is the default instance of axios. The AxiosInstance is the instance you get when you call axios.create().

Both of them work seamlessly, but when messing with the axios static, your hole code, including those libraries you don't know that their exists, are also affected. You should be careful when using it.

// AxiosStatic
import axios from 'axios';

// AxiosInstance
const instance = axios.create();

Customizing behaviors

You can customize the behaviors of this library in two ways, in a per request or in a global way.

import Axios from 'axios';

const instance = Axios.create({
  /** Here you can pass the axios options * */
});

// Global
setupCache(instance, {
  /** Here you can pass the interceptor options * */
});

// Per request
await instance.get('url', {
  /** Override axios options * */
  cache: {
    /** Override cache options * */
  }
});