Fix types

This commit is contained in:
SukkaW 2024-10-09 21:10:25 +08:00
parent 3e7ef5a294
commit 99e5d46531
3 changed files with 13 additions and 11 deletions

View File

@ -214,7 +214,7 @@ export class Cache<S = string> {
fn: (resp: Response) => Promise<T>,
opt: Omit<CacheApplyOption<T, S>, 'incrementTtlWhenHit'>,
requestInit?: RequestInit
) {
): Promise<T> {
if (opt.temporaryBypass) {
return fn(await fetchWithRetry(url, requestInit ?? defaultRequestInit));
}
@ -323,12 +323,12 @@ export class Cache<S = string> {
this.set(getETagKey(primaryUrl), r.headers.get('etag')!, TTL.ONE_WEEK_STATIC);
// If we do not have a cached value, we ignore 304
if (r.status === 304 && previouslyCached != null) {
if (r.status === 304 && typeof previouslyCached === 'string') {
controller.abort();
throw new Custom304NotModifiedError(primaryUrl);
throw new Custom304NotModifiedError(primaryUrl, previouslyCached);
}
} else if (!primaryETag && previouslyCached) {
throw new CustomNoETagFallbackError(previouslyCached as string);
} else if (!primaryETag && typeof previouslyCached === 'string') {
throw new CustomNoETagFallbackError(previouslyCached);
}
return r.text();
@ -369,13 +369,13 @@ export class Cache<S = string> {
this.set(getETagKey(url), res.headers.get('etag')!, TTL.ONE_WEEK_STATIC);
// If we do not have a cached value, we ignore 304
if (res.status === 304 && previouslyCached != null) {
if (res.status === 304 && typeof previouslyCached === 'string') {
controller.abort();
throw new Custom304NotModifiedError(url);
throw new Custom304NotModifiedError(url, previouslyCached);
}
} else if (!primaryETag && previouslyCached) {
} else if (!primaryETag && typeof previouslyCached === 'string') {
controller.abort();
throw new CustomNoETagFallbackError(previouslyCached as string);
throw new CustomNoETagFallbackError(previouslyCached);
}
const text = await res.text();

View File

@ -12,7 +12,7 @@ export class Custom304NotModifiedError extends Error {
public readonly name = 'Custom304NotModifiedError';
public readonly digest = 'Custom304NotModifiedError';
constructor(public readonly url: string) {
constructor(public readonly url: string, public readonly data: string) {
super('304 Not Modified');
}
}

View File

@ -107,7 +107,9 @@ function createFetchRetry($fetch: typeof fetch): FetchWithRetry {
}
console.log(picocolors.gray('[fetch fail]'), url, err);
throw err;
const newErr = new Error('Fetch failed');
newErr.cause = err;
throw newErr;
}
}, retryOpts);
} catch (err) {