Skip to content

Commit fb69d6f

Browse files
committed
fix: result type sometimes incorrrectly an union of types
1 parent 8eb553a commit fb69d6f

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

src/runtime/fetchTypes.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type UntypedUseLazyFetchOptions<
4848
UntypedOptionsToReplaceWithTypedOptions | 'lazy'
4949
>;
5050

51+
type LazyFetchOption = Pick<UseFetchOptions<void>, 'lazy'>;
52+
5153
type HTTPMethod =
5254
| 'get'
5355
| 'head'
@@ -67,11 +69,6 @@ type GetSupportedHttpMethods<PathInfo extends PlainObject> = {
6769
: never;
6870
}[keyof PathInfo];
6971

70-
type GetMethodOptions<PathInfo extends PlainObject> =
71-
'get' extends GetSupportedHttpMethods<PathInfo>
72-
? GetSupportedHttpMethods<PathInfo> | undefined
73-
: GetSupportedHttpMethods<PathInfo>;
74-
7572
type Get2xxReponses<Operation> = Operation extends { responses: {} }
7673
? {
7774
[key in keyof Operation['responses']]: key extends string | number
@@ -131,15 +128,26 @@ type GetHeaders<Operation> = Operation extends {
131128
: { headers?: Operation['parameters']['header'] & PlainObject }
132129
: { headers?: PlainObject };
133130

134-
type GetMethodProp<Methods, Method> = 'get' extends Methods
131+
type GetMethodProp<Methods extends string> = 'get' extends Methods
135132
? {
136133
// method is optional when u can do get
137-
method?: Method extends string ? Uppercase<Method> | Method : Method;
134+
method?: Methods | Uppercase<Methods>;
138135
}
139136
: {
140-
method: Method extends string ? Uppercase<Method> | Method : Method;
137+
method: Methods | Uppercase<Methods>;
141138
};
142139

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+
143151
export type SimplifiedFetchOptions = FetchOptions & {
144152
pathParams?: Record<string, string | number>;
145153
};
@@ -151,9 +159,10 @@ export type SimplifiedUseFetchOptions = UseFetchOptions<void> & {
151159
export type Fetch<Paths extends Record<string, any>> = <
152160
Path extends keyof Paths,
153161
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],
157166
Body extends GetBody<Operation>,
158167
PathParams extends GetPathParams<Operation>,
159168
Query extends GetQueryParams<Operation>,
@@ -163,19 +172,19 @@ export type Fetch<Paths extends Record<string, any>> = <
163172
path: Path,
164173
// see: https://stackoverflow.com/a/78720068/11463241
165174
...config: HasRequiredProperties<
166-
Headers & Query & PathParams & Body & GetMethodProp<MethodOptions, Method>
175+
Headers & Query & PathParams & Body & MethodProp
167176
> extends true
168177
? [
169178
config: UntypedFetchOptions &
170-
GetMethodProp<MethodOptions, Method> &
179+
MethodProp &
171180
Body &
172181
PathParams &
173182
Query &
174183
Headers,
175184
]
176185
: [
177186
config?: UntypedFetchOptions &
178-
GetMethodProp<MethodOptions, Method> &
187+
MethodProp &
179188
Body &
180189
PathParams &
181190
Query &
@@ -211,9 +220,10 @@ export type UseFetch<
211220
> = <
212221
Path extends keyof Paths,
213222
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],
217227
Body extends GetBody<Operation>,
218228
PathParams extends GetPathParams<Operation>,
219229
Query extends GetQueryParams<Operation>,
@@ -225,7 +235,7 @@ export type UseFetch<
225235
>(
226236
request: Ref<Path> | Path | (() => Path),
227237
...opts: HasRequiredProperties<
228-
Headers & Query & PathParams & Body & GetMethodProp<MethodOptions, Method>
238+
Headers & Query & PathParams & Body & MethodProp
229239
> extends true
230240
? [
231241
opts: UntypedUseLazyFetchOptions<
@@ -234,14 +244,8 @@ export type UseFetch<
234244
PickKeys,
235245
DefaultT
236246
> &
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>,
245249
]
246250
: [
247251
opts?: UntypedUseLazyFetchOptions<
@@ -250,14 +254,8 @@ export type UseFetch<
250254
PickKeys,
251255
DefaultT
252256
> &
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>,
261259
]
262260
) => AsyncData<
263261
PickFrom<Response, PickKeys> | DefaultT,

0 commit comments

Comments
 (0)