From a5ebad3d142f858c291d7ddf0b453f80180b146d Mon Sep 17 00:00:00 2001 From: arthurfiorette Date: Thu, 2 Feb 2023 20:29:21 -0300 Subject: [PATCH] fix: eslint warnings --- src/header/interpreter.ts | 6 +++--- src/interceptors/util.ts | 10 +++++++--- src/util/cache-predicate.ts | 6 +++++- src/util/types.ts | 5 ++++- test/interceptors/last-modified.test.ts | 4 ++-- test/mocks/axios.ts | 2 +- test/util/cache-predicate.test.ts | 2 ++ 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/header/interpreter.ts b/src/header/interpreter.ts index 34f6e96..977b826 100644 --- a/src/header/interpreter.ts +++ b/src/header/interpreter.ts @@ -5,7 +5,7 @@ import type { HeadersInterpreter } from './types'; export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => { if (!headers) return 'not enough headers'; - const cacheControl = headers[Header.CacheControl]; + const cacheControl: unknown = headers[Header.CacheControl]; if (cacheControl) { const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse( @@ -29,7 +29,7 @@ export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => { } if (maxAge !== undefined) { - const age = headers[Header.Age]; + const age: unknown = headers[Header.Age]; if (!age) { 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) { const milliseconds = Date.parse(String(expires)) - Date.now(); diff --git a/src/interceptors/util.ts b/src/interceptors/util.ts index 69f5cd5..4d20572 100644 --- a/src/interceptors/util.ts +++ b/src/interceptors/util.ts @@ -42,15 +42,19 @@ export function updateStaleRequest( const { etag, modifiedSince } = config.cache; if (etag) { - const etagValue = etag === true ? cache.data?.headers[Header.ETag] : etag; - etagValue && (config.headers[Header.IfNoneMatch] = etagValue); + const etagValue = + etag === true ? (cache.data?.headers[Header.ETag] as unknown) : etag; + + if (etagValue) { + config.headers[Header.IfNoneMatch] = etagValue; + } } if (modifiedSince) { config.headers[Header.IfModifiedSince] = modifiedSince === true ? // 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() : modifiedSince.toUTCString(); } diff --git a/src/util/cache-predicate.ts b/src/util/cache-predicate.ts index 21be5fb..bf59300 100644 --- a/src/util/cache-predicate.ts +++ b/src/util/cache-predicate.ts @@ -1,5 +1,6 @@ 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}. */ export async function testCachePredicate( @@ -24,6 +25,9 @@ export async function testCachePredicate( if ( !(await predicate( // 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] )) ) { diff --git a/src/util/types.ts b/src/util/types.ts index 5f43456..8a55fac 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -22,7 +22,10 @@ export type CachePredicateObject = { * * ### Remember, all axios headers are lowercase. */ - containsHeaders?: Record MaybePromise>; + containsHeaders?: Record< + string, + (header?: CacheAxiosResponse['headers'][string]) => MaybePromise + >; /** Check if the response matches this predicate. */ responseMatch?: (res: CacheAxiosResponse) => MaybePromise; diff --git a/test/interceptors/last-modified.test.ts b/test/interceptors/last-modified.test.ts index 2301307..0692e2e 100644 --- a/test/interceptors/last-modified.test.ts +++ b/test/interceptors/last-modified.test.ts @@ -104,7 +104,7 @@ describe('Last-Modified handling', () => { // 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 firstMyHeader = firstResponse.headers?.[XMockRandom]; + const firstMyHeader: unknown = firstResponse.headers?.[XMockRandom]; expect(firstMyHeader).toBeDefined(); expect(Number(firstMyHeader)).not.toBeNaN(); @@ -113,7 +113,7 @@ describe('Last-Modified handling', () => { const secondResponse = await axios.get('http://test.com', { cache: { modifiedSince: true } }); - const secondMyHeader = secondResponse.headers?.[XMockRandom]; + const secondMyHeader: unknown = secondResponse.headers?.[XMockRandom]; expect(secondMyHeader).toBeDefined(); expect(Number(secondMyHeader)).not.toBeNaN(); diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index 8d1ebfe..90f935c 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -16,7 +16,7 @@ export function mockAxios( axios.defaults.adapter = async (config) => { await 0; // Jumps to next tick of nodejs event loop - const should304 = + const should304: unknown = config.headers?.[Header.IfNoneMatch] || config.headers?.[Header.IfModifiedSince]; const status = should304 ? 304 : 200; diff --git a/test/util/cache-predicate.test.ts b/test/util/cache-predicate.test.ts index 47f777d..2bb910e 100644 --- a/test/util/cache-predicate.test.ts +++ b/test/util/cache-predicate.test.ts @@ -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 { testCachePredicate } from '../../src/util/cache-predicate'; import { mockAxios } from '../mocks/axios';