|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
1 | 2 | import {
|
2 | 3 | type Store,
|
3 | 4 | type FactoryConfig,
|
4 | 5 | type FactoryStore,
|
5 | 6 | } from 'cache-manager';
|
6 |
| -import {type ArgumentPaths, type AnyFunction} from './paths.d'; |
7 | 7 |
|
| 8 | +/** |
| 9 | + * Represents a type that can either be a single value of type T or an array of type T. |
| 10 | + * |
| 11 | + * @typeParam T - The type of the value(s) that can be stored in the SingleOrArray. |
| 12 | + */ |
| 13 | +export type SingleOrArray<T> = T | T[]; |
| 14 | + |
| 15 | +/** |
| 16 | + * Represents any function that takes any number of arguments and returns any value. |
| 17 | + */ |
| 18 | +export type CacheableFunction = ((...arguments_: any[]) => any) & { |
| 19 | + cacheOptions?: CachedFunctionOptions<any>; |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Represents the type for generating paths of a given object. |
| 24 | + * |
| 25 | + * @template T - The type of the object. |
| 26 | + * type Paths<T> = T extends Record<string, unknown> ? {[K in keyof T]: |
| 27 | + * `${Exclude<K, symbol>}${'' | `.${Paths<T[K]>}`}` |
| 28 | + * }[keyof T] : never; |
| 29 | +*/ |
| 30 | +// TODO: Implement the Paths type. |
| 31 | +export type Paths<T> = string; // eslint-disable-line @typescript-eslint/no-unused-vars |
| 32 | + |
| 33 | +/** |
| 34 | + * Represents the paths of the arguments of a given function type. |
| 35 | + * @template F - The function type. |
| 36 | + * @typeparam F - The function type. |
| 37 | + * @typeparam Paths - The paths of the arguments of the function type. |
| 38 | + */ |
| 39 | +export type ArgumentPaths<F extends CacheableFunction> = SingleOrArray<Paths<Parameters<F>>>; |
| 40 | + |
| 41 | +/** |
| 42 | + * Represents the options for initializing a cached function. |
| 43 | + */ |
8 | 44 | export type CachedFunctionInitializerOptions =
|
9 | 45 | {store: FactoryStore; config: FactoryConfig} |
|
10 | 46 | {store: Store};
|
11 | 47 |
|
12 |
| -export type CachedFunctionOptions<F extends AnyFunction> = Partial<CachedFunctionInitializerOptions> & { |
| 48 | +/** |
| 49 | + * Options for a cached function. |
| 50 | + * |
| 51 | + * @template F - The type of the cacheable function. |
| 52 | + * @property {Partial<CachedFunctionInitializerOptions>} [options] - Additional options for the cached function. |
| 53 | + * @property {ArgumentPaths<F>} [selector] - The selector for the cached function. |
| 54 | + * @property {number} [ttl] - The time-to-live (TTL) for the cached function. |
| 55 | + * @property {boolean} [force] - Whether to force the execution of the cached function. |
| 56 | + */ |
| 57 | +export type CachedFunctionOptions<F extends CacheableFunction> = Partial<CachedFunctionInitializerOptions> & { |
13 | 58 | selector?: ArgumentPaths<F>;
|
14 | 59 | ttl?: number;
|
15 | 60 | force?: boolean;
|
|
0 commit comments