Skip to content

Commit

Permalink
refactor: create generic createGetQueryData
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Oct 8, 2024
1 parent 2038e57 commit 4b69aae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
7 changes: 3 additions & 4 deletions apps/lend/src/entities/token/lib/query-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { tokenKeys } from '../model'
import { queryClient } from '@/shared/api/query-client'
import { TokenKey } from '@/entities/token'
import { createGetQueryData } from '@/shared/lib/queries'

export const getTokenQueryData = <Result, Key extends TokenKey = TokenKey>(key: TokenKey, keyData: Parameters<typeof tokenKeys[Key]>[0]) =>
queryClient.getQueryData<Result>(tokenKeys[key](keyData))

export const getTokenQueryData = createGetQueryData(tokenKeys)
19 changes: 12 additions & 7 deletions packages/curve-lib/src/shared/lib/queries/factory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { useQuery, QueryKey } from '@tanstack/react-query'
import type { UseQueryOptions } from '@tanstack/react-query'
import { QueryKey, useQuery } from '@tanstack/react-query'
import { queryClient } from '@/shared/api/query-client'

export function createQueryHook<TParams, TData, TQueryKey extends QueryKey>(
export const createQueryHook = <TParams, TData, TQueryKey extends QueryKey>(
getQueryOptions: (params: TParams, condition?: boolean) => UseQueryOptions<TData, Error, TData, TQueryKey>,
) {
return (params: TParams, condition?: boolean) => {
const options = getQueryOptions(params, condition)
return useQuery<TData, Error, TData, TQueryKey>(options)
}
) => (params: TParams, condition?: boolean) => {
const options = getQueryOptions(params, condition)
return useQuery<TData, Error, TData, TQueryKey>(options)
}

export const createGetQueryData = <KeysObject extends Record<keyof KeysObject & string, (arg: any) => any>>(
keys: KeysObject
) =>
<Result, Key extends keyof KeysObject = keyof KeysObject>(key: Key, keyData: Parameters<KeysObject[Key]>[0]) =>
queryClient.getQueryData<Result>(keys[key](keyData))

0 comments on commit 4b69aae

Please sign in to comment.