Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { APIError } from '@composio/client'; |
| 2 | |
| 3 | /** Whether a client error is the API rejecting the request with a 429. */ |
| 4 | export function isRateLimitResponse(error: unknown): error is APIError { |
| 5 | return error instanceof APIError && error.status === 429; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Seconds a 429 asks the caller to wait, from its `Retry-After` header (seconds or an HTTP date). |
| 10 | * Undefined when the header is missing or unreadable. |
| 11 | * |
| 12 | * @internal |
| 13 | */ |
| 14 | export function getRetryAfterSeconds(error: APIError): number | undefined { |
| 15 | const value = error.headers?.get('retry-after')?.trim(); |
| 16 | if (!value) { |
| 17 | return undefined; |
| 18 | } |
| 19 | const seconds = Number(value); |
| 20 | if (Number.isFinite(seconds)) { |
| 21 | return seconds >= 0 ? seconds : undefined; |
| 22 | } |
| 23 | const date = Date.parse(value); |
| 24 | return Number.isNaN(date) ? undefined : Math.max(0, (date - Date.now()) / 1000); |
| 25 | } |