import { CloseBotApiError, getCloseBotApi as getCloseBotContractApi, } from "@automate.ax/integration-contracts/closebot" import { parseRetryAfter, retryableActionError, terminalActionError, } from "../../../automation/actions" export const CLOSEBOT_ACCOUNT = "closebot" /** * Creates a CloseBot client whose failures carry action disposition. * * @param secret - Resolved CloseBot account secret. */ export function getCloseBotApi( secret: unknown, ): ReturnType { const api = getCloseBotContractApi(secret) const request = api.request.bind(api) api.request = async (path, options) => { try { return await request(path, options) } catch (error) { throw classifyCloseBotApiError(error) } } return api } /** * Marks known CloseBot failures without replacing their error identity. * * @param error - Provider error to classify. */ function classifyCloseBotApiError(error: unknown) { if (!(error instanceof CloseBotApiError)) return error if (error.status === 429) { return retryableActionError(error, { retryAt: parseRetryAfter(error.retryAfter), }) } return error.status < 500 ? terminalActionError(error) : error } /** * Returns whether an input contains a mutable provider field. * * @param input - Parsed action input. * @param ignoredKeys - Routing keys excluded from the update body. */ export function hasCloseBotUpdate( input: Record, ignoredKeys: readonly string[], ) { return Object.entries(input).some( ([key, value]) => !ignoredKeys.includes(key) && value !== undefined, ) }