Add MethodResponse utility type #1831
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)