diff --git a/exfetch.ts b/exfetch.ts index 11ad869..79a1789 100644 --- a/exfetch.ts +++ b/exfetch.ts @@ -288,46 +288,6 @@ export interface ExFetchOptions { */ userAgent?: string; } -/** - * @access private - */ -interface ExFetchRequestCacheCondition { - allowRestore: boolean; - allowSave: boolean; - useFirst: boolean; -} -/** - * Assert the request whether is able to cache. - * @access private - * @param {string | URL} input - * @param {Parameters[1]} init - * @returns {ExFetchRequestCacheCondition} - */ -function assertRequestCacheCondition(input: string | URL, init?: Parameters[1]): ExFetchRequestCacheCondition { - const common: boolean = new URL(input).protocol === "https:" && ( - typeof init === "undefined" || - typeof init.method === "undefined" || - init.method.toUpperCase() === "GET" - ); - let allowRestore: boolean = common; - let allowSave: boolean = common; - let useFirst = false; - switch (init?.cache) { - case "force-cache": - useFirst = true; - break; - case "no-store": - allowRestore = false; - allowSave = false; - break; - case "reload": - allowRestore = false; - break; - default: - break; - } - return { allowRestore, allowSave, useFirst }; -} /** * Resolve delay options. * @access private @@ -589,7 +549,12 @@ export class ExFetch { if (new URL(input).protocol === "file:") { return fetch(input, init); } - const requestCacheCondition: ExFetchRequestCacheCondition = assertRequestCacheCondition(input, init); + const requestCacheOption: RequestCache | undefined = init?.cache; + const requestCacheCommonCondition: boolean = new URL(input).protocol === "https:" && ( + typeof init === "undefined" || + typeof init.method === "undefined" || + init.method.toUpperCase() === "GET" + ); const requestFuzzy: Request = new Request(input, { ...init, cache: undefined, @@ -602,11 +567,11 @@ export class ExFetch { window: undefined }); let responseCached: Response | undefined = undefined; - if (requestCacheCondition.allowRestore) { + if (requestCacheCommonCondition && requestCacheOption !== "no-store" && requestCacheOption !== "reload") { await this.#cacheStorageLoad(); responseCached = await this.#cacheStorage?.match(requestFuzzy); } - if (requestCacheCondition.useFirst && typeof responseCached !== "undefined") { + if (requestCacheOption === "force-cache" && typeof responseCached !== "undefined") { return responseCached; } const responseCachedETag: string | undefined = responseCached?.headers.get("ETag") ?? undefined; @@ -675,7 +640,7 @@ export class ExFetch { await setDelay(delayTime, requestSignal); retries += 1; } while (retries <= this.#retry.maximum); - if (requestCacheCondition.allowSave && response.ok && response.status >= 200 && response.status < 300 && ( + if (requestCacheCommonCondition && requestCacheOption !== "no-store" && response.ok && response.status >= 200 && response.status < 300 && ( response.headers.has("ETag") || response.headers.has("Last-Modified") )) {