Skip to content

Commit

Permalink
refactor!: removed isAxiosCacheInterceptor function
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 8, 2022
1 parent f30e262 commit 37431a9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 34 deletions.
19 changes: 2 additions & 17 deletions src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import type { CacheInstance, CacheProperties } from './cache';

export type CacheOptions = Partial<CacheInstance> & Partial<CacheProperties>;

const symbolKey = Symbol();

/**
* Apply the caching interceptors for a already created axios instance.
*
Expand Down Expand Up @@ -92,11 +90,8 @@ export function setupCache(
};

// Apply interceptors
axiosCache.requestInterceptor.apply(axiosCache);
axiosCache.responseInterceptor.apply(axiosCache);

// @ts-expect-error - internal only
axiosCache[symbolKey] = 1;
axiosCache.requestInterceptor.apply();
axiosCache.responseInterceptor.apply();

return axiosCache;
}
Expand All @@ -105,13 +100,3 @@ export function setupCache(
export const useCache = setupCache as unknown as 'use setupCache instead';
/** @deprecated */
export const createCache = setupCache as unknown as 'use setupCache instead';

/**
* Detects if the given parameter has caching enabled. The only way to this return true is
* by using the {@link setupCache} function.
*
* @param axios The axios instance to use
* @returns True if the axios instance is using the caching interceptor
*/
export const isAxiosCacheInterceptor = (axios: any): axios is AxiosCacheInstance =>
!!axios && !!axios[symbolKey];
3 changes: 1 addition & 2 deletions test/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAxiosCacheInterceptor, setupCache } from '../src/cache/create';
import { setupCache } from '../src/cache/create';
import { buildMemoryStorage } from '../src/storage/memory';
import { buildWebStorage } from '../src/storage/web-api';

Expand All @@ -12,7 +12,6 @@ describe('test bundle imports', () => {
expect(console.warn).toHaveBeenCalledTimes(1);

expect(bundle.setupCache).toBe(setupCache);
expect(bundle.isAxiosCacheInterceptor).toBe(isAxiosCacheInterceptor);
expect(bundle.buildMemoryStorage).toBe(buildMemoryStorage);
expect(bundle.buildWebStorage).toBe(buildWebStorage);

Expand Down
16 changes: 1 addition & 15 deletions test/cache/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Axios from 'axios';
import { isAxiosCacheInterceptor, setupCache } from '../../src/cache/create';
import { setupCache } from '../../src/cache/create';

describe('tests header interpreter', () => {
it('tests argument composition', () => {
Expand All @@ -11,18 +11,4 @@ describe('tests header interpreter', () => {
expect(withConfig).not.toBeUndefined();
expect(withConfig.defaults.cache.ttl).toBe(1234);
});

it('tests isAxiosCacheInterceptor', () => {
expect(isAxiosCacheInterceptor(void 0)).toBe(false);
expect(isAxiosCacheInterceptor(1)).toBe(false);
expect(isAxiosCacheInterceptor('a')).toBe(false);
expect(isAxiosCacheInterceptor({})).toBe(false);
expect(isAxiosCacheInterceptor(Axios)).toBe(false);
expect(isAxiosCacheInterceptor(() => 0)).toBe(false);
expect(isAxiosCacheInterceptor(null)).toBe(false);
expect(isAxiosCacheInterceptor(undefined)).toBe(false);
expect(isAxiosCacheInterceptor({ a: 1, b: 'a' })).toBe(false);

expect(isAxiosCacheInterceptor(setupCache(Axios.create()))).toBe(true);
});
});

0 comments on commit 37431a9

Please sign in to comment.