axios-cache-interceptor/docs/pages/global-configuration.md
Arthur Fiorette 7293cf0c26
feat!: new bundle setup & fixed development bundles (#167)
* feat: added issue template

* fix: move dev bundles to a specific folder

* docs: some documentation changes

* docs: updated issue template

* chore!: updated build setup

* style: formatted code

* chore: allow importing any file
2022-03-11 13:46:08 -03:00

2.6 KiB

Global Configuration

When applying the interceptor, you can customize some properties:

const axios = setupCache(axios, {
  // Properties here
});

storage

The storage used to save the cache. Defaults to a simple in-memory storage. See more about storages.

generateKey

The function used to create different keys for each request. Defaults to a function that priorizes the id, and if not specified, a string is generated using the method, baseURL, params, data and url.

The default id generation can clarify this idea.

waiting

A simple object that will hold a promise for each pending request. Used to handle concurrent requests.

Can also be used as type of listener to know when a request is finished.

headerInterpreter

The function used to interpret all headers from a request and determine a time to live (ttl) number.

The possible returns are:

  • 'dont cache': the request will not be cached
  • 'not enough headers': the request will find other ways to determine the ttl
  • number: this will be the ttl value.

Example

// Typescript example!

import { setupCache, type HeaderInterpreter } from 'axios-cache-interceptor';

const myHeaderInterpreter: HeaderInterpreter = (headers) => {
  if (headers['x-my-custom-header']) {
    const seconds = Number(headers['x-my-custom-header']);

    if (seconds < 1) {
      return 'dont cache';
    }

    return seconds;
  }

  return 'not enough headers';
};

request and response Interceptors

These functions intercepts and modify the axios logic and objects. If you are using some sort of custom implementation, it is not guaranteed to any other documented thing work.

At this moment, you can see their code for more information here.

debug

This option only works when targeting a Development build.

The debug option will print debug information in the console. It is good if you need to trace any undesired behavior or issue.

You can enable it by setting debug to a function that receives an string.

// Will print debug info in the console.
setupCache(axios, {
  debug: console.log
});

// own logger or whatever.
setupCache(axios, {
  debug: (message) => {
    // Etc
    myCustomLogger.emit({
      key: 'axios-cache-interceptor',
      log: message
    });
  }
});

// Disables debug.
setupCache(axios, {
  debug: undefined
});
// or
axiosCacheInstance.debug = undefined;