mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: deprecated useCache in favor of createCache (#95)
This commit is contained in:
parent
fec63a84a7
commit
065b6eff74
16
README.md
16
README.md
@ -69,10 +69,10 @@ Axios Cache Interceptor</h1>
|
|||||||
|
|
||||||
```ts
|
```ts
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { useCache, SessionCacheStorage } from 'axios-cache-interceptor';
|
import { createCache, SessionCacheStorage } from 'axios-cache-interceptor';
|
||||||
|
|
||||||
// An axios instance with modified types
|
// An axios instance with modified types
|
||||||
const api = useCache(axios.create(), {
|
const api = createCache(axios.create(), {
|
||||||
/* options */
|
/* options */
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -148,9 +148,9 @@ yarn add axios axios-cache-interceptor
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const { useCache } = require('axios-cache-interceptor');
|
const { createCache } = require('axios-cache-interceptor');
|
||||||
// or
|
// or
|
||||||
import { useCache } from 'axios-cache-interceptor';
|
import { createCache } from 'axios-cache-interceptor';
|
||||||
```
|
```
|
||||||
|
|
||||||
### Via CDN
|
### Via CDN
|
||||||
@ -172,7 +172,7 @@ import { useCache } from 'axios-cache-interceptor';
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const { useCache } = window.AxiosCacheInterceptor;
|
const { createCache } = window.AxiosCacheInterceptor;
|
||||||
```
|
```
|
||||||
|
|
||||||
<br />
|
<br />
|
||||||
@ -200,13 +200,13 @@ To you use this cache interceptor, you can apply to an existing instance or crea
|
|||||||
one.
|
one.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { useCache } from 'axios-cache-interceptor';
|
import { createCache } from 'axios-cache-interceptor';
|
||||||
|
|
||||||
// Your axios instance
|
// Your axios instance
|
||||||
let axios;
|
let axios;
|
||||||
|
|
||||||
// Return the same axios instance, but with a modified Typescript type.
|
// Return the same axios instance, but with a modified Typescript type.
|
||||||
axios = useCache(axios, {
|
axios = createCache(axios, {
|
||||||
/* options here */
|
/* options here */
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
@ -283,7 +283,7 @@ the internal code. Remember that, depending on the
|
|||||||
When applying the interceptor, you can customize some properties:
|
When applying the interceptor, you can customize some properties:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const axios = useCache(axios, {
|
const axios = createCache(axios, {
|
||||||
// Properties here
|
// Properties here
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||||
|
|
||||||
const Axios = require('axios');
|
const Axios = require('axios');
|
||||||
const { useCache } = require('axios-cache-interceptor');
|
const { createCache } = require('axios-cache-interceptor');
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const axios = Axios.create({
|
const axios = Axios.create({
|
||||||
@ -18,7 +18,7 @@ async function main() {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const axiosWithCache = useCache(axios, {
|
const axiosWithCache = createCache(axios, {
|
||||||
ttl: 99999,
|
ttl: 99999,
|
||||||
|
|
||||||
// Parse the Cache-Control header to determine the cache strategy
|
// Parse the Cache-Control header to determine the cache strategy
|
||||||
|
|||||||
13
src/cache/create.ts
vendored
13
src/cache/create.ts
vendored
@ -16,7 +16,7 @@ export type CacheOptions = Partial<CacheInstance> & Partial<CacheProperties>;
|
|||||||
* @param config The config for the caching interceptors
|
* @param config The config for the caching interceptors
|
||||||
* @returns The same instance but with caching enabled
|
* @returns The same instance but with caching enabled
|
||||||
*/
|
*/
|
||||||
export function useCache(
|
export function createCache(
|
||||||
axios: AxiosInstance,
|
axios: AxiosInstance,
|
||||||
{
|
{
|
||||||
storage,
|
storage,
|
||||||
@ -60,3 +60,14 @@ export function useCache(
|
|||||||
|
|
||||||
return axiosCache;
|
return axiosCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply the caching interceptors for a already created axios instance.
|
||||||
|
*
|
||||||
|
* @param axios The already created axios instance
|
||||||
|
* @param config The config for the caching interceptors
|
||||||
|
* @returns The same instance but with caching enabled
|
||||||
|
*
|
||||||
|
* @deprecated Prefer {@link createCache}
|
||||||
|
*/
|
||||||
|
export const useCache = createCache;
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { useCache } from '../src/cache/create';
|
import { createCache } from '../src/cache/create';
|
||||||
import { BrowserAxiosStorage } from '../src/storage/browser';
|
import { BrowserAxiosStorage } from '../src/storage/browser';
|
||||||
import { MemoryAxiosStorage } from '../src/storage/memory';
|
import { MemoryAxiosStorage } from '../src/storage/memory';
|
||||||
import { AxiosStorage } from '../src/storage/storage';
|
import { AxiosStorage } from '../src/storage/storage';
|
||||||
@ -7,7 +7,7 @@ describe('test bundle imports', () => {
|
|||||||
it('should have basic storages', async () => {
|
it('should have basic storages', async () => {
|
||||||
const bundle = await import('../src/index.browser');
|
const bundle = await import('../src/index.browser');
|
||||||
|
|
||||||
expect(bundle.useCache).toBe(useCache);
|
expect(bundle.createCache).toBe(createCache);
|
||||||
expect(bundle.AxiosStorage).toBe(AxiosStorage);
|
expect(bundle.AxiosStorage).toBe(AxiosStorage);
|
||||||
expect(bundle.BrowserAxiosStorage).toBe(BrowserAxiosStorage);
|
expect(bundle.BrowserAxiosStorage).toBe(BrowserAxiosStorage);
|
||||||
expect(bundle.MemoryAxiosStorage).toBe(MemoryAxiosStorage);
|
expect(bundle.MemoryAxiosStorage).toBe(MemoryAxiosStorage);
|
||||||
|
|||||||
6
test/cache/create.test.ts
vendored
6
test/cache/create.test.ts
vendored
@ -1,13 +1,13 @@
|
|||||||
import Axios from 'axios';
|
import Axios from 'axios';
|
||||||
import { useCache } from '../../src/cache/create';
|
import { createCache } from '../../src/cache/create';
|
||||||
|
|
||||||
describe('tests header interpreter', () => {
|
describe('tests header interpreter', () => {
|
||||||
it('tests argument composition', () => {
|
it('tests argument composition', () => {
|
||||||
const axios = Axios.create();
|
const axios = Axios.create();
|
||||||
const withAxios = useCache(axios);
|
const withAxios = createCache(axios);
|
||||||
expect(withAxios).not.toBeUndefined();
|
expect(withAxios).not.toBeUndefined();
|
||||||
|
|
||||||
const withConfig = useCache(axios, { ttl: 1234 });
|
const withConfig = createCache(axios, { ttl: 1234 });
|
||||||
expect(withConfig).not.toBeUndefined();
|
expect(withConfig).not.toBeUndefined();
|
||||||
expect(withConfig.defaults.cache.ttl).toBe(1234);
|
expect(withConfig.defaults.cache.ttl).toBe(1234);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Axios from 'axios';
|
import Axios from 'axios';
|
||||||
import { AxiosCacheInstance, CacheProperties, useCache } from '../../src';
|
import { AxiosCacheInstance, CacheProperties, createCache } from '../../src';
|
||||||
import type { CacheInstance } from '../../src/cache/cache';
|
import type { CacheInstance } from '../../src/cache/cache';
|
||||||
import { Header } from '../../src/util/headers';
|
import { Header } from '../../src/util/headers';
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ export function mockAxios(
|
|||||||
options: Partial<CacheInstance> & Partial<CacheProperties> = {},
|
options: Partial<CacheInstance> & Partial<CacheProperties> = {},
|
||||||
responseHeaders: Record<string, string> = {}
|
responseHeaders: Record<string, string> = {}
|
||||||
): AxiosCacheInstance {
|
): AxiosCacheInstance {
|
||||||
const axios = useCache(Axios.create(), options);
|
const axios = createCache(Axios.create(), options);
|
||||||
|
|
||||||
// Axios interceptors are a stack, so apply this after the cache interceptor
|
// Axios interceptors are a stack, so apply this after the cache interceptor
|
||||||
axios.interceptors.request.use((config) => {
|
axios.interceptors.request.use((config) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user