import {z} from 'zod' export type GetApiCustomersIdCampaignsArgs = {id: string, after?: string | undefined}; export type GetApiCustomersIdCampaignsOptions = {baseUrl?: string | undefined, HttpBasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const campaign = z.object({id: z.string(), created_at: z.string(), updated_at: z.string(), business: z.string().nullable(), name: z.string(), enrolled_customers: z.array(z.string()).optional(), enrolled_customer_ids: z.array(z.string()).optional(), segment: z.string().nullable(), 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()})])), email_address: z.string().nullable(), phone_number: z.string().nullable(), status: z.enum(["draft", "scheduled", "started", "stopped", "finished"]), scheduled_at: z.string().nullable(), scheduled_end_at: z.string().nullable(), 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()})}), consumed_message_segments: z.object({sms: z.number().int(), mms: z.number().int(), email: z.number().int()}), estimated_message_segments: z.object({sms: z.number().int(), mms: z.number().int(), email: z.number().int()}).optional(), attributed_revenue: z.string().nullable(), metadata: z.record(z.string(), z.unknown())}); export const getApiCustomersIdCampaignsResponse = z.object({page_info: z.object({has_next_page: z.boolean(), end_cursor: z.string().nullable()}), total_count: z.number().int().optional(), data: z.array(campaign)}); 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 }; /** * List the campaigns a customer is enrolled in, directly or through a segment (paginated) * * GET /customers/{id}/campaigns */ export const getApiCustomersIdCampaigns = async (args: GetApiCustomersIdCampaignsArgs, options?: GetApiCustomersIdCampaignsOptions) => { const {baseUrl = "https://api.pocketflows.com", fetch: fetchFn = fetch, HttpBasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (HttpBasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${HttpBasicAuth.username}:${HttpBasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.id))}/campaigns${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(getApiCustomersIdCampaignsResponse, await res.json()) };