Skip to content

Commit

Permalink
types(cache): resolved value is not nullable (#2848)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Nov 5, 2024
1 parent 1cc357f commit e728f9b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/runtime/internal/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ function defaultCacheOptions() {
} as const;
}

type ResolvedCacheEntry<T> = CacheEntry<T> & { value: T };

export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
fn: (...args: ArgsT) => T | Promise<T>,
opts: CacheOptions<T, ArgsT> = {}
): (...args: ArgsT) => Promise<T | undefined> {
): (...args: ArgsT) => Promise<T> {
opts = { ...defaultCacheOptions(), ...opts };

const pending: { [key: string]: Promise<T> } = {};
Expand All @@ -50,7 +52,7 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
resolver: () => T | Promise<T>,
shouldInvalidateCache?: boolean,
event?: H3Event
): Promise<CacheEntry<T>> {
): Promise<ResolvedCacheEntry<T>> {
// 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)
Expand Down Expand Up @@ -148,10 +150,10 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
console.error(`[nitro] [cache] SWR handler error.`, error);
useNitroApp().captureError(error, { event, tags: ["cache"] });
});
return entry;
return entry as ResolvedCacheEntry<T>;
}

return _resolvePromise.then(() => entry);
return _resolvePromise.then(() => entry) as Promise<ResolvedCacheEntry<T>>;
}

return async (...args) => {
Expand Down

0 comments on commit e728f9b

Please sign in to comment.