|
3 | 3 | // SPDX-License-Identifier: Apache-2.0
|
4 | 4 |
|
5 | 5 | import type { IotaClient } from '@iota/iota-sdk/client';
|
6 |
| -import type { UseQueryOptions, UseQueryResult } from '@tanstack/react-query'; |
7 |
| -import { useQuery } from '@tanstack/react-query'; |
| 6 | +import type { |
| 7 | + UndefinedInitialDataOptions, |
| 8 | + UseQueryOptions, |
| 9 | + UseQueryResult, |
| 10 | +} from '@tanstack/react-query'; |
| 11 | +import { queryOptions, useQuery, useSuspenseQuery } from '@tanstack/react-query'; |
| 12 | +import { useMemo } from 'react'; |
8 | 13 |
|
9 | 14 | import type { PartialBy } from '../types/utilityTypes.js';
|
10 | 15 | import { useIotaClientContext } from './useIotaClient.js';
|
@@ -38,6 +43,34 @@ export type UseIotaClientQueryOptions<T extends keyof IotaRpcMethods, TData> = P
|
38 | 43 | 'queryKey'
|
39 | 44 | >;
|
40 | 45 |
|
| 46 | +export type GetIotaClientQueryOptions<T extends keyof IotaRpcMethods> = { |
| 47 | + client: IotaClient; |
| 48 | + network: string; |
| 49 | + method: T; |
| 50 | + options?: PartialBy< |
| 51 | + Omit<UndefinedInitialDataOptions<IotaRpcMethods[T]['result']>, 'queryFn'>, |
| 52 | + 'queryKey' |
| 53 | + >; |
| 54 | +} & (undefined extends IotaRpcMethods[T]['params'] |
| 55 | + ? { params?: IotaRpcMethods[T]['params'] } |
| 56 | + : { params: IotaRpcMethods[T]['params'] }); |
| 57 | + |
| 58 | +export function getIotaClientQuery<T extends keyof IotaRpcMethods>({ |
| 59 | + client, |
| 60 | + network, |
| 61 | + method, |
| 62 | + params, |
| 63 | + options, |
| 64 | +}: GetIotaClientQueryOptions<T>) { |
| 65 | + return queryOptions<IotaRpcMethods[T]['result']>({ |
| 66 | + ...options, |
| 67 | + queryKey: [network, method, params], |
| 68 | + queryFn: async () => { |
| 69 | + return await client[method](params as never); |
| 70 | + }, |
| 71 | + }); |
| 72 | +} |
| 73 | + |
41 | 74 | export function useIotaClientQuery<
|
42 | 75 | T extends keyof IotaRpcMethods,
|
43 | 76 | TData = IotaRpcMethods[T]['result'],
|
@@ -72,3 +105,40 @@ export function useIotaClientQuery<
|
72 | 105 | },
|
73 | 106 | });
|
74 | 107 | }
|
| 108 | + |
| 109 | +export function useIotaClientSuspenseQuery< |
| 110 | + T extends keyof IotaRpcMethods, |
| 111 | + TData = IotaRpcMethods[T]['result'], |
| 112 | +>( |
| 113 | + ...args: undefined extends IotaRpcMethods[T]['params'] |
| 114 | + ? [ |
| 115 | + method: T, |
| 116 | + params?: IotaRpcMethods[T]['params'], |
| 117 | + options?: UndefinedInitialDataOptions<TData>, |
| 118 | + ] |
| 119 | + : [ |
| 120 | + method: T, |
| 121 | + params: IotaRpcMethods[T]['params'], |
| 122 | + options?: UndefinedInitialDataOptions<TData>, |
| 123 | + ] |
| 124 | +) { |
| 125 | + const [method, params, options = {}] = args as [ |
| 126 | + method: T, |
| 127 | + params?: IotaRpcMethods[T]['params'], |
| 128 | + options?: UndefinedInitialDataOptions<TData>, |
| 129 | + ]; |
| 130 | + |
| 131 | + const iotaContext = useIotaClientContext(); |
| 132 | + |
| 133 | + const query = useMemo(() => { |
| 134 | + return getIotaClientQuery<T>({ |
| 135 | + client: iotaContext.client, |
| 136 | + network: iotaContext.network, |
| 137 | + method, |
| 138 | + params, |
| 139 | + options, |
| 140 | + }); |
| 141 | + }, [iotaContext.network, method, params, options]); |
| 142 | + |
| 143 | + return useSuspenseQuery(query); |
| 144 | +} |
0 commit comments