@@ -60,14 +60,19 @@ type HTTPMethod =
60
60
| 'options'
61
61
| 'trace' ;
62
62
63
- type GetSupportedHttpMethod < PathInfo extends PlainObject > = {
63
+ type GetSupportedHttpMethods < PathInfo extends PlainObject > = {
64
64
[ key in keyof PathInfo ] : PathInfo [ key ] extends { }
65
65
? key extends HTTPMethod
66
66
? key
67
67
: never
68
68
: never ;
69
69
} [ keyof PathInfo ] ;
70
70
71
+ type GetMethodOptions < PathInfo extends PlainObject > =
72
+ 'get' extends GetSupportedHttpMethods < PathInfo >
73
+ ? GetSupportedHttpMethods < PathInfo > | undefined
74
+ : GetSupportedHttpMethods < PathInfo > ;
75
+
71
76
type Get2xxReponses < Operation > = Operation extends { responses : { } }
72
77
? {
73
78
[ key in keyof Operation [ 'responses' ] ] : key extends string | number
@@ -149,37 +154,33 @@ export type SimplifiedUseFetchOptions = UseFetchOptions<void> & {
149
154
} ;
150
155
151
156
export type Fetch < Paths extends Record < string , any > > = <
152
- Path extends keyof Paths = keyof Paths ,
153
- Method extends GetSupportedHttpMethod <
154
- Paths ,
155
- Path
156
- > = 'get' extends GetSupportedHttpMethod < Paths , Path >
157
- ? 'get'
158
- : GetSupportedHttpMethod < Paths , Path > ,
159
- MethodProp extends GetMethodProp < Method > = GetMethodProp < Method > ,
160
- Operation extends Paths [ Path ] [ Method ] = Paths [ Path ] [ Method ] ,
161
- Body extends GetBody < Operation > = GetBody < Operation > ,
162
- PathParams extends GetPathParams < Operation > = GetPathParams < Operation > ,
163
- Query extends GetQueryParams < Operation > = GetQueryParams < Operation > ,
164
- Headers extends GetHeaders < Operation > = GetHeaders < Operation > ,
165
- Response extends Get2xxReponses < Operation > = Get2xxReponses < Operation > ,
157
+ Path extends keyof Paths ,
158
+ PathInfo extends Paths [ Path ] ,
159
+ MethodOptions extends GetMethodOptions < PathInfo > ,
160
+ Method extends Extract < MethodOptions , string > ,
161
+ Operation extends Paths [ Path ] [ Method ] ,
162
+ Body extends GetBody < Operation > ,
163
+ PathParams extends GetPathParams < Operation > ,
164
+ Query extends GetQueryParams < Operation > ,
165
+ Headers extends GetHeaders < Operation > ,
166
+ Response extends Get2xxReponses < Operation > ,
166
167
> (
167
168
path : Path ,
168
169
// see: https://stackoverflow.com/a/78720068/11463241
169
170
...config : HasRequiredProperties <
170
- Headers & Query & PathParams & Body & MethodProp
171
+ Headers & Query & PathParams & Body & GetMethodProp < MethodOptions , Method >
171
172
> extends true
172
173
? [
173
174
config : UntypedFetchOptions &
174
- MethodProp &
175
+ GetMethodProp < MethodOptions , Method > &
175
176
Body &
176
177
PathParams &
177
178
Query &
178
179
Headers ,
179
180
]
180
181
: [
181
182
config ?: UntypedFetchOptions &
182
- MethodProp &
183
+ GetMethodProp < MethodOptions , Method > &
183
184
Body &
184
185
PathParams &
185
186
Query &
@@ -213,27 +214,23 @@ export type UseFetch<
213
214
Paths extends Record < string , any > ,
214
215
Lazy extends boolean = false ,
215
216
> = <
216
- Path extends keyof Paths & string = keyof Paths & string ,
217
- Method extends GetSupportedHttpMethod <
218
- Paths ,
219
- Path
220
- > = 'get' extends GetSupportedHttpMethod < Paths , Path >
221
- ? 'get'
222
- : GetSupportedHttpMethod < Paths , Path > ,
223
- MethodProp extends GetMethodProp < Method > = GetMethodProp < Method > ,
224
- Operation extends Paths [ Path ] [ Method ] = Paths [ Path ] [ Method ] ,
225
- Body extends GetBody < Operation > = GetBody < Operation > ,
226
- PathParams extends GetPathParams < Operation > = GetPathParams < Operation > ,
227
- Query extends GetQueryParams < Operation > = GetQueryParams < Operation > ,
228
- Headers extends GetHeaders < Operation > = GetHeaders < Operation > ,
229
- Response extends Get2xxReponses < Operation > = Get2xxReponses < Operation > ,
217
+ Path extends keyof Paths ,
218
+ PathInfo extends Paths [ Path ] ,
219
+ MethodOptions extends GetMethodOptions < PathInfo > ,
220
+ Method extends Extract < MethodOptions , string > ,
221
+ Operation extends Paths [ Path ] [ Method ] ,
222
+ Body extends GetBody < Operation > ,
223
+ PathParams extends GetPathParams < Operation > ,
224
+ Query extends GetQueryParams < Operation > ,
225
+ Headers extends GetHeaders < Operation > ,
226
+ Response extends Get2xxReponses < Operation > ,
230
227
ErrorT = FetchError ,
231
228
PickKeys extends KeysOf < Response > = KeysOf < Response > ,
232
229
DefaultT = DefaultAsyncDataValue ,
233
230
> (
234
231
request : Ref < Path > | Path | ( ( ) => Path ) ,
235
232
...opts : HasRequiredProperties <
236
- Headers & Query & PathParams & Body & MethodProp
233
+ Headers & Query & PathParams & Body & GetMethodProp < MethodOptions , Method >
237
234
> extends true
238
235
? [
239
236
opts : UntypedUseLazyFetchOptions <
@@ -243,11 +240,13 @@ export type UseFetch<
243
240
DefaultT
244
241
> &
245
242
( Lazy extends false ? { lazy ?: boolean } : { } ) &
246
- ComputedOptions < MethodProp > &
247
- ComputedOptions < Body > &
248
- ComputedOptions < PathParams > &
249
- ComputedOptions < Query > &
250
- ComputedOptions < Headers > ,
243
+ ComputedOptions <
244
+ Headers &
245
+ Query &
246
+ PathParams &
247
+ Body &
248
+ GetMethodProp < MethodOptions , Method >
249
+ > ,
251
250
]
252
251
: [
253
252
opts ?: UntypedUseLazyFetchOptions <
@@ -257,11 +256,13 @@ export type UseFetch<
257
256
DefaultT
258
257
> &
259
258
( Lazy extends false ? { lazy ?: boolean } : { } ) &
260
- ComputedOptions < MethodProp > &
261
- ComputedOptions < Body > &
262
- ComputedOptions < PathParams > &
263
- ComputedOptions < Query > &
264
- ComputedOptions < Headers > ,
259
+ ComputedOptions <
260
+ Headers &
261
+ Query &
262
+ PathParams &
263
+ Body &
264
+ GetMethodProp < MethodOptions , Method >
265
+ > ,
265
266
]
266
267
) => AsyncData <
267
268
PickFrom < Response , PickKeys > | DefaultT ,
0 commit comments