From 0b31340564c06f68f9063899a59cbc31ee97264d Mon Sep 17 00:00:00 2001 From: Daniel Haarhoff Date: Fri, 17 Jan 2025 11:38:51 +0000 Subject: [PATCH] Hide cache internals from client Refs: #2186 --- src/HttpCache.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/HttpCache.ts b/src/HttpCache.ts index 32fd9c5c1..3df3eb5a2 100644 --- a/src/HttpCache.ts +++ b/src/HttpCache.ts @@ -6,7 +6,7 @@ interface CacheValue { response: StoredResponse } -export type CacheKey = URL +type CacheKey = string type StoredResponse = typeof StoredResponseSchema.Encoded @@ -23,16 +23,16 @@ export class HttpCache extends Context.Tag('HttpCache')< request: HttpClientRequest.HttpClientRequest, ) => Effect.Effect<{ staleAt: DateTime.DateTime; response: HttpClientResponse.HttpClientResponse } | undefined> set: (response: HttpClientResponse.HttpClientResponse, staleAt: DateTime.DateTime) => Effect.Effect - delete: (key: CacheKey) => Effect.Effect + delete: (url: URL) => Effect.Effect } >() {} export const layer = Layer.sync(HttpCache, () => { - const cache = new Map() + const cache = new Map() return { get: request => pipe( - cache.get(keyForRequest(request).href), + cache.get(keyForRequest(request)), Option.fromNullable, Option.map(({ staleAt, response }) => ({ staleAt, @@ -58,10 +58,10 @@ export const layer = Layer.sync(HttpCache, () => { }), Effect.andThen(Schema.encode(StoredResponseSchema)), Effect.andThen(storedResponse => { - cache.set(keyForRequest(response.request).href, { staleAt, response: storedResponse }) + cache.set(keyForRequest(response.request), { staleAt, response: storedResponse }) }), ), - delete: key => Effect.succeed(cache.delete(key.href)), + delete: url => Effect.succeed(cache.delete(url.href)), } }) @@ -69,5 +69,5 @@ const keyForRequest = (request: HttpClientRequest.HttpClientRequest): CacheKey = const url = new URL(request.url) url.search = UrlParams.toString(request.urlParams) - return url + return url.href }