diff --git a/src/@types/global.ts b/src/@types/global.ts index eb0f0e9..e49a0fd 100644 --- a/src/@types/global.ts +++ b/src/@types/global.ts @@ -79,7 +79,7 @@ export type APIProcessingHook = (params: I /** * Represents the cache key for an API endpoint. It can be a string param key, an array of param keys, or a function that generates the key from params. */ -export type CacheKey = keyof TArgs | Array | ((params?: TArgs) => string); +export type CacheKey = keyof Required> | Array>> | ((params?: Partial) => string); /** * Represents the additional cache key argument that can be added to a `cacheKey` getter function. @@ -106,7 +106,7 @@ export interface IHookBaseConfig>> extends IHookBaseConfig { /** The cache key to store the response against, it can be a string param key, an array of param keys, or a function that generates the key from params. */ - cacheKey?: CacheKey>>; + cacheKey?: CacheKey>; /** Additional config to send to SWR (like settings or fallback data for SSR) */ swrConfig?: SWRConfiguration; /** If this property is false, the query fetch will wait until it becomes true or undefined. Useful for holding back queries until conditions are met */ diff --git a/src/hooks/useInfiniteQuery.ts b/src/hooks/useInfiniteQuery.ts index 3dc633b..c815507 100644 --- a/src/hooks/useInfiniteQuery.ts +++ b/src/hooks/useInfiniteQuery.ts @@ -60,7 +60,7 @@ export const useInfiniteQuery = < const cacheKeyValue = React.useCallback( (index: number, prevData?: any) => { const finalParams = hookConfig?.params?.(index, prevData); - return [readCacheKey>>(endpointId, hookConfig?.cacheKey, finalParams), finalParams]; + return [readCacheKey>(endpointId, hookConfig?.cacheKey, finalParams), finalParams]; }, [hookConfig?.cacheKey, hookConfig?.params, endpointId] ); diff --git a/src/hooks/useQuery.ts b/src/hooks/useQuery.ts index 2dc5041..229d354 100644 --- a/src/hooks/useQuery.ts +++ b/src/hooks/useQuery.ts @@ -57,7 +57,7 @@ export const useQuery = ) => Promise>>(endpointId, hookConfig?.cacheKey, hookConfig?.params); + return readCacheKey>(endpointId, hookConfig?.cacheKey, hookConfig?.params); }, [hookConfig?.cacheKey, hookConfig?.params, hookConfig?.waitFor, endpointId]); /** Protect the root fetcher from causing dependency changes in SWR */ diff --git a/src/utils/caching.spec.ts b/src/utils/caching.spec.ts index 0c4ff1e..c99a348 100644 --- a/src/utils/caching.spec.ts +++ b/src/utils/caching.spec.ts @@ -40,7 +40,7 @@ describe('readCacheKey', () => { }); it('should return cache key built when executed with a function', () => { - const cacheKey = (params?: { id: number; name: string }) => `${params?.id}-${params?.name}`; + const cacheKey = (params?: { id?: number; name?: string }) => `${params?.id}-${params?.name}`; const result = readCacheKey('endpoint', cacheKey, { id: 123, name: 'test' }); expect(result).toEqual('endpoint.123-test'); }); diff --git a/src/utils/caching.ts b/src/utils/caching.ts index 68325b2..a99757e 100644 --- a/src/utils/caching.ts +++ b/src/utils/caching.ts @@ -23,7 +23,7 @@ export const cacheKeyConcat = (...args: Array): string => { * @param {TArgs} [params] - The parameters for the API call. * @returns {string|undefined} - The final concatenated cache key, or undefined if the cache key params are not all present. */ -export const readCacheKey = (endpointId?: string, cacheKey?: CacheKey, params?: TArgs): string | undefined => { +export const readCacheKey = (endpointId?: string, cacheKey?: CacheKey, params?: Partial): string | undefined => { switch (typeof cacheKey) { case 'function': const funcResult = cacheKey(params); @@ -32,14 +32,14 @@ export const readCacheKey = (endpointId?: string, cacheKey?: CacheKey `${(params?.[key] as string | undefined) ?? ''}`).filter((key) => !!key); + const lookupResults = cacheKey.map((key) => `${(params?.[key as string] as string | undefined) ?? ''}`).filter((key) => !!key); return cacheKeyConcat(endpointId, ...lookupResults); } return undefined;