diff --git a/README.md b/README.md index 4933d281..1831aaf8 100644 --- a/README.md +++ b/README.md @@ -69,10 +69,10 @@ Axios Cache Interceptor ```ts import axios from 'axios'; -import { useCache, SessionCacheStorage } from 'axios-cache-interceptor'; +import { createCache, SessionCacheStorage } from 'axios-cache-interceptor'; // An axios instance with modified types -const api = useCache(axios.create(), { +const api = createCache(axios.create(), { /* options */ }); @@ -148,9 +148,9 @@ yarn add axios axios-cache-interceptor ``` ```js -const { useCache } = require('axios-cache-interceptor'); +const { createCache } = require('axios-cache-interceptor'); // or -import { useCache } from 'axios-cache-interceptor'; +import { createCache } from 'axios-cache-interceptor'; ``` ### Via CDN @@ -172,7 +172,7 @@ import { useCache } from 'axios-cache-interceptor'; ``` ```js -const { useCache } = window.AxiosCacheInterceptor; +const { createCache } = window.AxiosCacheInterceptor; ```
@@ -200,13 +200,13 @@ To you use this cache interceptor, you can apply to an existing instance or crea one. ```js -import { useCache } from 'axios-cache-interceptor'; +import { createCache } from 'axios-cache-interceptor'; // Your axios instance let axios; // Return the same axios instance, but with a modified Typescript type. -axios = useCache(axios, { +axios = createCache(axios, { /* options here */ }); ``` @@ -283,7 +283,7 @@ the internal code. Remember that, depending on the When applying the interceptor, you can customize some properties: ```js -const axios = useCache(axios, { +const axios = createCache(axios, { // Properties here }); ``` diff --git a/examples/runkit.js b/examples/runkit.js index 9d17a3c0..f60dbcb7 100644 --- a/examples/runkit.js +++ b/examples/runkit.js @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-var-requires */ const Axios = require('axios'); -const { useCache } = require('axios-cache-interceptor'); +const { createCache } = require('axios-cache-interceptor'); async function main() { const axios = Axios.create({ @@ -18,7 +18,7 @@ async function main() { * ``` */ - const axiosWithCache = useCache(axios, { + const axiosWithCache = createCache(axios, { ttl: 99999, // Parse the Cache-Control header to determine the cache strategy diff --git a/src/cache/create.ts b/src/cache/create.ts index 40233a6f..8cd7607c 100644 --- a/src/cache/create.ts +++ b/src/cache/create.ts @@ -16,7 +16,7 @@ export type CacheOptions = Partial & Partial; * @param config The config for the caching interceptors * @returns The same instance but with caching enabled */ -export function useCache( +export function createCache( axios: AxiosInstance, { storage, @@ -60,3 +60,14 @@ export function useCache( 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; diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 6f006361..ae47257f 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -1,4 +1,4 @@ -import { useCache } from '../src/cache/create'; +import { createCache } from '../src/cache/create'; import { BrowserAxiosStorage } from '../src/storage/browser'; import { MemoryAxiosStorage } from '../src/storage/memory'; import { AxiosStorage } from '../src/storage/storage'; @@ -7,7 +7,7 @@ describe('test bundle imports', () => { it('should have basic storages', async () => { const bundle = await import('../src/index.browser'); - expect(bundle.useCache).toBe(useCache); + expect(bundle.createCache).toBe(createCache); expect(bundle.AxiosStorage).toBe(AxiosStorage); expect(bundle.BrowserAxiosStorage).toBe(BrowserAxiosStorage); expect(bundle.MemoryAxiosStorage).toBe(MemoryAxiosStorage); diff --git a/test/cache/create.test.ts b/test/cache/create.test.ts index 2ed6d875..c9649b93 100644 --- a/test/cache/create.test.ts +++ b/test/cache/create.test.ts @@ -1,13 +1,13 @@ import Axios from 'axios'; -import { useCache } from '../../src/cache/create'; +import { createCache } from '../../src/cache/create'; describe('tests header interpreter', () => { it('tests argument composition', () => { const axios = Axios.create(); - const withAxios = useCache(axios); + const withAxios = createCache(axios); expect(withAxios).not.toBeUndefined(); - const withConfig = useCache(axios, { ttl: 1234 }); + const withConfig = createCache(axios, { ttl: 1234 }); expect(withConfig).not.toBeUndefined(); expect(withConfig.defaults.cache.ttl).toBe(1234); }); diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index 5decf7c6..5f812920 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -1,5 +1,5 @@ import Axios from 'axios'; -import { AxiosCacheInstance, CacheProperties, useCache } from '../../src'; +import { AxiosCacheInstance, CacheProperties, createCache } from '../../src'; import type { CacheInstance } from '../../src/cache/cache'; import { Header } from '../../src/util/headers'; @@ -9,7 +9,7 @@ export function mockAxios( options: Partial & Partial = {}, responseHeaders: Record = {} ): 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.request.use((config) => {