From e728f9b04fa7fec8a6abf7922b309a1f665b90e6 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 5 Nov 2024 09:27:31 +0100 Subject: [PATCH] types(cache): resolved value is not nullable (#2848) --- src/runtime/internal/cache.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/runtime/internal/cache.ts b/src/runtime/internal/cache.ts index b278f5f71e..8d1bc93f51 100644 --- a/src/runtime/internal/cache.ts +++ b/src/runtime/internal/cache.ts @@ -31,10 +31,12 @@ function defaultCacheOptions() { } as const; } +type ResolvedCacheEntry = CacheEntry & { value: T }; + export function defineCachedFunction( fn: (...args: ArgsT) => T | Promise, opts: CacheOptions = {} -): (...args: ArgsT) => Promise { +): (...args: ArgsT) => Promise { opts = { ...defaultCacheOptions(), ...opts }; const pending: { [key: string]: Promise } = {}; @@ -50,7 +52,7 @@ export function defineCachedFunction( resolver: () => T | Promise, shouldInvalidateCache?: boolean, event?: H3Event - ): Promise> { + ): Promise> { // Use extension for key to avoid conflicting with parent namespace (foo/bar and foo/bar/baz) const cacheKey = [opts.base, group, name, key + ".json"] .filter(Boolean) @@ -148,10 +150,10 @@ export function defineCachedFunction( console.error(`[nitro] [cache] SWR handler error.`, error); useNitroApp().captureError(error, { event, tags: ["cache"] }); }); - return entry; + return entry as ResolvedCacheEntry; } - return _resolvePromise.then(() => entry); + return _resolvePromise.then(() => entry) as Promise>; } return async (...args) => {