import {z} from 'zod' export type GetApiBroadcastsIdRecipientsArgs = {id: string, status?: ('queued' | 'sending' | 'sent' | 'held' | 'skipped' | 'failed') | undefined, activity?: ('opened' | 'clicked' | 'not_opened' | 'not_delivered' | 'bounced' | 'complained' | 'unsubscribed') | undefined, query?: string | undefined, include?: 'links' | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiBroadcastsIdRecipientsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBroadcastsIdRecipientsResponse = z.object({data: z.array(z.object({contactId: z.string(), email: z.string(), status: z.string(), skipReason: z.unknown(), emailId: z.string(), error: z.unknown(), attempts: z.number().int(), sentAt: z.string(), provider: z.string(), deliveredAt: z.string(), openedAt: z.string(), clickedAt: z.unknown(), opens: z.number().int(), clicks: z.number().int(), bouncedAt: z.unknown(), complainedAt: z.unknown(), unsubscribedAt: z.unknown()})), count: z.number().int(), totals: z.object({queued: z.number().int(), sending: z.number().int(), sent: z.number().int(), held: z.number().int(), skipped: z.number().int(), failed: z.number().int(), total: 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 }; /** * List a broadcast's recipients, outcomes, and engagement * * GET /broadcasts/{id}/recipients */ export const getApiBroadcastsIdRecipients = async (args: GetApiBroadcastsIdRecipientsArgs, options?: GetApiBroadcastsIdRecipientsOptions) => { const {baseUrl = "https://sendhq.cc/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.activity != null) query.append("activity", String(args.activity)) if (args.query != null) query.append("query", String(args.query)) if (args.include != null) query.append("include", String(args.include)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/broadcasts/${encodeURIComponent(String(args.id))}/recipients${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiBroadcastsIdRecipientsResponse, await res.json()) };