import {z} from 'zod' export type CreateBroadcastInput = {segment_id: string, topic_id?: string | undefined, from: string, subject: string, html?: string | undefined, text?: string | undefined, reply_to?: string | undefined, name?: string | undefined, scheduled_at?: (string | string) | undefined, ab_test?: ({variants: Array<{name: string, subject: string, html?: string | undefined, text?: string | undefined, weight: number}>, test_sample_size?: number | undefined, test_duration_minutes?: number | undefined}) | undefined}; export type CreateApiBroadcastsArgs = {body: CreateBroadcastInput}; export type CreateApiBroadcastsOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const broadcast = z.object({id: z.number().int(), publicId: z.string(), name: z.string(), fromAddress: z.string().optional(), fromName: z.string().nullable().optional(), subject: z.string(), htmlBody: z.string().nullable().optional(), textBody: z.string().nullable().optional(), replyTo: z.array(z.string()).nullable().optional(), segmentId: z.number().int().nullable().optional(), segmentPublicId: z.string().nullable().optional(), topicId: z.number().int().nullable().optional(), topicPublicId: z.string().nullable().optional(), status: z.enum(["draft", "queued", "sending", "sent", "cancelled"]), scheduledAt: z.string().nullable().optional(), sentAt: z.string().nullable().optional(), totalRecipients: z.number().int().optional(), deliveredCount: z.number().int().optional(), openedCount: z.number().int().optional(), clickedCount: z.number().int().optional(), bouncedCount: z.number().int().optional(), deferredCount: z.number().int().optional(), abTestEnabled: z.boolean().optional(), winnerVariantId: z.number().int().nullable().optional(), testSampleSize: z.number().int().nullable().optional(), testDurationMinutes: z.number().int().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const createApiBroadcastsResponse = z.object({broadcast: broadcast}); 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 }; /** * Create a draft broadcast * * POST /broadcasts * * Also accepts auth: bearerOAuth */ export const createApiBroadcasts = async (args: CreateApiBroadcastsArgs, options?: CreateApiBroadcastsOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/broadcasts${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(createApiBroadcastsResponse, await res.json()) };