diff --git a/src/createCacheGroup.tsx b/src/createCacheGroup.tsx index 99f109c..c662c6f 100644 --- a/src/createCacheGroup.tsx +++ b/src/createCacheGroup.tsx @@ -5,7 +5,7 @@ import { CacheGroup } from './types/CacheGroup'; import { QueryCacheBag } from './types/QueryCacheBag'; import { QueryStore } from './types/QueryStore'; -export const createCacheGroup = (customStore?: QueryStore): CacheGroup => { +export const createCacheGroup = (customStore?: QueryStore): CacheGroup => { const CacheContext = createSafeContext(); return { diff --git a/src/hooks/useQueryCache.ts b/src/hooks/useQueryCache.ts index de50196..10cb513 100644 --- a/src/hooks/useQueryCache.ts +++ b/src/hooks/useQueryCache.ts @@ -8,8 +8,8 @@ import { QueryStore } from '../types/QueryStore'; import { convertKeyToArguments } from '../utils/convertKeyToArguments'; import { QueryKeyMap } from '../utils/QueryKeyMap'; -export const useQueryCache = (customCache?: QueryStore): QueryCacheBag => { - const cache = useRef(new QueryKeyMap(customCache)); +export const useQueryCache = (customCache?: QueryStore): QueryCacheBag => { + const cache = useRef>(new QueryKeyMap(customCache)); const getValue = useCallback( (key: TKey, fetcher: Fetcher): FetchResult => { diff --git a/src/index.ts b/src/index.ts index e6e2c98..b05710a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,4 +4,5 @@ export * from './hooks/useQuery'; // Types export * from './types/CacheGroup'; +export * from './types/FetchResult'; export * from './types/QueryStore'; diff --git a/src/types/QueryStore.ts b/src/types/QueryStore.ts index ddad687..1e9516a 100644 --- a/src/types/QueryStore.ts +++ b/src/types/QueryStore.ts @@ -1,5 +1,7 @@ -export type QueryStore = { - get: (key: string) => T | undefined; - set: (key: string, value: T) => void; +import { FetchResult } from './FetchResult'; + +export type QueryStore = { + get: (key: string) => FetchResult | undefined; + set: (key: string, value: FetchResult) => void; has: (key: string) => boolean; }; diff --git a/src/utils/QueryKeyMap.ts b/src/utils/QueryKeyMap.ts index c16420f..b450d44 100644 --- a/src/utils/QueryKeyMap.ts +++ b/src/utils/QueryKeyMap.ts @@ -3,16 +3,16 @@ import { Key } from '../types/Key'; import { QueryStore } from '../types/QueryStore'; import { stringifyKey } from './stringifyKey'; -export class QueryKeyMap { - constructor(private storage: QueryStore = new Map()) {} +export class QueryKeyMap { + constructor(private storage: QueryStore = new Map>()) {} - public get = (key: Key): FetchResult => { + public get = (key: Key): FetchResult => { const stringifiedKey = stringifyKey(key); return this.storage.get(stringifiedKey) as FetchResult; }; - public set = (key: Key, value: FetchResult): void => { + public set = (key: Key, value: FetchResult): void => { const stringifiedKey = stringifyKey(key); this.storage.set(stringifiedKey, value); diff --git a/test/customQueryStorage.test.tsx b/test/customQueryStorage.test.tsx index 6e900b2..8ebc38f 100644 --- a/test/customQueryStorage.test.tsx +++ b/test/customQueryStorage.test.tsx @@ -1,13 +1,14 @@ import { render, waitFor } from '@testing-library/react'; import React, { Suspense } from 'react'; import { createCacheGroup, QueryStore, useQuery } from '../src'; +import { FetchResult } from '../src/types/FetchResult'; import '@testing-library/jest-dom'; -const object: Record = {}; +const object: Record> = {}; -const globalQueryStore: QueryStore = { - get: (key: string) => { - return object[key] as T; +const globalQueryStore: QueryStore = { + get: (key: string) => { + return object[key]; }, set: (key, value) => { object[key] = value;