@@ -69,21 +69,29 @@ type GetSupportedHttpMethods<PathInfo extends PlainObject> = {
69
69
: never ;
70
70
} [ keyof PathInfo ] ;
71
71
72
+ /* eslint-disable @typescript-eslint/no-invalid-void-type -- this is about a response type, so void is valid */
72
73
type Get2xxReponses < Operation > = Operation extends { responses : { } }
73
74
? {
74
75
[ key in keyof Operation [ 'responses' ] ] : key extends string | number
75
76
? `${key } ` extends `2${string } `
76
- ? Operation [ 'responses' ] [ key ] extends {
77
- content : {
78
- 'application/json' : { } ;
79
- } ;
80
- }
81
- ? Operation [ 'responses' ] [ key ] [ 'content' ] [ 'application/json' ]
82
- : never
77
+ ? Operation [ 'responses' ] [ key ] extends never | undefined | null
78
+ ? void
79
+ : Operation [ 'responses' ] [ key ] extends {
80
+ content ?: null | undefined | never ;
81
+ }
82
+ ? void
83
+ : Operation [ 'responses' ] [ key ] extends {
84
+ content : {
85
+ 'application/json' : { } ;
86
+ } ;
87
+ }
88
+ ? Operation [ 'responses' ] [ key ] [ 'content' ] [ 'application/json' ]
89
+ : unknown
83
90
: never
84
91
: never ;
85
92
} [ keyof Operation [ 'responses' ] ]
86
93
: unknown ;
94
+ /* eslint-enable @typescript-eslint/no-invalid-void-type */
87
95
88
96
type GetBody < Operation > = Operation extends {
89
97
requestBody : {
@@ -131,10 +139,10 @@ type GetHeaders<Operation> = Operation extends {
131
139
type GetMethodProp < Methods , Method > = 'get' extends Methods
132
140
? {
133
141
// method is optional when u can do get
134
- method ?: Method extends string ? Uppercase < Method > | Method : Method ;
142
+ method ?: Method ;
135
143
}
136
144
: {
137
- method : Method extends string ? Uppercase < Method > | Method : Method ;
145
+ method : Method ;
138
146
} ;
139
147
140
148
export type SimplifiedFetchOptions = FetchOptions & {
@@ -148,9 +156,15 @@ export type SimplifiedUseFetchOptions = UseFetchOptions<void> & {
148
156
export type Fetch < Paths extends Record < string , any > > = <
149
157
Path extends keyof Paths ,
150
158
PathInfo extends Paths [ Path ] ,
159
+ // credit to nuxt-open-fetch for the complex method generics.
151
160
MethodOptions extends GetSupportedHttpMethods < PathInfo > ,
152
- Method extends Extract < MethodOptions , string > ,
153
- Operation extends Paths [ Path ] [ Method ] ,
161
+ MethodLiteral extends MethodOptions | Uppercase < MethodOptions > ,
162
+ Method extends Lowercase < MethodLiteral > extends MethodOptions
163
+ ? Lowercase < MethodLiteral >
164
+ : MethodOptions ,
165
+ // use get when method is not specified
166
+ ResolvedMethod extends 'get' extends Method ? 'get' : Method ,
167
+ Operation extends PathInfo [ ResolvedMethod ] ,
154
168
Body extends GetBody < Operation > ,
155
169
PathParams extends GetPathParams < Operation > ,
156
170
Query extends GetQueryParams < Operation > ,
@@ -160,19 +174,23 @@ export type Fetch<Paths extends Record<string, any>> = <
160
174
path : Path ,
161
175
// see: https://stackoverflow.com/a/78720068/11463241
162
176
...config : HasRequiredProperties <
163
- Headers & Query & PathParams & Body & GetMethodProp < MethodOptions , Method >
177
+ Headers &
178
+ Query &
179
+ PathParams &
180
+ Body &
181
+ GetMethodProp < MethodOptions , MethodLiteral >
164
182
> extends true
165
183
? [
166
184
config : UntypedFetchOptions &
167
- GetMethodProp < MethodOptions , Method > &
185
+ GetMethodProp < MethodOptions , MethodLiteral > &
168
186
Body &
169
187
PathParams &
170
188
Query &
171
189
Headers ,
172
190
]
173
191
: [
174
192
config ?: UntypedFetchOptions &
175
- GetMethodProp < MethodOptions , Method > &
193
+ GetMethodProp < MethodOptions , MethodLiteral > &
176
194
Body &
177
195
PathParams &
178
196
Query &
@@ -208,9 +226,15 @@ export type UseFetch<
208
226
> = <
209
227
Path extends keyof Paths ,
210
228
PathInfo extends Paths [ Path ] ,
229
+ // credit to nuxt-open-fetch for the complex method generics.
211
230
MethodOptions extends GetSupportedHttpMethods < PathInfo > ,
212
- Method extends Extract < MethodOptions , string > ,
213
- Operation extends Paths [ Path ] [ Method ] ,
231
+ MethodLiteral extends MethodOptions | Uppercase < MethodOptions > ,
232
+ Method extends Lowercase < MethodLiteral > extends MethodOptions
233
+ ? Lowercase < MethodLiteral >
234
+ : MethodOptions ,
235
+ // use get when method is not specified
236
+ ResolvedMethod extends 'get' extends Method ? 'get' : Method ,
237
+ Operation extends PathInfo [ ResolvedMethod ] ,
214
238
Body extends GetBody < Operation > ,
215
239
PathParams extends GetPathParams < Operation > ,
216
240
Query extends GetQueryParams < Operation > ,
0 commit comments