Skip to content

Commit 75554e6

Browse files
upstream(ts-sdk): Add new router utilities to dapp-kit (#7599)
Porting commit: MystenLabs/sui@640b757
1 parent c855f8c commit 75554e6

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

.changeset/warm-parrots-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@iota/dapp-kit': patch
3+
---
4+
5+
Add `getIotaClientQuery` to get the `queryOptions` config for usage with the `QueryClient` outside of React hooks. Added `useIotaClientSuspenseQuery` to support suspense-based data fetching.

sdk/dapp-kit/src/hooks/useIotaClientQuery.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
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';
813

914
import type { PartialBy } from '../types/utilityTypes.js';
1015
import { useIotaClientContext } from './useIotaClient.js';
@@ -38,6 +43,34 @@ export type UseIotaClientQueryOptions<T extends keyof IotaRpcMethods, TData> = P
3843
'queryKey'
3944
>;
4045

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+
4174
export function useIotaClientQuery<
4275
T extends keyof IotaRpcMethods,
4376
TData = IotaRpcMethods[T]['result'],
@@ -72,3 +105,40 @@ export function useIotaClientQuery<
72105
},
73106
});
74107
}
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

Comments
 (0)