Skip to content

Commit

Permalink
refactor: deprecated useCache in favor of createCache (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Dec 27, 2021
1 parent fec63a8 commit 065b6ef
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ Axios Cache Interceptor</h1>

```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 */
});

Expand Down Expand Up @@ -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
Expand All @@ -172,7 +172,7 @@ import { useCache } from 'axios-cache-interceptor';
```

```js
const { useCache } = window.AxiosCacheInterceptor;
const { createCache } = window.AxiosCacheInterceptor;
```

<br />
Expand Down Expand Up @@ -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 */
});
```
Expand Down Expand Up @@ -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
});
```
Expand Down
4 changes: 2 additions & 2 deletions examples/runkit.js
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type CacheOptions = Partial<CacheInstance> & Partial<CacheProperties>;
* @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,
Expand Down Expand Up @@ -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;
4 changes: 2 additions & 2 deletions test/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test/cache/create.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/axios.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,7 +9,7 @@ export function mockAxios(
options: Partial<CacheInstance> & Partial<CacheProperties> = {},
responseHeaders: Record<string, string> = {}
): 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) => {
Expand Down

0 comments on commit 065b6ef

Please sign in to comment.