import { AxiomApiError, getAxiomApi as getAxiomContractApi, } from "@automate.ax/integration-contracts/axiom" import { retryableActionError, terminalActionError, } from "../../automation/actions" import type { ResolvedIntegrationAccount } from "../../automation/integrations" export const AXIOM_ACCOUNT = "axiom" export const AXIOM_API_TOKEN_ACCOUNT = { connections: [{ connectionMethodId: "api-token" }], } as const /** * Resolves an action-aware Axiom client. * * @param account - Resolved Axiom API-token account. * @param edgeUrl - Optional Axiom-owned ingest or query edge. */ export function getAxiomApi( account: ResolvedIntegrationAccount<"axiom">, edgeUrl?: string, ) { const api = getAxiomContractApi(account, edgeUrl) const request = api.request.bind(api) api.request = async (path, options) => { try { return await request(path, options) } catch (error) { if (!(error instanceof AxiomApiError)) throw error if (error.status === 429 || error.status === 430) { const reset = error.queryLimitReset ?? error.ingestLimitReset ?? error.rateLimitReset throw retryableActionError(error, { retryAt: error.retryAfter !== undefined ? new Date(Date.now() + error.retryAfter * 1_000) : reset !== undefined ? new Date(reset * 1_000) : undefined, }) } throw error.status < 500 ? terminalActionError(error) : error } } return api } /** * Encodes one provider resource path segment. * * @param value - Raw provider resource identifier. */ export function axiomPath(value: string) { return encodeURIComponent(value) }