• 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.

    Parameters

    Returns FejMiddlewareFunction

    Middleware function

    Example: Basic retry with defaults

    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

    Example: Custom retry logic

    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}`);
    },
    }));