mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
fix: eslint warnings
This commit is contained in:
parent
eddff579c5
commit
a5ebad3d14
@ -5,7 +5,7 @@ import type { HeadersInterpreter } from './types';
|
|||||||
export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => {
|
export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => {
|
||||||
if (!headers) return 'not enough headers';
|
if (!headers) return 'not enough headers';
|
||||||
|
|
||||||
const cacheControl = headers[Header.CacheControl];
|
const cacheControl: unknown = headers[Header.CacheControl];
|
||||||
|
|
||||||
if (cacheControl) {
|
if (cacheControl) {
|
||||||
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(
|
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(
|
||||||
@ -29,7 +29,7 @@ export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (maxAge !== undefined) {
|
if (maxAge !== undefined) {
|
||||||
const age = headers[Header.Age];
|
const age: unknown = headers[Header.Age];
|
||||||
|
|
||||||
if (!age) {
|
if (!age) {
|
||||||
return maxAge * 1000;
|
return maxAge * 1000;
|
||||||
@ -39,7 +39,7 @@ export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const expires = headers[Header.Expires];
|
const expires: unknown = headers[Header.Expires];
|
||||||
|
|
||||||
if (expires) {
|
if (expires) {
|
||||||
const milliseconds = Date.parse(String(expires)) - Date.now();
|
const milliseconds = Date.parse(String(expires)) - Date.now();
|
||||||
|
|||||||
@ -42,15 +42,19 @@ export function updateStaleRequest<D>(
|
|||||||
const { etag, modifiedSince } = config.cache;
|
const { etag, modifiedSince } = config.cache;
|
||||||
|
|
||||||
if (etag) {
|
if (etag) {
|
||||||
const etagValue = etag === true ? cache.data?.headers[Header.ETag] : etag;
|
const etagValue =
|
||||||
etagValue && (config.headers[Header.IfNoneMatch] = etagValue);
|
etag === true ? (cache.data?.headers[Header.ETag] as unknown) : etag;
|
||||||
|
|
||||||
|
if (etagValue) {
|
||||||
|
config.headers[Header.IfNoneMatch] = etagValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (modifiedSince) {
|
if (modifiedSince) {
|
||||||
config.headers[Header.IfModifiedSince] =
|
config.headers[Header.IfModifiedSince] =
|
||||||
modifiedSince === true
|
modifiedSince === true
|
||||||
? // If last-modified is not present, use the createdAt timestamp
|
? // If last-modified is not present, use the createdAt timestamp
|
||||||
cache.data.headers[Header.LastModified] ||
|
(cache.data.headers[Header.LastModified] as unknown) ||
|
||||||
new Date(cache.createdAt).toUTCString()
|
new Date(cache.createdAt).toUTCString()
|
||||||
: modifiedSince.toUTCString();
|
: modifiedSince.toUTCString();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import type { CacheAxiosResponse } from '../cache/axios';
|
import type { CacheAxiosResponse } from '../cache/axios';
|
||||||
import type { CachePredicate } from './types';
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
import type { CachePredicate, CachePredicateObject } from './types';
|
||||||
|
|
||||||
/** Tests an response against a {@link CachePredicateObject}. */
|
/** Tests an response against a {@link CachePredicateObject}. */
|
||||||
export async function testCachePredicate<R = unknown, D = unknown>(
|
export async function testCachePredicate<R = unknown, D = unknown>(
|
||||||
@ -24,6 +25,9 @@ export async function testCachePredicate<R = unknown, D = unknown>(
|
|||||||
if (
|
if (
|
||||||
!(await predicate(
|
!(await predicate(
|
||||||
// Axios response headers are in lowercase, but check both just in case.
|
// Axios response headers are in lowercase, but check both just in case.
|
||||||
|
|
||||||
|
// FIXME: https://github.com/axios/axios/pull/5525
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||||
response.headers[header.toLowerCase()] ?? response.headers[header]
|
response.headers[header.toLowerCase()] ?? response.headers[header]
|
||||||
))
|
))
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -22,7 +22,10 @@ export type CachePredicateObject<R = unknown, D = unknown> = {
|
|||||||
*
|
*
|
||||||
* ### Remember, all axios headers are lowercase.
|
* ### Remember, all axios headers are lowercase.
|
||||||
*/
|
*/
|
||||||
containsHeaders?: Record<string, (header?: string) => MaybePromise<boolean>>;
|
containsHeaders?: Record<
|
||||||
|
string,
|
||||||
|
(header?: CacheAxiosResponse['headers'][string]) => MaybePromise<boolean>
|
||||||
|
>;
|
||||||
|
|
||||||
/** Check if the response matches this predicate. */
|
/** Check if the response matches this predicate. */
|
||||||
responseMatch?: (res: CacheAxiosResponse<R, D>) => MaybePromise<boolean>;
|
responseMatch?: (res: CacheAxiosResponse<R, D>) => MaybePromise<boolean>;
|
||||||
|
|||||||
@ -104,7 +104,7 @@ describe('Last-Modified handling', () => {
|
|||||||
|
|
||||||
// First request, return x-my-header. Ttl 1 to make the cache stale
|
// First request, return x-my-header. Ttl 1 to make the cache stale
|
||||||
const firstResponse = await axios.get('http://test.com', { cache: { ttl: -1 } });
|
const firstResponse = await axios.get('http://test.com', { cache: { ttl: -1 } });
|
||||||
const firstMyHeader = firstResponse.headers?.[XMockRandom];
|
const firstMyHeader: unknown = firstResponse.headers?.[XMockRandom];
|
||||||
|
|
||||||
expect(firstMyHeader).toBeDefined();
|
expect(firstMyHeader).toBeDefined();
|
||||||
expect(Number(firstMyHeader)).not.toBeNaN();
|
expect(Number(firstMyHeader)).not.toBeNaN();
|
||||||
@ -113,7 +113,7 @@ describe('Last-Modified handling', () => {
|
|||||||
const secondResponse = await axios.get('http://test.com', {
|
const secondResponse = await axios.get('http://test.com', {
|
||||||
cache: { modifiedSince: true }
|
cache: { modifiedSince: true }
|
||||||
});
|
});
|
||||||
const secondMyHeader = secondResponse.headers?.[XMockRandom];
|
const secondMyHeader: unknown = secondResponse.headers?.[XMockRandom];
|
||||||
|
|
||||||
expect(secondMyHeader).toBeDefined();
|
expect(secondMyHeader).toBeDefined();
|
||||||
expect(Number(secondMyHeader)).not.toBeNaN();
|
expect(Number(secondMyHeader)).not.toBeNaN();
|
||||||
|
|||||||
@ -16,7 +16,7 @@ export function mockAxios(
|
|||||||
axios.defaults.adapter = async (config) => {
|
axios.defaults.adapter = async (config) => {
|
||||||
await 0; // Jumps to next tick of nodejs event loop
|
await 0; // Jumps to next tick of nodejs event loop
|
||||||
|
|
||||||
const should304 =
|
const should304: unknown =
|
||||||
config.headers?.[Header.IfNoneMatch] || config.headers?.[Header.IfModifiedSince];
|
config.headers?.[Header.IfNoneMatch] || config.headers?.[Header.IfModifiedSince];
|
||||||
const status = should304 ? 304 : 200;
|
const status = should304 ? 304 : 200;
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||||
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||||
import type { CachedStorageValue } from '../../src/storage/types';
|
import type { CachedStorageValue } from '../../src/storage/types';
|
||||||
import { testCachePredicate } from '../../src/util/cache-predicate';
|
import { testCachePredicate } from '../../src/util/cache-predicate';
|
||||||
import { mockAxios } from '../mocks/axios';
|
import { mockAxios } from '../mocks/axios';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user