import {z} from 'zod' export type UpdateBroadcastInput = {segment_id?: string | undefined, topic_id?: (string | null) | undefined, from?: string | undefined, subject?: string | undefined, 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 PatchApiBroadcastsIdArgs = {id: string, body: UpdateBroadcastInput}; export type PatchApiBroadcastsIdOptions = {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 patchApiBroadcastsIdResponse = 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 }; /** * Update a draft broadcast * * PATCH /broadcasts/{id} * * Also accepts auth: bearerOAuth */ export const patchApiBroadcastsId = async (args: PatchApiBroadcastsIdArgs, options?: PatchApiBroadcastsIdOptions) => { 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/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiBroadcastsIdResponse, await res.json()) };