import {z} from 'zod' export type GetApiBroadcastsIdArgs = {id: string}; export type GetApiBroadcastsIdOptions = {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 getApiBroadcastsIdResponse = 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 }; /** * Get a broadcast * * GET /broadcasts/{id} * * Also accepts auth: bearerOAuth */ export const getApiBroadcastsId = async (args: GetApiBroadcastsIdArgs, options?: GetApiBroadcastsIdOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiBroadcastsIdResponse, await res.json()) };