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AA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,OAAA,GAAwB;AAC7B,IAAA,MAAM,GAAA,GAAM,QAAQ,GAAA,CAAI,cAAA;AACxB,IAAA,IAAI,CAAC,GAAA,EAAK;AACR,MAAA,MAAM,IAAI,KAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AACA,IAAA,OAAO,IAAI,cAAa,GAAG,CAAA;AAAA,EAC7B;AACF","file":"email.cjs","sourcesContent":["import { Resend } from 'resend';\nimport type { CreateEmailOptions } from 'resend';\n\n/**\n * Options for sending a single email or one item in a batch.\n */\nexport interface MailOptions {\n  /**\n   * Sender address.\n   * - Custom domains (e.g. `hi@aastar.io`) must be verified in the Resend dashboard\n   *   (SPF/DKIM/DMARC DNS records) before use.\n   * - Without domain verification, use `onboarding@resend.dev` (test only).\n   */\n  from: string;\n  to: string | string[];\n  subject: string;\n  html?: string;\n  text?: string;\n  cc?: string | string[];\n  bcc?: string | string[];\n  replyTo?: string;\n  attachments?: Array<{ filename: string; content: Buffer | string }>;\n  headers?: Record<string, string>;\n  tags?: Array<{ name: string; value: string }>;\n  /**\n   * ISO 8601 datetime with timezone offset for scheduled delivery.\n   * Example: `\"2026-06-01T09:00:00+07:00\"` or `\"2026-06-01T02:00:00Z\"`.\n   * Must be a future timestamp; Resend rejects past or timezone-ambiguous values.\n   */\n  scheduledAt?: string;\n  /**\n   * Idempotency key for agent retry scenarios.\n   * Duplicate calls with the same key will not send the email again.\n   * Only supported by `send()` — ignored by `sendBatch()`.\n   */\n  idempotencyKey?: string;\n}\n\n/** Result of a successful single send or one item in a batch result. */\nexport interface SendResult {\n  id: string;\n}\n\n/** Per-item result for sendBatch — includes index for error attribution. */\nexport interface BatchSendResult {\n  index: number;\n  id: string;\n}\n\n/** Error thrown when one or more batch items fail. */\nexport class BatchSendError extends Error {\n  constructor(\n    message: string,\n    public readonly failures: Array<{ index: number; reason: string }>,\n  ) {\n    super(message);\n    this.name = 'BatchSendError';\n  }\n}\n\nfunction toResendPayload(options: Omit<MailOptions, 'idempotencyKey'>): CreateEmailOptions {\n  return options as unknown as CreateEmailOptions;\n}\n\n/**\n * ResendMailer — server-side email utility built on the Resend API.\n *\n * Install: `pnpm add @aastar/email`\n *\n * @example\n * ```ts\n * import { ResendMailer } from '@aastar/email';\n *\n * const mailer = ResendMailer.fromEnv(); // reads RESEND_API_KEY\n * await mailer.send({\n *   from: 'hi@aastar.io',\n *   to: 'user@example.com',\n *   subject: 'Welcome',\n *   html: '<h1>Hello!</h1>',\n * });\n * ```\n *\n * @example Agent tool with idempotency\n * ```ts\n * await mailer.send({\n *   from: 'hi@aastar.io',\n *   to: 'user@example.com',\n *   subject: 'Your receipt',\n *   html: '...',\n *   idempotencyKey: `receipt-${txHash}`,\n * });\n * ```\n */\nexport class ResendMailer {\n  private client: Resend;\n\n  /** @param apiKey - Resend API key from https://resend.com/api-keys */\n  constructor(apiKey: string) {\n    if (!apiKey) {\n      throw new Error('Resend API key is required. Get yours at https://resend.com/api-keys');\n    }\n    this.client = new Resend(apiKey);\n  }\n\n  /**\n   * Send a single email.\n   * @returns `{ id }` — Resend email ID for delivery status queries\n   */\n  async send(options: MailOptions): Promise<SendResult> {\n    const { idempotencyKey, ...emailOptions } = options;\n    const { data, error } = await this.client.emails.send(\n      toResendPayload(emailOptions),\n      idempotencyKey ? { idempotencyKey } : undefined,\n    );\n    if (error) throw new Error(`Failed to send email: ${error.message}`);\n    if (!data?.id) throw new Error('Resend returned no email ID');\n    return { id: data.id };\n  }\n\n  /**\n   * Send a batch of emails (max 100 per request).\n   * `idempotencyKey` is stripped per item — not supported by the batch endpoint.\n   *\n   * @throws {BatchSendError} if any item is missing an ID in the response\n   * @returns Results in the same order as the input\n   */\n  async sendBatch(emails: MailOptions[]): Promise<BatchSendResult[]> {\n    if (emails.length === 0) return [];\n    if (emails.length > 100) {\n      throw new Error('sendBatch supports at most 100 emails per request');\n    }\n\n    const payload = emails.map(({ idempotencyKey: _, ...rest }) => toResendPayload(rest));\n    const { data, error } = await this.client.batch.send(payload);\n\n    if (error) throw new Error(`Failed to send batch emails: ${error.message}`);\n\n    const results = (data as unknown as Array<{ id?: string }>) ?? [];\n    const failures: Array<{ index: number; reason: string }> = [];\n    const successes: BatchSendResult[] = [];\n\n    results.forEach((item: { id?: string }, index: number) => {\n      if (!item?.id) {\n        failures.push({ index, reason: 'No email ID returned for this item' });\n      } else {\n        successes.push({ index, id: item.id });\n      }\n    });\n\n    if (failures.length > 0) {\n      throw new BatchSendError(\n        `sendBatch: ${failures.length} of ${emails.length} emails failed`,\n        failures,\n      );\n    }\n\n    return successes;\n  }\n\n  /**\n   * Create a ResendMailer from the `RESEND_API_KEY` environment variable.\n   * Recommended for server-side and CI usage.\n   * @throws if the environment variable is not set\n   */\n  static fromEnv(): ResendMailer {\n    const key = process.env.RESEND_API_KEY;\n    if (!key) {\n      throw new Error(\n        'RESEND_API_KEY environment variable is not set. Pass the key directly: new ResendMailer(\"re_...\")',\n      );\n    }\n    return new ResendMailer(key);\n  }\n}\n"]}