Skip to content

Commit f820891

Browse files
committed
feat: Refactor cachedFunction to use selectorToCacheKey
1 parent 9c37dcd commit f820891

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

src/index.d.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import {
23
type Store,
34
type FactoryConfig,
45
type FactoryStore,
56
} from 'cache-manager';
6-
import {type ArgumentPaths, type AnyFunction} from './paths.d';
77

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+
*/
844
export type CachedFunctionInitializerOptions =
945
{store: FactoryStore; config: FactoryConfig} |
1046
{store: Store};
1147

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> & {
1358
selector?: ArgumentPaths<F>;
1459
ttl?: number;
1560
force?: boolean;

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import _ from 'lodash';
55
import {
66
type Cache, caching, type Store,
77
} from 'cache-manager';
8-
import type {CachedFunctionInitializerOptions, CachedFunctionOptions} from './index.d';
9-
import type {AnyFunction as CacheableFunction, ArgumentPaths} from './paths.d';
8+
import {
9+
type CachedFunctionInitializerOptions, type CachedFunctionOptions, type CacheableFunction, type ArgumentPaths,
10+
} from './index.d';
1011

1112
let cache: Cache | undefined;
1213

src/paths.d.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)