mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: removed "abstract" buildInterceptor function
This commit is contained in:
parent
d30b862605
commit
f30e2622dd
@ -1,35 +1,10 @@
|
||||
import type {
|
||||
AxiosCacheInstance,
|
||||
CacheAxiosResponse,
|
||||
CacheRequestConfig
|
||||
} from '../cache/axios';
|
||||
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios';
|
||||
|
||||
export interface AxiosInterceptor<T> {
|
||||
onFulfilled?(value: T): T | Promise<T>;
|
||||
onRejected?(error: any): any;
|
||||
apply: (axios: AxiosCacheInstance) => void;
|
||||
apply: () => void;
|
||||
}
|
||||
|
||||
export type RequestInterceptor = AxiosInterceptor<CacheRequestConfig<unknown, unknown>>;
|
||||
export type ResponseInterceptor = AxiosInterceptor<CacheAxiosResponse<unknown, unknown>>;
|
||||
|
||||
export function buildInterceptor(
|
||||
type: 'request',
|
||||
interceptor: Omit<RequestInterceptor, 'apply'>
|
||||
): RequestInterceptor;
|
||||
|
||||
export function buildInterceptor(
|
||||
type: 'response',
|
||||
interceptor: Omit<ResponseInterceptor, 'apply'>
|
||||
): ResponseInterceptor;
|
||||
|
||||
export function buildInterceptor(
|
||||
type: 'request' | 'response',
|
||||
{ onFulfilled, onRejected }: Omit<AxiosInterceptor<unknown>, 'apply'>
|
||||
): AxiosInterceptor<unknown> {
|
||||
return {
|
||||
onFulfilled,
|
||||
onRejected,
|
||||
apply: (axios) => axios.interceptors[type].use(onFulfilled, onRejected)
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import { deferred } from 'fast-defer';
|
||||
import { buildInterceptor } from '..';
|
||||
import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios';
|
||||
import type {
|
||||
CachedResponse,
|
||||
CachedStorageValue,
|
||||
LoadingStorageValue
|
||||
} from '../storage/types';
|
||||
import type { RequestInterceptor } from './build';
|
||||
import {
|
||||
ConfigWithCache,
|
||||
createValidateStatus,
|
||||
@ -14,8 +14,7 @@ import {
|
||||
} from './util';
|
||||
|
||||
export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
return buildInterceptor('request', {
|
||||
onFulfilled: async (config) => {
|
||||
const onFulfilled: RequestInterceptor['onFulfilled'] = async (config) => {
|
||||
if (config.cache === false) {
|
||||
return config;
|
||||
}
|
||||
@ -35,9 +34,9 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
// Not cached, continue the request, and mark it as fetching
|
||||
emptyOrStale: if (cache.state == 'empty' || cache.state === 'stale') {
|
||||
/**
|
||||
* This checks for simultaneous access to a new key. The js event loop jumps on
|
||||
* the first await statement, so the second (asynchronous call) request may have
|
||||
* already started executing.
|
||||
* This checks for simultaneous access to a new key. The js event loop jumps on the
|
||||
* first await statement, so the second (asynchronous call) request may have already
|
||||
* started executing.
|
||||
*/
|
||||
if (axios.waiting[key]) {
|
||||
cache = (await axios.storage.get(key)) as
|
||||
@ -50,8 +49,8 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
axios.waiting[key] = deferred();
|
||||
|
||||
/**
|
||||
* Add a default reject handler to catch when the request is aborted without
|
||||
* others waiting for it.
|
||||
* Add a default reject handler to catch when the request is aborted without others
|
||||
* waiting for it.
|
||||
*/
|
||||
axios.waiting[key]?.catch(() => undefined);
|
||||
|
||||
@ -107,6 +106,10 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
});
|
||||
|
||||
return config;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
onFulfilled,
|
||||
apply: () => axios.interceptors.request.use(onFulfilled)
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,15 +1,32 @@
|
||||
import { buildInterceptor } from '..';
|
||||
import type { AxiosCacheInstance } from '../cache/axios';
|
||||
import type { CacheProperties } from '../cache/cache';
|
||||
import type { CachedStorageValue } from '../storage/types';
|
||||
import { shouldCacheResponse } from '../util/cache-predicate';
|
||||
import { Header } from '../util/headers';
|
||||
import { updateCache } from '../util/update-cache';
|
||||
import { rejectResponse, setupCacheData } from './util';
|
||||
import type { ResponseInterceptor } from './build';
|
||||
import { setupCacheData } from './util';
|
||||
|
||||
export function defaultResponseInterceptor(axios: AxiosCacheInstance) {
|
||||
return buildInterceptor('response', {
|
||||
onFulfilled: async (response) => {
|
||||
export function defaultResponseInterceptor(
|
||||
axios: AxiosCacheInstance
|
||||
): ResponseInterceptor {
|
||||
/**
|
||||
* Rejects cache for an response response.
|
||||
*
|
||||
* Also update the waiting list for this key by rejecting it.
|
||||
*/
|
||||
const rejectResponse = async (
|
||||
{ storage, waiting }: AxiosCacheInstance,
|
||||
responseId: string
|
||||
) => {
|
||||
// Update the cache to empty to prevent infinite loading state
|
||||
await storage.remove(responseId);
|
||||
// Reject the deferred if present
|
||||
waiting[responseId]?.reject(null);
|
||||
delete waiting[responseId];
|
||||
};
|
||||
|
||||
const onFulfilled: ResponseInterceptor['onFulfilled'] = async (response) => {
|
||||
response.id ??= axios.generateKey(response.config);
|
||||
response.cached ??= false;
|
||||
|
||||
@ -106,6 +123,10 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance) {
|
||||
|
||||
// Return the response with cached as false, because it was not cached at all
|
||||
return response;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
onFulfilled,
|
||||
apply: () => axios.interceptors.response.use(onFulfilled)
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,9 +1,5 @@
|
||||
import type { Method } from 'axios';
|
||||
import type {
|
||||
AxiosCacheInstance,
|
||||
CacheAxiosResponse,
|
||||
CacheRequestConfig
|
||||
} from '../cache/axios';
|
||||
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios';
|
||||
import type { CacheProperties } from '../cache/cache';
|
||||
import type { CachedResponse, StaleStorageValue } from '../storage/types';
|
||||
import { Header } from '../util/headers';
|
||||
@ -21,11 +17,10 @@ export function createValidateStatus(
|
||||
}
|
||||
|
||||
/** Checks if the given method is in the methods array */
|
||||
export function isMethodIn(requestMethod?: Method, methodList?: Method[]): boolean {
|
||||
if (!requestMethod || !methodList) {
|
||||
return false;
|
||||
}
|
||||
|
||||
export function isMethodIn(
|
||||
requestMethod: Method = 'get',
|
||||
methodList: Method[] = []
|
||||
): boolean {
|
||||
requestMethod = requestMethod.toLowerCase() as Lowercase<Method>;
|
||||
|
||||
for (const method of methodList) {
|
||||
@ -99,19 +94,3 @@ export function setupCacheData<R, D>(
|
||||
headers: response.headers
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects cache for an response response.
|
||||
*
|
||||
* Also update the waiting list for this key by rejecting it.
|
||||
*/
|
||||
export async function rejectResponse(
|
||||
{ storage, waiting }: AxiosCacheInstance,
|
||||
responseId: string
|
||||
) {
|
||||
// Update the cache to empty to prevent infinite loading state
|
||||
await storage.remove(responseId);
|
||||
// Reject the deferred if present
|
||||
waiting[responseId]?.reject(null);
|
||||
delete waiting[responseId];
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user