-
-
Notifications
You must be signed in to change notification settings - Fork 485
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
fix(#1977): refactor handling for empty responses #1986
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 0345a28 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
I think this is a breaking change, but one most seem to be in favor of. Love the direction of this so far! Please let us know if you need any assistance with passing tests, or have any questions. |
Looking at #1970, I have been thinking about the "parse-as" problem a bit as well. I'm wondering if it would make sense to make the parsing lazier. One issue I see with the current API is that the caller cannot look at the status code to decide on how to parse the response. I'm not sure this is very ergonomic. For example, it is very possible that an API returns a binary stream on 200, but a JSON response on other error codes. In this case, being able to specify One way to work around this problem is to provide accessors: const resp = client.GET("/thing");
if (resp.status == 200) {
resp.stream()
} else {
resp.json()
} This is somewhat inspired by requests. I realize
However, I fear if we go down the "pass parseAs parameter path", we'll get more and more complicated options (e.g. parseAs per response code, etc.). |
@gzm0 I completely agree— This is related to some longer conversations we maintainers have had where some part of openapi-fetch’s runtime is code-generated from peoples’ specs—that would remove the need for parseAs in exchange for bigger client weight and slightly-more memory. That’s of course a completely separate conversation, but not entirely unrelated. I do think that decision of “does openapi-fetch continue on its current path of requiring zero codegen beyond openapi-typescript” will likely affect the decisions on the |
Agreed. FWIW: Have we considered simply returning the That would allow something like: const response = await client.GET("/path");
if (!response.ok) { // type narrowing on both `ok` and `status` might be tricky but probably necessary for ergonomics.
throw new Error("foo");
}
const data = await response.json(); While all the lower-level body retrieval methods of |
That was one of the earliest decisions I made to just save hundreds of |
Yeah, that's fair.
Fair enough. So it really seems this warrants wider discussion. |
Changes
This PR includes a couple of different changes for
openapi-fetch
:data
anderror
are the same to reduce complexity.As an example, this means that whenever a user uses the
parseAs: 'stream'
both thedata
anderror
property will return aReadableStream
fromresponse.body
204
status to determine if a response is emptyresponse.body
is null''
) get converted toundefined
after failing to parse it asjson
(fixesUnexpected end of JSON input
with empty response #1977)How to Review
See changes & tests
Checklist
docs/
updated (if necessary)pnpm run update:examples
run (only applicable for openapi-typescript)