55 type UseQueryResult ,
66 type UseSuspenseQueryOptions ,
77 type UseSuspenseQueryResult ,
8+ type QueryClient ,
89 useMutation ,
910 useQuery ,
1011 useSuspenseQuery ,
@@ -21,9 +22,9 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
2122> (
2223 method : Method ,
2324 url : Path ,
24- ...[ init , options ] : HasRequiredKeys < Init > extends never
25- ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?]
26- : [ Init & { [ key : string ] : unknown } , Options ?]
25+ ...[ init , options , queryClient ] : HasRequiredKeys < Init > extends never
26+ ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?, QueryClient ? ]
27+ : [ Init & { [ key : string ] : unknown } , Options ?, QueryClient ? ]
2728) => UseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
2829
2930export type UseSuspenseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
@@ -35,9 +36,9 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
3536> (
3637 method : Method ,
3738 url : Path ,
38- ...[ init , options ] : HasRequiredKeys < Init > extends never
39- ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?]
40- : [ Init & { [ key : string ] : unknown } , Options ?]
39+ ...[ init , options , queryClient ] : HasRequiredKeys < Init > extends never
40+ ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?, QueryClient ? ]
41+ : [ Init & { [ key : string ] : unknown } , Options ?, QueryClient ? ]
4142) => UseSuspenseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
4243
4344export type UseMutationMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
@@ -50,6 +51,7 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
5051 method : Method ,
5152 url : Path ,
5253 options ?: Options ,
54+ queryClient ?: QueryClient ,
5355) => UseMutationResult < Response [ "data" ] , Response [ "error" ] , Init > ;
5456
5557export interface OpenapiQueryClient < Paths extends { } , Media extends MediaType = MediaType > {
@@ -64,51 +66,60 @@ export default function createClient<Paths extends {}, Media extends MediaType =
6466 client : FetchClient < Paths , Media > ,
6567) : OpenapiQueryClient < Paths , Media > {
6668 return {
67- useQuery : ( method , path , ...[ init , options ] ) => {
68- return useQuery ( {
69- queryKey : [ method , path , init ] ,
70- queryFn : async ( ) => {
71- const mth = method . toUpperCase ( ) as keyof typeof client ;
72- const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
73- const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
74- if ( error || ! data ) {
75- throw error ;
76- }
77- return data ;
69+ useQuery : ( method , path , ...[ init , options , queryClient ] ) => {
70+ return useQuery (
71+ {
72+ queryKey : [ method , path , init ] ,
73+ queryFn : async ( ) => {
74+ const mth = method . toUpperCase ( ) as keyof typeof client ;
75+ const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
76+ const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
77+ if ( error || ! data ) {
78+ throw error ;
79+ }
80+ return data ;
81+ } ,
82+ ...options ,
7883 } ,
79- ... options ,
80- } ) ;
84+ queryClient ,
85+ ) ;
8186 } ,
82- useSuspenseQuery : ( method , path , ...[ init , options ] ) => {
83- return useSuspenseQuery ( {
84- queryKey : [ method , path , init ] ,
85- queryFn : async ( ) => {
86- const mth = method . toUpperCase ( ) as keyof typeof client ;
87- const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
88- const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
89- if ( error || ! data ) {
90- throw error ;
91- }
92- return data ;
87+ useSuspenseQuery : ( method , path , ...[ init , options , queryClient ] ) => {
88+ return useSuspenseQuery (
89+ {
90+ queryKey : [ method , path , init ] ,
91+ queryFn : async ( ) => {
92+ const mth = method . toUpperCase ( ) as keyof typeof client ;
93+ const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
94+ const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
95+ if ( error || ! data ) {
96+ throw error ;
97+ }
98+ return data ;
99+ } ,
100+ ...options ,
93101 } ,
94- ... options ,
95- } ) ;
102+ queryClient ,
103+ ) ;
96104 } ,
97- useMutation : ( method , path , options ) => {
98- return useMutation ( {
99- mutationKey : [ method , path ] ,
100- mutationFn : async ( init ) => {
101- // TODO: Put in external fn for reusability
102- const mth = method . toUpperCase ( ) as keyof typeof client ;
103- const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
104- const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
105- if ( error || ! data ) {
106- throw error ;
107- }
108- return data ;
105+ useMutation : ( method , path , options , queryClient ) => {
106+ return useMutation (
107+ {
108+ mutationKey : [ method , path ] ,
109+ mutationFn : async ( init ) => {
110+ // TODO: Put in external fn for reusability
111+ const mth = method . toUpperCase ( ) as keyof typeof client ;
112+ const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
113+ const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
114+ if ( error || ! data ) {
115+ throw error ;
116+ }
117+ return data ;
118+ } ,
119+ ...options ,
109120 } ,
110- ... options ,
111- } ) ;
121+ queryClient ,
122+ ) ;
112123 } ,
113124 } ;
114125}
0 commit comments