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 { RPCLink } from '@orpc/client/fetch'
import { ClientRetryPlugin, ClientRetryPluginContext } from '@orpc/client/plugins'
interface ORPCClientContext extends ClientRetryPluginContext {}
const link = new RPCLink<ORPCClientContext>({
url: 'http://localhost:3000/rpc',
plugins: [
new ClientRetryPlugin({
default: {}, // Override the default context if needed
}),
],
})
const client: RouterClient<typeof router, ORPCClientContext> = createORPCClient(link)
Usage
ts
const planets = await client.planet.list({ limit: 10 }, {
context: {
retry: 3, // Maximum retry attempts
retryDelay: 2000, // Delay between retries in ms
shouldRetry: options => true, // Determines whether to retry based on the error
onRetry: (options) => {}, // Hook executed on each retry
}
})
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)
}