Retry configuration
Middleware function
import { createFej, createRetryMiddleware } from 'fej';
const api = createFej();
api.use('retry', createRetryMiddleware({
attempts: 3,
delay: 1000,
backoff: 'exponential',
}), 50); // Priority 50 to run before other middleware
api.use('retry', createRetryMiddleware({
attempts: 5,
delay: 500,
backoff: 'exponential',
maxDelay: 10000,
shouldRetry: (error, ctx) => {
// Only retry on network errors or 5xx status codes
return error.name === 'NetworkError' ||
(ctx.response && ctx.response.status >= 500);
},
onRetry: (error, attempt, ctx) => {
console.log(`Retry attempt ${attempt} for ${ctx.request.url}`);
},
}));
Create a retry middleware with exponential backoff
Creates middleware that automatically retries failed requests with configurable backoff strategies. Supports exponential, linear, and fixed delay patterns.