Client Retry Plugin
The Client Retry Plugin enables retrying client calls when errors occur.
Setup
Before you begin, please review the Client Context documentation.
ts
import { } from '@orpc/client/fetch'
import { , ClientRetryPluginContext } from '@orpc/client/plugins'
interface ORPCClientContext extends ClientRetryPluginContext {}
const = new <ORPCClientContext>({
: 'http://localhost:3000/rpc',
: [
new ({
: { // Optional override for default options
: ({ }) => {
if (.('.') === 'planet.list') {
return 2
}
return 0
}
},
}),
],
})
const : <typeof , ORPCClientContext> = ()INFO
The link can be any supported oRPC link, such as RPCLink, OpenAPILink, or custom implementations.
Usage
ts
const = await ..({ : 10 }, {
: {
: 3, // Maximum retry attempts
: 2000, // Delay between retries in ms
: => true, // Determines whether to retry based on the error
: () => {
// Hook executed on each retry
return () => {
// Execute after the retry is complete
}
},
}
})INFO
By default, retries are disabled unless a retry count is explicitly set.
- retry: Maximum retry attempts before throwing an error (default:
0). - retryDelay: Delay between retries (default:
(o) => o.lastEventRetry ?? 2000). - shouldRetry: Function that determines whether to retry (default:
true).
Event Iterator (SSE)
To replicate the behavior of EventSource for Event Iterator, use the following configuration:
ts
const streaming = await client.streaming('the input', {
context: {
retry: Number.POSITIVE_INFINITY,
}
})
for await (const message of streaming) {
console.log(message)
}