CSRF-X Auth Library - v1.0.3
    Preparing search index...

    Function fetchWithRetry

    HTTP client with automatic retries and timeout support.

    Wraps fetch with exponential backoff retries and per‑request timeout.

    The request URL.

    Configuration options (timeout, retries, headers, etc.).

    Optional predicate to override retryOn.

    A Response object from the first successful request.

    With code NETWORK_ERROR when all attempts fail.

    0.1.1

    • Performs an HTTP request with automatic retries and timeout.

      Parameters

      • url: string | URL

        The request URL (string or URL object).

      • options: FetchOptions = {}

        Configuration options extending standard RequestInit with retry settings.

      • shouldRetry: (res: Response) => boolean = DEFAULT_RETRY_PREDICATE

        Optional predicate that overrides the retryOn value from options.
        If provided, this function is used instead of options.retryOn.

      Returns Promise<Response>

      A Response object from the first successful request (i.e., a response that either
      satisfies response.ok === true or for which the retry predicate returns false).

      With code NETWORK_ERROR when all attempts fail, or when the last error
      cannot be classified otherwise. The original error is preserved as the cause.

      Retry conditions:

      • The request fails with a network error, timeout, or any exception.
      • The response is not ok (i.e., response.ok === false) and the retry predicate returns true.

      Retry mechanism:

      • Retries use exponential backoff: retryDelayMs * 2^attempt.
      • The timeout is enforced per attempt using an AbortController.
      • If a response is received but the retry predicate returns false, the response is returned immediately (no further retries).

      Default behavior:

      • Timeout: 15 seconds
      • Max retries: 3
      • Retry delay: 100ms (exponential)
      • Retry predicate: only server errors (HTTP 5xx)

      Important: The signal option is not allowed because it is used internally.
      If you need custom abort logic, wrap this function with your own AbortController.

      // Basic usage with default retry (5xx only)
      const res = await fetchWithRetry('https://api.example.com/data');
      // Custom retry for any non‑200 response, with more retries
      const res = await fetchWithRetry('/api/login', {
      method: 'POST',
      body: JSON.stringify({ user: 'admin' }),
      headers: { 'Content-Type': 'application/json' },
      retryOn: (res) => !res.ok,
      maxRetries: 5,
      timeoutMs: 10000
      });
      // Override retry predicate via third parameter
      const res = await fetchWithRetry(
      'https://api.example.com/data',
      { maxRetries: 2 },
      (res) => res.status === 429 // retry only on rate limiting
      );

      0.1.1