5
5
type UseQueryResult ,
6
6
type UseSuspenseQueryOptions ,
7
7
type UseSuspenseQueryResult ,
8
+ type QueryClient ,
8
9
useMutation ,
9
10
useQuery ,
10
11
useSuspenseQuery ,
@@ -21,9 +22,9 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
21
22
> (
22
23
method : Method ,
23
24
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 ? ]
27
28
) => UseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
28
29
29
30
export type UseSuspenseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
@@ -35,9 +36,9 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
35
36
> (
36
37
method : Method ,
37
38
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 ? ]
41
42
) => UseSuspenseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
42
43
43
44
export type UseMutationMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
@@ -50,6 +51,7 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
50
51
method : Method ,
51
52
url : Path ,
52
53
options ?: Options ,
54
+ queryClient ?: QueryClient ,
53
55
) => UseMutationResult < Response [ "data" ] , Response [ "error" ] , Init > ;
54
56
55
57
export interface OpenapiQueryClient < Paths extends { } , Media extends MediaType = MediaType > {
@@ -64,51 +66,60 @@ export default function createClient<Paths extends {}, Media extends MediaType =
64
66
client : FetchClient < Paths , Media > ,
65
67
) : OpenapiQueryClient < Paths , Media > {
66
68
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 ,
78
83
} ,
79
- ... options ,
80
- } ) ;
84
+ queryClient ,
85
+ ) ;
81
86
} ,
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 ,
93
101
} ,
94
- ... options ,
95
- } ) ;
102
+ queryClient ,
103
+ ) ;
96
104
} ,
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 ,
109
120
} ,
110
- ... options ,
111
- } ) ;
121
+ queryClient ,
122
+ ) ;
112
123
} ,
113
124
} ;
114
125
}
0 commit comments