import {z} from 'zod' export type CreateApiBusinessesIdEmailBlastsArgs = {id: string, body: {name: string, email_template: string | unknown, enrolled_customers?: Array | undefined, excluded_customers?: Array | undefined, email_address?: (string | null) | undefined, segment?: (string | null) | undefined}}; export type CreateApiBusinessesIdEmailBlastsOptions = {baseUrl?: string | undefined, HttpBasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const emailBlast = z.object({id: z.string(), name: z.string(), created_at: z.string(), email_template: z.string().nullable(), status: z.string(), scheduled_at: z.string().nullable(), segment: z.string().nullable(), email_address: z.string().nullable(), statistics: z.object({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()}), executions: z.object({total: z.number().int(), pending: z.number().int(), started: z.number().int(), finished: z.number().int(), errored: z.number().int()})}), enrolled_customers: z.array(z.string()).optional(), excluded_customers: z.array(z.string()).optional(), attributed_revenue: z.string().nullable()}); 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 }; /** * Create an email blast * * POST /businesses/{id}/email_blasts */ export const createApiBusinessesIdEmailBlasts = async (args: CreateApiBusinessesIdEmailBlastsArgs, options?: CreateApiBusinessesIdEmailBlastsOptions) => { 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(/\/+$/, '')}/businesses/${encodeURIComponent(String(args.id))}/email_blasts${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(emailBlast, await res.json()) };