From 0ca2dba89cfc28ba3748004dacf58c587842c30c Mon Sep 17 00:00:00 2001 From: ctot-nondef Date: Tue, 22 Oct 2024 17:02:18 +0200 Subject: [PATCH] fix: cache response body instead of full response --- composables/use-api-client.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/composables/use-api-client.ts b/composables/use-api-client.ts index 1c43dc9..e3f086d 100644 --- a/composables/use-api-client.ts +++ b/composables/use-api-client.ts @@ -16,7 +16,7 @@ function basicSecurityWorker(securityData: userPass | null): RequestParams | und return undefined; } -const cache: Map> = new Map>(); +const cache: Map> = new Map>(); async function fetchWithETag( input: globalThis.Request | URL | string, @@ -41,13 +41,15 @@ async function fetchWithETag( if (response.status === 304) { if (cachedETag && Object.keys(cachedETag).length === 1) { - const response = Object.values(cachedETag)[0]; - if (response) return response.clone(); + const body = Object.values(cachedETag)[0]; + if (body) return new Response(JSON.stringify(body), { status: 200 }); } throw new Error(`Cache error!`); } else if (response.ok) { if (currentETag) { - cache.set(url, { [currentETag]: response.clone() }); + // response body can't be typed at this point + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + cache.set(url, { [currentETag]: await response.clone().json() }); } return response; } else {