import {z} from 'zod' export type EmailsBroadcastParametersUpdate = {name?: string | undefined, subject?: string | undefined, preheadline?: (string | null) | undefined, from_email?: string | undefined, reply_to_email?: (string | null) | undefined, html_body?: (string | null) | undefined, text_body?: (string | null) | undefined, template_id?: (number | null) | undefined, filter_id?: (string | number | null) | undefined, split_test?: boolean | undefined, split_test_percentage?: number | undefined, split_test_wait_time?: number | undefined, variants?: (Array<{subject: string, preheadline?: (string | null) | undefined}>) | undefined, smart_sending?: boolean | undefined, smart_sending_time?: (number | null) | undefined, smart_sending_unit?: ('hour' | 'day' | 'null' | null) | undefined, send_at?: (string | null) | undefined, send_at_local_time?: (string | null) | undefined, time_zone?: (string | null) | undefined, external_id?: (string | null) | undefined, topic_ids?: Array | undefined}; export type PatchApiEmailsBroadcastsIdArgs = {id: string, body: {emails_broadcast?: EmailsBroadcastParametersUpdate | undefined}}; export type PatchApiEmailsBroadcastsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emailsBroadcastAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), workspace_id: z.number().int().optional(), name: z.string().nullable().optional(), subject: z.string().nullable().optional(), preheadline: z.string().nullable().optional(), status: z.enum(["draft", "scheduled", "sending", "paused", "unhealthy", "complete", "canceled"]).optional(), origination_channel_type: z.string().nullable().optional(), origination_channel_id: z.number().int().nullable().optional(), external_id: z.string().nullable().optional(), filter_id: z.number().int().nullable().optional(), send_at: z.string().nullable().optional(), send_at_local_time: z.string().nullable().optional(), time_zone: z.string().nullable().optional(), send_immediately: z.boolean().optional(), smart_sending: z.boolean().optional(), smart_sending_time: z.number().nullable().optional(), smart_sending_unit: z.enum(["hour", "day", "null"]).nullable().optional(), sending_at: z.string().nullable().optional(), complete_at: z.string().nullable().optional(), from_email: z.string().nullable().optional(), from_name: z.string().nullable().optional(), reply_to_email: z.string().nullable().optional(), template: z.object({id: z.number().int().optional(), public_id: z.string().optional(), name: z.string().nullable().optional()}).nullable().optional(), topic_ids: z.array(z.number().int()).optional(), split_test: z.boolean().optional(), split_test_percentage: z.number().int().nullable().optional(), split_test_wait_time: z.number().int().nullable().optional(), variants: z.array(z.object({subject: z.string().nullable().optional(), preheadline: z.string().nullable().optional(), send_percentage: z.number().nullable().optional(), main: z.boolean().optional(), open_count: z.number().int().optional(), winner: z.boolean().nullable().optional()})).optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); 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 Email Broadcast * * PATCH /emails/broadcasts/{id} * * Also accepts auth: OAuth2 */ export const patchApiEmailsBroadcastsId = async (args: PatchApiEmailsBroadcastsIdArgs, options?: PatchApiEmailsBroadcastsIdOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/emails/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(emailsBroadcastAttributes, await res.json()) };