Fetch middleware for server-directed retry timing based on Retry-After headers.
npm install @qfetch/middleware-retry-afterFetch middleware for server-directed retry timing based on Retry-After headers.
Respects server-provided retry timing for rate limiting and temporary unavailability. When a response includes a valid Retry-After header with a retryable status code (429 or 503 by default), the middleware waits the server-specified duration before retrying. Supports both delay-seconds and HTTP-date formats per RFC 9110.
Intended for use with @qfetch/core.
``bash`
npm install @qfetch/middleware-retry-after @proventuslabs/retry-strategies
`typescript
import { withRetryAfter } from '@qfetch/middleware-retry-after';
import { withBaseUrl } from '@qfetch/middleware-base-url';
import { withResponseError } from '@qfetch/middleware-response-error';
import { fullJitter, upto } from '@proventuslabs/retry-strategies';
import { compose } from '@qfetch/core';
// Resilient API client that respects rate limits
const api = compose(
withResponseError(),
withRetryAfter({
// Retry up to 3 times with jitter to prevent thundering herd
strategy: () => upto(3, fullJitter(100, 5_000)),
// Cap server-requested delays at 30 seconds
maxServerDelay: 30_000,
}),
withBaseUrl('https://api.example.com/v1/'),
)(fetch);
// Automatic retry on 429/503 with Retry-After header
const data = await api('resource').then(r => r.json());
`
For complete API reference, examples, and type definitions, see the API documentation.
- RFC 9110 §10.2.3 - Retry-After - Defines Retry-After` header format and semantics
- RFC 6585 §4 - 429 Too Many Requests - Rate limiting status code
- RFC 9110 §15.6.4 - 503 Service Unavailable - Temporary unavailability status code