mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: removed symbol support and more tests
This commit is contained in:
parent
70e5c07ff3
commit
5b6d14f314
@ -19,8 +19,8 @@ import { Deferred } from '../util/deferred';
|
||||
import { KeyGenerator } from '../util/key-generator';
|
||||
|
||||
export type CacheUpdater =
|
||||
| ((cached: EmptyStorageValue | CachedStorageValue, newData: any) => CachedStorageValue | void)
|
||||
| 'delete';
|
||||
| 'delete'
|
||||
| ((cached: EmptyStorageValue | CachedStorageValue, newData: any) => CachedStorageValue | void);
|
||||
|
||||
export type DefaultCacheRequestConfig = AxiosRequestConfig & {
|
||||
cache: CacheProperties;
|
||||
@ -82,7 +82,7 @@ export type CacheAxiosResponse<T = any> = AxiosResponse<T> & {
|
||||
* The id used for this request. if config specified an id, the id
|
||||
* will be returned
|
||||
*/
|
||||
id: string | symbol;
|
||||
id: string;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -95,7 +95,7 @@ export type CacheRequestConfig = AxiosRequestConfig & {
|
||||
*
|
||||
* @default undefined
|
||||
*/
|
||||
id?: string | symbol;
|
||||
id?: string;
|
||||
|
||||
/**
|
||||
* All cache options for the request.
|
||||
@ -124,6 +124,8 @@ export default interface CacheInstance {
|
||||
/**
|
||||
* A simple object that holds all deferred objects until it is
|
||||
* resolved or rejected.
|
||||
*
|
||||
* Can be used to listen when a request is cached or not.
|
||||
*/
|
||||
waiting: Record<string, Deferred<CachedResponse, void>>;
|
||||
|
||||
|
||||
@ -91,9 +91,7 @@ export class CacheResponseInterceptor implements AxiosInterceptor<CacheAxiosResp
|
||||
const deferred = this.axios.waiting[key];
|
||||
|
||||
// Resolve all other requests waiting for this response
|
||||
if (deferred) {
|
||||
await deferred.resolve(newCache.data);
|
||||
}
|
||||
await deferred?.resolve(newCache.data);
|
||||
|
||||
await this.axios.storage.set(key, newCache);
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { StatusCodes } from '../../src';
|
||||
import { axiosMock, mockAxios } from '../mocks/axios';
|
||||
|
||||
describe('test request interceptor', () => {
|
||||
@ -20,4 +21,42 @@ describe('test request interceptor', () => {
|
||||
expect(result.status).toBe(axiosMock.statusCode);
|
||||
expect(result.statusText).toBe(axiosMock.statusText);
|
||||
});
|
||||
|
||||
it('tests header interpreter integration', async () => {
|
||||
const axiosNoCache = mockAxios({}, { 'cache-control': 'no-cache' });
|
||||
|
||||
// Make first request to cache it
|
||||
await axiosNoCache.get('', { cache: { interpretHeader: true } });
|
||||
const resultNoCache = await axiosNoCache.get('');
|
||||
|
||||
expect(resultNoCache.status).toBe(axiosMock.statusCode);
|
||||
expect(resultNoCache.statusText).toBe(axiosMock.statusText);
|
||||
|
||||
const axiosCache = mockAxios({}, { 'cache-control': `maxAge=${60 * 60 * 24 * 365}` });
|
||||
|
||||
// Make first request to cache it
|
||||
await axiosCache.get('', { cache: { interpretHeader: true } });
|
||||
const resultCache = await axiosCache.get('');
|
||||
|
||||
expect(resultCache.status).toBe(StatusCodes.CACHED_STATUS_CODE);
|
||||
expect(resultCache.statusText).toBe(StatusCodes.CACHED_STATUS_TEXT);
|
||||
});
|
||||
|
||||
it('tests update cache integration', async () => {
|
||||
const axios = mockAxios();
|
||||
|
||||
const { id } = await axios.get('key01');
|
||||
|
||||
await axios.get('key02', {
|
||||
cache: {
|
||||
update: {
|
||||
[id]: 'delete' as const
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const cache = await axios.storage.get(id);
|
||||
|
||||
expect(cache.state).toBe('empty');
|
||||
});
|
||||
});
|
||||
|
||||
@ -7,7 +7,8 @@ export const axiosMock = {
|
||||
};
|
||||
|
||||
export function mockAxios(
|
||||
options?: Partial<CacheInstance> & Partial<CacheProperties>
|
||||
options: Partial<CacheInstance> & Partial<CacheProperties> = {},
|
||||
headers: Record<string, string> = {}
|
||||
): AxiosCacheInstance {
|
||||
const axios = createCache({
|
||||
// Defaults to cache every request
|
||||
@ -20,7 +21,7 @@ export function mockAxios(
|
||||
data: true,
|
||||
status: axiosMock.statusCode,
|
||||
statusText: axiosMock.statusText,
|
||||
headers: {},
|
||||
headers,
|
||||
config
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user