import {z} from 'zod' export type SendEmailRequest = {from: string, to: string | Array, cc?: (string | Array) | undefined, bcc?: (string | Array) | undefined, subject?: string | undefined, html: string, text?: string | undefined, reply_to?: string | undefined, headers?: {[key: string]: string} | undefined, message_class?: ('transactional' | 'marketing') | undefined, draft_id?: string | undefined, reply_to_email_id?: string | undefined, thread_id?: string | undefined, labels?: Array | undefined, template?: ({id: string, key?: string | undefined, version_id?: string | undefined, versionId?: string | undefined, data?: {[key: string]: unknown} | undefined} | {id?: string | undefined, key: string, version_id?: string | undefined, versionId?: string | undefined, data?: {[key: string]: unknown} | undefined}) | undefined} | {[key: string]: unknown} | {from: string, to: string | Array, cc?: (string | Array) | undefined, bcc?: (string | Array) | undefined, subject?: string | undefined, html?: string | undefined, text: string, reply_to?: string | undefined, headers?: {[key: string]: string} | undefined, message_class?: ('transactional' | 'marketing') | undefined, draft_id?: string | undefined, reply_to_email_id?: string | undefined, thread_id?: string | undefined, labels?: Array | undefined, template?: ({id: string, key?: string | undefined, version_id?: string | undefined, versionId?: string | undefined, data?: {[key: string]: unknown} | undefined} | {id?: string | undefined, key: string, version_id?: string | undefined, versionId?: string | undefined, data?: {[key: string]: unknown} | undefined}) | undefined} | {[key: string]: unknown} | {from: string, to: string | Array, cc?: (string | Array) | undefined, bcc?: (string | Array) | undefined, subject?: string | undefined, html?: string | undefined, text?: string | undefined, reply_to?: string | undefined, headers?: {[key: string]: string} | undefined, message_class?: ('transactional' | 'marketing') | undefined, draft_id?: string | undefined, reply_to_email_id?: string | undefined, thread_id?: string | undefined, labels?: Array | undefined, template: {id: string, key?: string | undefined, version_id?: string | undefined, versionId?: string | undefined, data?: {[key: string]: unknown} | undefined} | {id?: string | undefined, key: string, version_id?: string | undefined, versionId?: string | undefined, data?: {[key: string]: unknown} | undefined}} | {[key: string]: unknown}; export type CreateApiEmailsBatchArgs = {'Idempotency-Key'?: string | undefined, body: {emails: Array}}; export type CreateApiEmailsBatchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEmailsBatchResponse = z.object({data: z.array(z.object({index: z.number().int(), ok: z.boolean(), id: z.string()})), count: z.number().int(), successful: z.number().int(), failed: z.number().int()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Send up to 100 individualized messages * * POST /emails/batch */ export const createApiEmailsBatch = async (args: CreateApiEmailsBatchArgs, options?: CreateApiEmailsBatchOptions) => { const {baseUrl = "https://sendhq.cc/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/emails/batch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiEmailsBatchResponse, await res.json()) };