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

Updated QueryStore type #20

Merged
merged 1 commit into from
Aug 23, 2022
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/createCacheGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>): CacheGroup => {
const CacheContext = createSafeContext<QueryCacheBag>();

return {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useQueryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryKeyMap>(new QueryKeyMap(customCache));
export const useQueryCache = (customCache?: QueryStore<unknown>): QueryCacheBag => {
const cache = useRef<QueryKeyMap<unknown>>(new QueryKeyMap(customCache));

const getValue = useCallback(
<TData, TKey extends Key>(key: TKey, fetcher: Fetcher<TData, TKey>): FetchResult<TData> => {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './hooks/useQuery';

// Types
export * from './types/CacheGroup';
export * from './types/FetchResult';
export * from './types/QueryStore';
8 changes: 5 additions & 3 deletions src/types/QueryStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export type QueryStore = {
get: <T>(key: string) => T | undefined;
set: <T>(key: string, value: T) => void;
import { FetchResult } from './FetchResult';

export type QueryStore<TData> = {
get: (key: string) => FetchResult<TData> | undefined;
set: (key: string, value: FetchResult<TData>) => void;
has: (key: string) => boolean;
};
8 changes: 4 additions & 4 deletions src/utils/QueryKeyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TData> {
constructor(private storage: QueryStore<TData> = new Map<string, FetchResult<TData>>()) {}

public get = <TData>(key: Key): FetchResult<TData> => {
public get = (key: Key): FetchResult<TData> => {
const stringifiedKey = stringifyKey(key);

return this.storage.get(stringifiedKey) as FetchResult<TData>;
};

public set = <TData>(key: Key, value: FetchResult<TData>): void => {
public set = (key: Key, value: FetchResult<TData>): void => {
const stringifiedKey = stringifyKey(key);

this.storage.set(stringifiedKey, value);
Expand Down
9 changes: 5 additions & 4 deletions test/customQueryStorage.test.tsx
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {};
const object: Record<string, FetchResult<unknown>> = {};

const globalQueryStore: QueryStore = {
get: <T,>(key: string) => {
return object[key] as T;
const globalQueryStore: QueryStore<unknown> = {
get: (key: string) => {
return object[key];
},
set: (key, value) => {
object[key] = value;
Expand Down