Skip to content

Commit 4a7b6cc

Browse files
committed
fix: wrong overload resolution
1 parent 7c7ac00 commit 4a7b6cc

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

src/runtime/fetchTypes.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,29 @@ type GetSupportedHttpMethods<PathInfo extends PlainObject> = {
6969
: never;
7070
}[keyof PathInfo];
7171

72+
/* eslint-disable @typescript-eslint/no-invalid-void-type -- this is about a response type, so void is valid */
7273
type Get2xxReponses<Operation> = Operation extends { responses: {} }
7374
? {
7475
[key in keyof Operation['responses']]: key extends string | number
7576
? `${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
8390
: never
8491
: never;
8592
}[keyof Operation['responses']]
8693
: unknown;
94+
/* eslint-enable @typescript-eslint/no-invalid-void-type */
8795

8896
type GetBody<Operation> = Operation extends {
8997
requestBody: {
@@ -131,10 +139,10 @@ type GetHeaders<Operation> = Operation extends {
131139
type GetMethodProp<Methods, Method> = 'get' extends Methods
132140
? {
133141
// method is optional when u can do get
134-
method?: Method extends string ? Uppercase<Method> | Method : Method;
142+
method?: Method;
135143
}
136144
: {
137-
method: Method extends string ? Uppercase<Method> | Method : Method;
145+
method: Method;
138146
};
139147

140148
export type SimplifiedFetchOptions = FetchOptions & {
@@ -148,9 +156,15 @@ export type SimplifiedUseFetchOptions = UseFetchOptions<void> & {
148156
export type Fetch<Paths extends Record<string, any>> = <
149157
Path extends keyof Paths,
150158
PathInfo extends Paths[Path],
159+
// credit to nuxt-open-fetch for the complex method generics.
151160
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],
154168
Body extends GetBody<Operation>,
155169
PathParams extends GetPathParams<Operation>,
156170
Query extends GetQueryParams<Operation>,
@@ -160,19 +174,23 @@ export type Fetch<Paths extends Record<string, any>> = <
160174
path: Path,
161175
// see: https://stackoverflow.com/a/78720068/11463241
162176
...config: HasRequiredProperties<
163-
Headers & Query & PathParams & Body & GetMethodProp<MethodOptions, Method>
177+
Headers &
178+
Query &
179+
PathParams &
180+
Body &
181+
GetMethodProp<MethodOptions, MethodLiteral>
164182
> extends true
165183
? [
166184
config: UntypedFetchOptions &
167-
GetMethodProp<MethodOptions, Method> &
185+
GetMethodProp<MethodOptions, MethodLiteral> &
168186
Body &
169187
PathParams &
170188
Query &
171189
Headers,
172190
]
173191
: [
174192
config?: UntypedFetchOptions &
175-
GetMethodProp<MethodOptions, Method> &
193+
GetMethodProp<MethodOptions, MethodLiteral> &
176194
Body &
177195
PathParams &
178196
Query &
@@ -208,9 +226,15 @@ export type UseFetch<
208226
> = <
209227
Path extends keyof Paths,
210228
PathInfo extends Paths[Path],
229+
// credit to nuxt-open-fetch for the complex method generics.
211230
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],
214238
Body extends GetBody<Operation>,
215239
PathParams extends GetPathParams<Operation>,
216240
Query extends GetQueryParams<Operation>,

0 commit comments

Comments
 (0)