Skip to content

Commit 4ae16ee

Browse files
committed
fix: return type for non-get api not working
1 parent 9073f88 commit 4ae16ee

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

src/runtime/fetchTypes.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,19 @@ type HTTPMethod =
6060
| 'options'
6161
| 'trace';
6262

63-
type GetSupportedHttpMethod<PathInfo extends PlainObject> = {
63+
type GetSupportedHttpMethods<PathInfo extends PlainObject> = {
6464
[key in keyof PathInfo]: PathInfo[key] extends {}
6565
? key extends HTTPMethod
6666
? key
6767
: never
6868
: never;
6969
}[keyof PathInfo];
7070

71+
type GetMethodOptions<PathInfo extends PlainObject> =
72+
'get' extends GetSupportedHttpMethods<PathInfo>
73+
? GetSupportedHttpMethods<PathInfo> | undefined
74+
: GetSupportedHttpMethods<PathInfo>;
75+
7176
type Get2xxReponses<Operation> = Operation extends { responses: {} }
7277
? {
7378
[key in keyof Operation['responses']]: key extends string | number
@@ -149,37 +154,33 @@ export type SimplifiedUseFetchOptions = UseFetchOptions<void> & {
149154
};
150155

151156
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>,
166167
>(
167168
path: Path,
168169
// see: https://stackoverflow.com/a/78720068/11463241
169170
...config: HasRequiredProperties<
170-
Headers & Query & PathParams & Body & MethodProp
171+
Headers & Query & PathParams & Body & GetMethodProp<MethodOptions, Method>
171172
> extends true
172173
? [
173174
config: UntypedFetchOptions &
174-
MethodProp &
175+
GetMethodProp<MethodOptions, Method> &
175176
Body &
176177
PathParams &
177178
Query &
178179
Headers,
179180
]
180181
: [
181182
config?: UntypedFetchOptions &
182-
MethodProp &
183+
GetMethodProp<MethodOptions, Method> &
183184
Body &
184185
PathParams &
185186
Query &
@@ -213,27 +214,23 @@ export type UseFetch<
213214
Paths extends Record<string, any>,
214215
Lazy extends boolean = false,
215216
> = <
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>,
230227
ErrorT = FetchError,
231228
PickKeys extends KeysOf<Response> = KeysOf<Response>,
232229
DefaultT = DefaultAsyncDataValue,
233230
>(
234231
request: Ref<Path> | Path | (() => Path),
235232
...opts: HasRequiredProperties<
236-
Headers & Query & PathParams & Body & MethodProp
233+
Headers & Query & PathParams & Body & GetMethodProp<MethodOptions, Method>
237234
> extends true
238235
? [
239236
opts: UntypedUseLazyFetchOptions<
@@ -243,11 +240,13 @@ export type UseFetch<
243240
DefaultT
244241
> &
245242
(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+
>,
251250
]
252251
: [
253252
opts?: UntypedUseLazyFetchOptions<
@@ -257,11 +256,13 @@ export type UseFetch<
257256
DefaultT
258257
> &
259258
(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+
>,
265266
]
266267
) => AsyncData<
267268
PickFrom<Response, PickKeys> | DefaultT,

0 commit comments

Comments
 (0)