import {z} from 'zod' export type CreateApiTriggersIdArgs = {id: string, body: {project_defined_event: string, name?: string | undefined, email_address?: (string | null) | undefined, phone_number?: (string | null) | undefined, excluded_customers?: Array | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, steps?: (Array<{type: 'email', custom_step_id?: string | undefined, email_template_id: string} | {type: 'email', custom_step_id?: string | undefined, subject: string, body: string} | {type: 'sms', custom_step_id?: string | undefined, sms_template_id: string} | {type: 'sms', custom_step_id?: string | undefined, message: string} | {type: 'wait', duration: number} | {type: 'if', filter: string}>) | undefined} | {internal_event_definition: {type: 'customer_created' | 'customer_joined_segment' | 'customer_birthday' | 'lead_captured', segment?: (string | null) | undefined}, name?: string | undefined, email_address?: (string | null) | undefined, phone_number?: (string | null) | undefined, excluded_customers?: Array | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, steps?: (Array<{type: 'email', custom_step_id?: string | undefined, email_template_id: string} | {type: 'email', custom_step_id?: string | undefined, subject: string, body: string} | {type: 'sms', custom_step_id?: string | undefined, sms_template_id: string} | {type: 'sms', custom_step_id?: string | undefined, message: string} | {type: 'wait', duration: number} | {type: 'if', filter: string}>) | undefined} | {name?: string | undefined, email_address?: (string | null) | undefined, phone_number?: (string | null) | undefined, excluded_customers?: Array | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, steps?: (Array<{type: 'email', custom_step_id?: string | undefined, email_template_id: string} | {type: 'email', custom_step_id?: string | undefined, subject: string, body: string} | {type: 'sms', custom_step_id?: string | undefined, sms_template_id: string} | {type: 'sms', custom_step_id?: string | undefined, message: string} | {type: 'wait', duration: number} | {type: 'if', filter: string}>) | undefined}}; export type CreateApiTriggersIdOptions = {baseUrl?: string | undefined, HttpBasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const trigger = z.object({id: z.string(), created_at: z.string(), updated_at: z.string(), name: z.string(), project_defined_event: z.string().nullable(), internal_event_definition: z.object({type: z.enum(["customer_created", "customer_joined_segment", "customer_birthday", "lead_captured"]), segment: z.string().nullable().optional()}).nullable(), trigger: z.string().nullable().optional(), steps: z.array(z.union([z.object({type: z.literal("email"), custom_step_id: z.string().optional(), email_template_id: z.string()}), z.object({type: z.literal("email"), custom_step_id: z.string().optional(), subject: z.string(), body: z.string()}), z.object({type: z.literal("sms"), custom_step_id: z.string().optional(), sms_template_id: z.string()}), z.object({type: z.literal("sms"), custom_step_id: z.string().optional(), message: z.string()}), z.object({type: z.literal("wait"), duration: z.number().int()}), z.object({type: z.literal("if"), filter: z.string()})])), status: z.enum(["draft", "active", "inactive"]), filter: z.string().nullable(), email_address: z.string().nullable(), phone_number: z.string().nullable(), attributed_revenue: z.union([z.number(), z.string()]).nullable(), metadata: z.record(z.string(), z.unknown()), statistics: z.object({total_customers_enrolled: z.number().int().optional(), total_customers_started: z.number().int().optional(), total_customers_finished: z.number().int().optional(), emails: z.object({sent: z.number().int(), delivered: z.number().int(), opened: z.number().int(), soft_bounced: z.number().int(), hard_bounced: z.number().int(), spam_reported: z.number().int(), clicked: z.number().int().nullable()}), custom_steps: z.array(z.object({custom_step: z.string(), sent: z.number().int()})), executions: z.object({total: z.number().int(), pending: z.number().int(), started: z.number().int(), finished: z.number().int(), errored: z.number().int()}), sms: z.object({sms_sent_count: z.number().int(), sms_sent_segments_count: z.number().int(), mms_sent_count: z.number().int(), mms_sent_segments_count: 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 }; /** * Update a trigger * * POST /triggers/{id} */ export const createApiTriggersId = async (args: CreateApiTriggersIdArgs, options?: CreateApiTriggersIdOptions) => { const {baseUrl = "https://api.pocketflows.com", fetch: fetchFn = fetch, HttpBasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HttpBasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${HttpBasicAuth.username}:${HttpBasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/triggers/${encodeURIComponent(String(args.id))}${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(trigger, await res.json()) };