drwpow/openapi-fetch

How to handle undefined?

darr1s opened this issue · 3 comments

Description

Coming from openapi-generator/typescript-fetch, this library seems to wrap all response data with possibly undefined.

Reproduction

In latest v0.1.0,

image

Expected result

Unsure, TBD

Checklist

See the README—it gives instructions. It’s only | undefined because you may have an error. as soon as you handle the error, data will be defined.

Also check your OpenAPI schema—most people don’t realize that by default, OpenAPI marks all properties as optional. Be sure your schema has required: true specified on the response and on the top-level object.

Thanks, it does seems like we do not need to use try {} catch {}.

For those who doesn't know, as long as we do

const { data, error } = apiClient.get(...)
if (error) throw Error
return data

Then data will not be undefined.

That’s one way of dealing with it! This is my opinion, but personally I wouldn’t throw, especially because the error object has a useful message returned from your API on what went wrong. It’s probably better to show that to the user than simply throwing an exception they won’t be able to act upon 🙂