Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve type definitions for CacheOptions #2700

Merged
merged 4 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime/internal/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function defaultCacheOptions() {

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

Expand Down
20 changes: 10 additions & 10 deletions src/types/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export interface CacheEntry<T = any> {
integrity?: string;
}

export interface CacheOptions<T = any> {
export interface CacheOptions<T = any, ArgsT extends unknown[] = any[]> {
name?: string;
getKey?: (...args: any[]) => string | Promise<string>;
transform?: (entry: CacheEntry<T>, ...args: any[]) => any;
validate?: (entry: CacheEntry<T>) => boolean;
shouldInvalidateCache?: (...args: any[]) => boolean | Promise<boolean>;
shouldBypassCache?: (...args: any[]) => boolean | Promise<boolean>;
getKey?: (...args: ArgsT) => string | Promise<string>;
transform?: (entry: CacheEntry<T>, ...args: ArgsT) => any;
validate?: (entry: CacheEntry<T>, ...args: ArgsT) => boolean;
shouldInvalidateCache?: (...args: ArgsT) => boolean | Promise<boolean>;
shouldBypassCache?: (...args: ArgsT) => boolean | Promise<boolean>;
group?: string;
integrity?: any;
/**
Expand All @@ -32,10 +32,10 @@ export interface ResponseCacheEntry<T = any> {
}

export interface CachedEventHandlerOptions<T = any>
extends Omit<CacheOptions<ResponseCacheEntry<T>>, "transform" | "validate"> {
shouldInvalidateCache?: (event: H3Event) => boolean | Promise<boolean>;
shouldBypassCache?: (event: H3Event) => boolean | Promise<boolean>;
getKey?: (event: H3Event) => string | Promise<string>;
extends Omit<
CacheOptions<ResponseCacheEntry<T>, [H3Event]>,
"transform" | "validate"
> {
headersOnly?: boolean;
varies?: string[] | readonly string[];
}