Skip to content

[feat]: parseAs options reports correct data type #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 6, 2023
30 changes: 21 additions & 9 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export type QuerySerializer<T> = (
export type BodySerializer<T> = (body: OperationRequestBodyContent<T>) => any;

export type ParseAs = "json" | "text" | "blob" | "arrayBuffer" | "stream";
export type ParseAsResponse<T, K> = K extends ParseAs
? {
json: T;
text: Awaited<ReturnType<Response["text"]>>;
blob: Awaited<ReturnType<Response["blob"]>>;
arrayBuffer: Awaited<ReturnType<Response["arrayBuffer"]>>;
stream: Response["body"];
}[K]
: T;

export interface DefaultParamsOption {
params?: {
Expand All @@ -62,9 +71,12 @@ export type RequestBodyOption<T> = OperationRequestBodyContent<T> extends never

export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, "body">;

export type FetchResponse<T> =
export type FetchResponse<T, O extends FetchOptions<any>> =
| {
data: FilterKeys<SuccessResponse<ResponseObjectMap<T>>, MediaType>;
data: ParseAsResponse<
FilterKeys<SuccessResponse<ResponseObjectMap<T>>, MediaType>,
O["parseAs"]
>;
error?: never;
response: Response;
}
Expand All @@ -86,13 +98,12 @@ export default function createClient<Paths extends {}>(
clientOptions?: ClientOptions,
): {
/** Call a GET endpoint */
GET<P extends PathsWithMethod<Paths, "get">>(
GET<
P extends PathsWithMethod<Paths, "get">,
O extends FetchOptions<FilterKeys<Paths[P], "get">> = {},
>(
url: P,
...init: HasRequiredKeys<
FetchOptions<FilterKeys<Paths[P], "get">>
> extends never
? [(FetchOptions<FilterKeys<Paths[P], "get">> | undefined)?]
: [FetchOptions<FilterKeys<Paths[P], "get">>]
...init: HasRequiredKeys<O> extends never ? [O?] : [O]
): Promise<
FetchResponse<
"get" extends infer T
Expand All @@ -101,7 +112,8 @@ export default function createClient<Paths extends {}>(
? Paths[P][T]
: unknown
: never
: never
: never,
O
>
>;
/** Call a PUT endpoint */
Expand Down