1
1
import {
2
- UseMutationOptions ,
3
- UseMutationResult ,
4
- UseQueryOptions ,
5
- UseQueryResult ,
6
- UseSuspenseQueryOptions ,
7
- UseSuspenseQueryResult ,
2
+ type UseMutationOptions ,
3
+ type UseMutationResult ,
4
+ type UseQueryOptions ,
5
+ type UseQueryResult ,
6
+ type UseSuspenseQueryOptions ,
7
+ type UseSuspenseQueryResult ,
8
8
useMutation ,
9
9
useQuery ,
10
10
} from "@tanstack/react-query" ;
11
- import {
12
- ClientMethod ,
13
- FetchResponse ,
14
- MaybeOptionalInit ,
15
- OpenapiClient ,
16
- } from "openapi-fetch" ;
17
- import {
18
- HasRequiredKeys ,
19
- HttpMethod ,
20
- MediaType ,
21
- PathsWithMethod ,
22
- } from "openapi-typescript-helpers" ;
11
+ import type { ClientMethod , FetchResponse , MaybeOptionalInit , OpenapiClient } from "openapi-fetch" ;
12
+ import type { HasRequiredKeys , HttpMethod , MediaType , PathsWithMethod } from "openapi-typescript-helpers" ;
23
13
24
- export type UseQueryMethod <
25
- Paths extends Record < string , Record < HttpMethod , { } > > ,
26
- Media extends MediaType ,
27
- > = {
28
- <
29
- Method extends HttpMethod ,
30
- Path extends PathsWithMethod < Paths , Method > ,
31
- Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
32
- Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
33
- Options extends Omit <
34
- UseQueryOptions < Response [ "data" ] , Response [ "error" ] > ,
35
- "queryKey" | "queryFn"
36
- > ,
37
- > (
38
- method : Method ,
39
- url : Path ,
40
- ...[ init , options ] : HasRequiredKeys < Init > extends never
41
- ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?]
42
- : [ Init & { [ key : string ] : unknown } , Options ?]
43
- ) : UseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
44
- } ;
14
+ export type UseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
15
+ Method extends HttpMethod ,
16
+ Path extends PathsWithMethod < Paths , Method > ,
17
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
18
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
19
+ Options extends Omit < UseQueryOptions < Response [ "data" ] , Response [ "error" ] > , "queryKey" | "queryFn" > ,
20
+ > (
21
+ method : Method ,
22
+ url : Path ,
23
+ ...[ init , options ] : HasRequiredKeys < Init > extends never
24
+ ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?]
25
+ : [ Init & { [ key : string ] : unknown } , Options ?]
26
+ ) => UseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
45
27
46
- export type UseSuspenseQueryMethod <
47
- Paths extends Record < string , Record < HttpMethod , { } > > ,
48
- Media extends MediaType ,
49
- > = {
50
- <
51
- Method extends HttpMethod ,
52
- Path extends PathsWithMethod < Paths , Method > ,
53
- Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
54
- Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
55
- Options extends Omit <
56
- UseSuspenseQueryOptions < Response [ "data" ] , Response [ "error" ] > ,
57
- "queryKey" | "queryFn"
58
- > ,
59
- > (
60
- method : Method ,
61
- url : Path ,
62
- ...[ init , options ] : HasRequiredKeys < Init > extends never
63
- ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?]
64
- : [ Init & { [ key : string ] : unknown } , Options ?]
65
- ) : UseSuspenseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
66
- } ;
28
+ export type UseSuspenseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
29
+ Method extends HttpMethod ,
30
+ Path extends PathsWithMethod < Paths , Method > ,
31
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
32
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
33
+ Options extends Omit < UseSuspenseQueryOptions < Response [ "data" ] , Response [ "error" ] > , "queryKey" | "queryFn" > ,
34
+ > (
35
+ method : Method ,
36
+ url : Path ,
37
+ ...[ init , options ] : HasRequiredKeys < Init > extends never
38
+ ? [ ( Init & { [ key : string ] : unknown } ) ?, Options ?]
39
+ : [ Init & { [ key : string ] : unknown } , Options ?]
40
+ ) => UseSuspenseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
67
41
68
- export type UseMutationMethod <
69
- Paths extends Record < string , Record < HttpMethod , { } > > ,
70
- Media extends MediaType ,
71
- > = {
72
- <
73
- Method extends HttpMethod ,
74
- Path extends PathsWithMethod < Paths , Method > ,
75
- Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
76
- Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
77
- Options extends Omit <
78
- UseMutationOptions < Response [ "data" ] , Response [ "error" ] , Init > ,
79
- "mutationKey" | "mutationFn"
80
- > ,
81
- > (
82
- method : Method ,
83
- url : Path ,
84
- options ?: Options ,
85
- // TODO: Add support for Partial request options that become optional in UseMutationResult Context
86
- ) : UseMutationResult < Response [ "data" ] , Response [ "error" ] , Init > ;
87
- } ;
42
+ export type UseMutationMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
43
+ Method extends HttpMethod ,
44
+ Path extends PathsWithMethod < Paths , Method > ,
45
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
46
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
47
+ Options extends Omit < UseMutationOptions < Response [ "data" ] , Response [ "error" ] , Init > , "mutationKey" | "mutationFn" > ,
48
+ > (
49
+ method : Method ,
50
+ url : Path ,
51
+ options ?: Options ,
52
+ ) => UseMutationResult < Response [ "data" ] , Response [ "error" ] , Init > ;
88
53
89
- export interface OpenapiQueryClient <
90
- Paths extends { } ,
91
- Media extends MediaType = MediaType ,
92
- > {
54
+ export interface OpenapiQueryClient < Paths extends { } , Media extends MediaType = MediaType > {
93
55
useQuery : UseQueryMethod < Paths , Media > ;
94
56
useSuspenseQery : any ;
95
57
useMutation : UseMutationMethod < Paths , Media > ;
96
58
}
97
59
98
- export default function createClient <
99
- Paths extends { } ,
100
- Media extends MediaType = MediaType ,
101
- > ( client : OpenapiClient < Paths , Media > ) : OpenapiQueryClient < Paths , Media > {
60
+ export default function createClient < Paths extends { } , Media extends MediaType = MediaType > (
61
+ client : OpenapiClient < Paths , Media > ,
62
+ ) : OpenapiQueryClient < Paths , Media > {
102
63
return {
103
64
useQuery : ( method , path , ...[ init , options ] ) => {
104
65
return useQuery ( {
@@ -107,7 +68,9 @@ export default function createClient<
107
68
const mth = method . toUpperCase ( ) as keyof typeof client ;
108
69
const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
109
70
const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
110
- if ( error || ! data ) throw error ;
71
+ if ( error || ! data ) {
72
+ throw error ;
73
+ }
111
74
return data ;
112
75
} ,
113
76
...options ,
@@ -121,7 +84,9 @@ export default function createClient<
121
84
const mth = method . toUpperCase ( ) as keyof typeof client ;
122
85
const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
123
86
const { data, error } = await fn ( path , init as any ) ; // TODO: find a way to avoid as any
124
- if ( error || ! data ) throw error ;
87
+ if ( error || ! data ) {
88
+ throw error ;
89
+ }
125
90
return data ;
126
91
} ,
127
92
...options ,
0 commit comments