WIP Add axios error response type with status discriminator #3029
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.
This PR is based on the discussion in #495
I tried to implement a proof-of-concept that allows type narrowing for errors based on the status response.
In my project I am using Axios with Tanstack Query, so I am using these plugins.
With these changes, the error type is created as discriminated union with the status code as the discriminator.
Then
AxiosErroris wrapped with a custom type to be status aware as well.Then the type of the error in the Tanstack Query mutation can be narrowed based on the status:
With this approach, the extension could be added in a backwards compatible way; since no existing types need to be changed, only the new discriminated union type needs to be added.
This PR is obviously not ready yet. I was looking for this functionality and thought I give it a try if I can add it.
Would love to hear feedback and add the missing bits to get this PR in a good shape.