import { getResendApi as getResendRawApi } from "@automate.ax/integration-contracts/resend" import * as z from "zod" import { integrationScope as scope, type ResolvedIntegrationAccount, } from "../../automation/integrations" const RESEND_API_KEY_SECRET_SCHEMA = z.object({ apiKey: z.string().min(1), }) const RESEND_OAUTH_SECRET_SCHEMA = z.object({ accessToken: z.string().min(1), }) const RESEND_SEND_SCOPE = scope.any("full_access", "emails:send") export const RESEND_FULL_ACCESS_ACCOUNT_OPTIONS = { connections: [ { connectionMethodId: "oauth", requiredScope: "full_access" }, { connectionMethodId: "api-key", requiredScope: "full_access" }, ], } as const export const RESEND_SEND_ACCOUNT_OPTIONS = { connections: [ { connectionMethodId: "oauth", requiredScope: RESEND_SEND_SCOPE }, { connectionMethodId: "api-key", requiredScope: RESEND_SEND_SCOPE }, ], } as const /** * Creates a schema-validating Resend client for a resolved account. * * @param account - Resolved Resend OAuth or API-key account. * @throws {Error} When the account uses an unsupported connection method. */ export function getResendApi(account: ResolvedIntegrationAccount<"resend">) { if (account.connectionMethodId === "oauth") { return getResendRawApi(RESEND_OAUTH_SECRET_SCHEMA.parse(account.secret)) } if (account.connectionMethodId === "api-key") { return getResendRawApi(RESEND_API_KEY_SECRET_SCHEMA.parse(account.secret)) } throw new Error( `Unsupported Resend connection method: ${account.connectionMethodId}`, ) }