fix: eslint warnings

This commit is contained in:
arthurfiorette 2023-02-02 20:29:21 -03:00
parent eddff579c5
commit a5ebad3d14
No known key found for this signature in database
GPG Key ID: 9D190CD53C53C555
7 changed files with 24 additions and 11 deletions

View File

@ -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();

View File

@ -42,15 +42,19 @@ export function updateStaleRequest<D>(
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();
}

View File

@ -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<R = unknown, D = unknown>(
@ -24,6 +25,9 @@ export async function testCachePredicate<R = unknown, D = unknown>(
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]
))
) {

View File

@ -22,7 +22,10 @@ export type CachePredicateObject<R = unknown, D = unknown> = {
*
* ### 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. */
responseMatch?: (res: CacheAxiosResponse<R, D>) => MaybePromise<boolean>;

View File

@ -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();

View File

@ -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;

View File

@ -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';