Allow supplying a retry handler to generated API clients #271
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 could be something implemented outside of a client, but it would be nice to have some kind of retry facility within the client itself.
This stemmed from an issue encountered trying to integrate a generated client with the
remix-auth-oauth2
package on Node 19+ over http. Node 19 introduced a change to the default http agent which set thekeepAlive
setting to true, meaning that connections will be re-used by default compared to Node 18.In the case of our custom OAuth2 strategy,
remix-auth-oauth2
makes its own request to the token endpoint with theConnection: Close
header, after which we immediately make a request to the same server via the generated client. Since the agent has been configured to re-use connection, we seem to be reusing the socket that has yet to been closed on the client side resulting in the server issuing a reset since it has closed the connection. The generated client does not handle errors from the initialfetch
request that is made, so this bubbles up to the application.This PR introduces an optional retry handler that wraps both
fetch
and response errors, allowing API clients to be constructed with customized retry logic based on their needs.An example of how this may be used in this use case is: