|
1 | 1 | import type { |
2 | 2 | ErrorResponse, |
3 | | - SuccessResponse, |
4 | 3 | FilterKeys, |
| 4 | + HasRequiredKeys, |
| 5 | + HttpMethod, |
5 | 6 | MediaType, |
| 7 | + OperationRequestBodyContent, |
6 | 8 | PathsWithMethod, |
7 | 9 | ResponseObjectMap, |
8 | | - OperationRequestBodyContent, |
9 | | - HasRequiredKeys, |
| 10 | + SuccessResponse, |
10 | 11 | } from "openapi-typescript-helpers"; |
11 | 12 |
|
12 | 13 | // Note: though "any" is considered bad practice in general, this library relies |
@@ -80,7 +81,7 @@ type BodyType<T = unknown> = { |
80 | 81 | stream: Response["body"]; |
81 | 82 | }; |
82 | 83 | export type ParseAs = keyof BodyType; |
83 | | -export type ParseAsResponse<T, O extends FetchOptions> = O extends { |
| 84 | +export type ParseAsResponse<T, O> = O extends { |
84 | 85 | parseAs: ParseAs; |
85 | 86 | } |
86 | 87 | ? BodyType<T>[O["parseAs"]] |
@@ -110,13 +111,7 @@ export type RequestBodyOption<T> = |
110 | 111 | export type FetchOptions<T> = RequestOptions<T> & |
111 | 112 | Omit<RequestInit, "body" | "headers">; |
112 | 113 |
|
113 | | -/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */ |
114 | | -export type MaybeOptionalInit<P extends {}, M extends keyof P> = |
115 | | - HasRequiredKeys<FetchOptions<FilterKeys<P, M>>> extends never |
116 | | - ? [(FetchOptions<FilterKeys<P, M>> | undefined)?] |
117 | | - : [FetchOptions<FilterKeys<P, M>>]; |
118 | | - |
119 | | -export type FetchResponse<T, O extends FetchOptions> = |
| 114 | +export type FetchResponse<T, O> = |
120 | 115 | | { |
121 | 116 | data: ParseAsResponse< |
122 | 117 | FilterKeys<SuccessResponse<ResponseObjectMap<T>>, MediaType>, |
@@ -174,7 +169,19 @@ export interface Middleware { |
174 | 169 | onResponse?: typeof onResponse; |
175 | 170 | } |
176 | 171 |
|
177 | | -export type ClientMethod<Paths extends {}, M> = < |
| 172 | +/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */ |
| 173 | +export type MaybeOptionalInit< |
| 174 | + P extends Record<HttpMethod, {}>, |
| 175 | + M extends keyof P, |
| 176 | +> = |
| 177 | + HasRequiredKeys<FetchOptions<FilterKeys<P, M>>> extends never |
| 178 | + ? [(FetchOptions<FilterKeys<P, M>> | undefined)?] |
| 179 | + : [FetchOptions<FilterKeys<P, M>>]; |
| 180 | + |
| 181 | +export type ClientMethod< |
| 182 | + Paths extends Record<string, Record<HttpMethod, {}>>, |
| 183 | + M extends HttpMethod, |
| 184 | +> = < |
178 | 185 | P extends PathsWithMethod<Paths, M>, |
179 | 186 | I extends MaybeOptionalInit<Paths[P], M>, |
180 | 187 | >( |
|
0 commit comments