Skip to content

[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

Closed
wants to merge 4 commits into from
Closed

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Nov 18, 2023

Changes

Adds parseAs inference; continues from #1428

How to Review

  • N/A

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

Copy link

changeset-bot bot commented Nov 18, 2023

⚠️ No Changeset found

Latest commit: 30f9383

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

Deploying with  Cloudflare Pages  Cloudflare Pages

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

View logs

@@ -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"
Copy link
Contributor Author

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"
Copy link
Contributor Author

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"
Copy link
Contributor Author

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

@drwpow drwpow changed the title Feat/parse as Add parseAs type inference Nov 18, 2023
@drwpow drwpow changed the title Add parseAs type inference [openapi-fetch] Support parseAs type inference Nov 18, 2023
@drwpow drwpow closed this Dec 6, 2023
@drwpow drwpow deleted the feat/parse-as branch February 15, 2024 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants