-
-
Notifications
You must be signed in to change notification settings - Fork 528
[openapi-fetch] Support parseAs type inference #1442
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
Conversation
|
Deploying with
|
Latest commit: |
30f9383
|
Status: | ✅ Deploy successful! |
Preview URL: | https://163dd43f.openapi-ts.pages.dev |
Branch Preview URL: | https://feat-parse-as.openapi-ts.pages.dev |
@@ -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 extends ParseAs> = K extends "text" | |||
? Awaited<ReturnType<Response["text"]>> | |||
: K extends "blob" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The object was much cleaner, however, I was not able to get that working.
This is messier, but it does have the advantage of always falling back to the original type T
whereas the cleaner object could return unknown
: never | ||
: never | ||
Paths[P]["get"], | ||
I[0]["parseAs"] extends ParseAs ? I[O]["parseAs"] : "json" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like there’s a way to avoid this, but I couldn’t think of a way. I needed this in my version for TS to fall back to json
if init?.parseAs
was omitted
): Promise< | ||
FetchResponse< | ||
"get" extends infer T | ||
? T extends "get" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was autogenerated TypeScript defs, not something I think is necessary. I cleaned this up to be more readable, and at least in the tests I didn’t notice any regressions
Changes
Adds parseAs inference; continues from #1428
How to Review
Checklist
docs/
updated (if necessary)pnpm run update:examples
run (only applicable for openapi-typescript)