Typescript: add 'lastResponse' to return types #990
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.
r? @ctrudeau-stripe @jlomas-stripe
cc @remi-stripe @stripe/api-libraries
Fixes #983
Approach:
In codegen, all the API responses that correspond to single resources now return e.g.
Stripe.Response<Customer>
instead ofCustomer
. What isStripe.Response
?As far as I can reason, intersection types are subtypes of their constituents, so this should be non-breaking and safe.
List responses are a little trickier. I didn't change anything in the codegen, because each method definition (e.g.
stripe.accounts.listExternalAccounts
has a type likeso instead I redefined ApiListPromise to
extends Promise<Stripe.Response<ApiList<T>>>
instead ofextends Promise<ApiList<T>>
I was also a bit concerned about the "intersection type of union types". In theory,
Response<BankAccount | Card>
should be the same asResponse<BankAccount> | Response<Card>
, and so when a user does a refinement (i.e. they doif (res.object === 'card') {...}), inside their if statement,
resshould be a
Response`. I checked it and in practice this seems to work.Testing strategy:
Added property accesses to the typescriptTest.js for lists and a non-list response.