mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
feat: added isAxiosCacheInterceptor function
This commit is contained in:
parent
e6a9d1223a
commit
c32c452190
15
src/cache/create.ts
vendored
15
src/cache/create.ts
vendored
@ -9,6 +9,8 @@ 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.
|
||||
*
|
||||
@ -87,6 +89,9 @@ export function setupCache(
|
||||
axiosCache.requestInterceptor.use();
|
||||
axiosCache.responseInterceptor.use();
|
||||
|
||||
// @ts-expect-error - internal only
|
||||
axiosCache[symbolKey] = 1;
|
||||
|
||||
return axiosCache;
|
||||
}
|
||||
|
||||
@ -94,3 +99,13 @@ 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];
|
||||
|
||||
@ -1,6 +1,11 @@
|
||||
/** Index file for webpack and cdn usage */
|
||||
|
||||
export { createCache, setupCache, useCache } from './cache/create';
|
||||
export {
|
||||
createCache,
|
||||
isAxiosCacheInterceptor,
|
||||
setupCache,
|
||||
useCache
|
||||
} from './cache/create';
|
||||
export { BrowserAxiosStorage } from './storage/browser';
|
||||
export { MemoryAxiosStorage } from './storage/memory';
|
||||
export { AxiosStorage } from './storage/storage';
|
||||
|
||||
@ -1,6 +1,11 @@
|
||||
/** Index file for webpack and cdn usage */
|
||||
|
||||
export { createCache, setupCache, useCache } from './cache/create';
|
||||
export {
|
||||
createCache,
|
||||
isAxiosCacheInterceptor,
|
||||
setupCache,
|
||||
useCache
|
||||
} from './cache/create';
|
||||
export { BrowserAxiosStorage } from './storage/browser';
|
||||
export { MemoryAxiosStorage } from './storage/memory';
|
||||
export { AxiosStorage } from './storage/storage';
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { setupCache } from '../src/cache/create';
|
||||
import { isAxiosCacheInterceptor, setupCache } from '../src/cache/create';
|
||||
import { BrowserAxiosStorage } from '../src/storage/browser';
|
||||
import { MemoryAxiosStorage } from '../src/storage/memory';
|
||||
import { AxiosStorage } from '../src/storage/storage';
|
||||
@ -8,6 +8,7 @@ describe('test bundle imports', () => {
|
||||
const bundle = await import('../src/index.browser');
|
||||
|
||||
expect(bundle.setupCache).toBe(setupCache);
|
||||
expect(bundle.isAxiosCacheInterceptor).toBe(isAxiosCacheInterceptor);
|
||||
expect(bundle.AxiosStorage).toBe(AxiosStorage);
|
||||
expect(bundle.BrowserAxiosStorage).toBe(BrowserAxiosStorage);
|
||||
expect(bundle.MemoryAxiosStorage).toBe(MemoryAxiosStorage);
|
||||
@ -22,6 +23,7 @@ describe('test bundle imports', () => {
|
||||
expect(console.warn).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(bundle.setupCache).toBe(setupCache);
|
||||
expect(bundle.isAxiosCacheInterceptor).toBe(isAxiosCacheInterceptor);
|
||||
expect(bundle.AxiosStorage).toBe(AxiosStorage);
|
||||
expect(bundle.BrowserAxiosStorage).toBe(BrowserAxiosStorage);
|
||||
expect(bundle.MemoryAxiosStorage).toBe(MemoryAxiosStorage);
|
||||
|
||||
16
test/cache/create.test.ts
vendored
16
test/cache/create.test.ts
vendored
@ -1,5 +1,5 @@
|
||||
import Axios from 'axios';
|
||||
import { setupCache } from '../../src/cache/create';
|
||||
import { isAxiosCacheInterceptor, setupCache } from '../../src/cache/create';
|
||||
|
||||
describe('tests header interpreter', () => {
|
||||
it('tests argument composition', () => {
|
||||
@ -11,4 +11,18 @@ 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);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user