docs: updated keyGenerator info

This commit is contained in:
arthurfiorette 2022-01-14 18:50:30 -03:00
parent 9379fce100
commit 3c8b373d51
No known key found for this signature in database
GPG Key ID: 9D190CD53C53C555

View File

@ -45,21 +45,20 @@ property.
By default, it uses the `method`, `baseURL`, `params`, `data` and `url` properties from
the request object into an hashcode generated by the `object-code` library.
You can make two "different" requests share the same cache with this property.
An example:
If you need to change or share the cache for two requests based on other properties, like
headers and etc, you can create your own Key Generator:
```js #runkit
const axios = require('axios');
const { setupCache, buildKeyGenerator } = require('axios-cache-interceptor');
const generator = buildKeyGenerator(({ headers }) => {
const generator = buildKeyGenerator(({ headers = {} }) => {
// In this imaginary example, two requests will
// be treated as the same if their x-cache-server header is the same.
// The result of this function, being a object or not, will be
// hashed by `object-code` library.
return headers?.['x-cache-server'] || 'not-set';
return headers['x-cache-server'] || 'not-set';
});
const axios = mockAxios({