Timeout Middleware
Abort requests that exceed a time limit and throw a typed TimeoutError
Basic Usage
import { timeout } from '@cleverbrush/client/timeout';
const client = createClient(api, {
middlewares: [timeout({ timeout: 5000 })],
});How It Works
The middleware creates an AbortController and starts a timer. If the timer fires before the fetch completes, the request is aborted and a TimeoutError is thrown. If the caller provides their own AbortSignal, it is linked — aborting either signal cancels the request.
Handling TimeoutError
import { isTimeoutError } from '@cleverbrush/client';
try {
await client.todos.list();
} catch (err) {
if (isTimeoutError(err)) {
console.log(`Timed out after ${err.timeout}ms`);
}
}