@@ -48,6 +48,8 @@ type UntypedUseLazyFetchOptions<
48
48
UntypedOptionsToReplaceWithTypedOptions | 'lazy'
49
49
> ;
50
50
51
+ type LazyFetchOption = Pick < UseFetchOptions < void > , 'lazy' > ;
52
+
51
53
type HTTPMethod =
52
54
| 'get'
53
55
| 'head'
@@ -67,11 +69,6 @@ type GetSupportedHttpMethods<PathInfo extends PlainObject> = {
67
69
: never ;
68
70
} [ keyof PathInfo ] ;
69
71
70
- type GetMethodOptions < PathInfo extends PlainObject > =
71
- 'get' extends GetSupportedHttpMethods < PathInfo >
72
- ? GetSupportedHttpMethods < PathInfo > | undefined
73
- : GetSupportedHttpMethods < PathInfo > ;
74
-
75
72
type Get2xxReponses < Operation > = Operation extends { responses : { } }
76
73
? {
77
74
[ key in keyof Operation [ 'responses' ] ] : key extends string | number
@@ -131,15 +128,26 @@ type GetHeaders<Operation> = Operation extends {
131
128
: { headers ?: Operation [ 'parameters' ] [ 'header' ] & PlainObject }
132
129
: { headers ?: PlainObject } ;
133
130
134
- type GetMethodProp < Methods , Method > = 'get' extends Methods
131
+ type GetMethodProp < Methods extends string > = 'get' extends Methods
135
132
? {
136
133
// method is optional when u can do get
137
- method ?: Method extends string ? Uppercase < Method > | Method : Method ;
134
+ method ?: Methods | Uppercase < Methods > ;
138
135
}
139
136
: {
140
- method : Method extends string ? Uppercase < Method > | Method : Method ;
137
+ method : Methods | Uppercase < Methods > ;
141
138
} ;
142
139
140
+ type GetMethod < ObjectWithMethodProp extends PlainObject > =
141
+ ObjectWithMethodProp extends { }
142
+ ? 'get'
143
+ : ObjectWithMethodProp extends {
144
+ method : infer U ;
145
+ }
146
+ ? U extends string
147
+ ? Lowercase < U >
148
+ : never
149
+ : never ;
150
+
143
151
export type SimplifiedFetchOptions = FetchOptions & {
144
152
pathParams ?: Record < string , string | number > ;
145
153
} ;
@@ -151,9 +159,10 @@ export type SimplifiedUseFetchOptions = UseFetchOptions<void> & {
151
159
export type Fetch < Paths extends Record < string , any > > = <
152
160
Path extends keyof Paths ,
153
161
PathInfo extends Paths [ Path ] ,
154
- MethodOptions extends GetMethodOptions < PathInfo > ,
155
- Method extends Extract < MethodOptions , string > ,
156
- Operation extends Paths [ Path ] [ Method ] ,
162
+ MethodOptions extends GetSupportedHttpMethods < PathInfo > ,
163
+ MethodProp extends GetMethodProp < MethodOptions > ,
164
+ Method extends GetMethod < MethodProp > ,
165
+ Operation extends PathInfo [ Method ] ,
157
166
Body extends GetBody < Operation > ,
158
167
PathParams extends GetPathParams < Operation > ,
159
168
Query extends GetQueryParams < Operation > ,
@@ -163,19 +172,19 @@ export type Fetch<Paths extends Record<string, any>> = <
163
172
path : Path ,
164
173
// see: https://stackoverflow.com/a/78720068/11463241
165
174
...config : HasRequiredProperties <
166
- Headers & Query & PathParams & Body & GetMethodProp < MethodOptions , Method >
175
+ Headers & Query & PathParams & Body & MethodProp
167
176
> extends true
168
177
? [
169
178
config : UntypedFetchOptions &
170
- GetMethodProp < MethodOptions , Method > &
179
+ MethodProp &
171
180
Body &
172
181
PathParams &
173
182
Query &
174
183
Headers ,
175
184
]
176
185
: [
177
186
config ?: UntypedFetchOptions &
178
- GetMethodProp < MethodOptions , Method > &
187
+ MethodProp &
179
188
Body &
180
189
PathParams &
181
190
Query &
@@ -211,9 +220,10 @@ export type UseFetch<
211
220
> = <
212
221
Path extends keyof Paths ,
213
222
PathInfo extends Paths [ Path ] ,
214
- MethodOptions extends GetMethodOptions < PathInfo > ,
215
- Method extends Extract < MethodOptions , string > ,
216
- Operation extends Paths [ Path ] [ Method ] ,
223
+ MethodOptions extends GetSupportedHttpMethods < PathInfo > ,
224
+ MethodProp extends GetMethodProp < MethodOptions > ,
225
+ Method extends GetMethod < MethodProp > ,
226
+ Operation extends PathInfo [ Method ] ,
217
227
Body extends GetBody < Operation > ,
218
228
PathParams extends GetPathParams < Operation > ,
219
229
Query extends GetQueryParams < Operation > ,
@@ -225,7 +235,7 @@ export type UseFetch<
225
235
> (
226
236
request : Ref < Path > | Path | ( ( ) => Path ) ,
227
237
...opts : HasRequiredProperties <
228
- Headers & Query & PathParams & Body & GetMethodProp < MethodOptions , Method >
238
+ Headers & Query & PathParams & Body & MethodProp
229
239
> extends true
230
240
? [
231
241
opts : UntypedUseLazyFetchOptions <
@@ -234,14 +244,8 @@ export type UseFetch<
234
244
PickKeys ,
235
245
DefaultT
236
246
> &
237
- ( Lazy extends false ? { lazy ?: boolean } : { } ) &
238
- ComputedOptions <
239
- Headers &
240
- Query &
241
- PathParams &
242
- Body &
243
- GetMethodProp < MethodOptions , Method >
244
- > ,
247
+ ( Lazy extends false ? LazyFetchOption : { } ) &
248
+ ComputedOptions < Headers & Query & PathParams & Body & MethodProp > ,
245
249
]
246
250
: [
247
251
opts ?: UntypedUseLazyFetchOptions <
@@ -250,14 +254,8 @@ export type UseFetch<
250
254
PickKeys ,
251
255
DefaultT
252
256
> &
253
- ( Lazy extends false ? { lazy ?: boolean } : { } ) &
254
- ComputedOptions <
255
- Headers &
256
- Query &
257
- PathParams &
258
- Body &
259
- GetMethodProp < MethodOptions , Method >
260
- > ,
257
+ ( Lazy extends false ? LazyFetchOption : { } ) &
258
+ ComputedOptions < Headers & Query & PathParams & Body & MethodProp > ,
261
259
]
262
260
) => AsyncData <
263
261
PickFrom < Response , PickKeys > | DefaultT ,
0 commit comments