qfetch
    Preparing search index...

    Function withAuthorization

    • Middleware that injects authorization headers and retries on 401 responses.

      Parameters

      Returns MiddlewareExecutor

      Injects Authorization headers using a TokenProvider interface. When a 401 Unauthorized response is received, the middleware calls the token provider again (allowing token refresh) and retries according to the configured backoff strategy. Existing Authorization headers are respected and not overridden.

      Only 401 status triggers retry; other error statuses pass through unchanged.

      If the token provider returns an invalid token (missing properties).

      If the token provider throws an error during getToken().

      If the request's AbortSignal is aborted during retry delay.

      If the strategy delay exceeds maximum safe timeout (~24.8 days).

      import { withAuthorization } from "@qfetch/middleware-authorization";
      import { constant, upto } from "@proventuslabs/retry-strategies";

      const qfetch = withAuthorization({
      tokenProvider: {
      getToken: async () => ({
      accessToken: "my-token",
      tokenType: "Bearer"
      })
      },
      strategy: () => upto(1, constant(0))
      })(fetch);