-
-
Notifications
You must be signed in to change notification settings - Fork 529
Add MethodResponse utility type #1831
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
Add MethodResponse utility type #1831
Conversation
Thank you! You can add a Thanks for such a detailed writeup! You made a great argument for adding this utility, and this is a great addition. |
🦋 Changeset detectedLatest commit: d51ed16 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 |
Why not use async function getData(): Promise<SuccessResponse<paths['/data']['get']>> |
@piousdeer |
Changes
Adds a new
MethodResponse
utility type toopenapi-fetch
to easily reference the return type of a fetch call in your codeHow to Review
Understanding the rationale
There are many times in a codebase where you do something like this:
The return type of this function is based solely on the
data
endpoint result, so it would be nice to make this relation explicitly clearBefore this PR
The easiest way to do this is the following code, is the following code
This has a problem though: if
client
isn't statically known as a global variable. In that case, you cannot get around this by just doingClient<paths>['GET']<'/data', {}>
as this is not valid typescriptAfter this PR
This is the new code which is both shorter, and also works well even if
typeof client
is not possible and a type variable is used in its placeWhy not just use schemas?
Another option is to use
components['schemas']['GetDataResponse']
, which is also valid and just a matter of preference. I think being able to explicitly specify the connection between a method and the response is more flexible than thisFor example, you could create a wrapper on the type I added to this library (which I don't think you can easily do before this PR), by doing something like
Additionally, this
components['schemas']
option isn't always guaranteed to work. For example, there are cases where the schema is partially inlined into theoperations
object:As you can see in this example, you cannot just use
GetDataResponse
directly because it's missing the fact it's actually contained inside adataSummary
objectCode
This is just a utility type, so there is no functionality change
Checklist
docs/
updated (if necessary)pnpm run update:examples
run (only applicable for openapi-typescript)