import {z} from 'zod' export type GetApiAutomationsAutomationIdMessagesArgs = {automation_id: number}; export type GetApiAutomationsAutomationIdMessagesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, ShopId?: string | undefined, fetch?: typeof fetch | undefined}; export const automationPersonalizationConfig = z.object({enabled: z.boolean().optional(), tone: z.union([z.enum(["engaging", "bold", "inspirational"]), z.string()]).nullable().optional(), fallback_message: z.string().nullable().optional(), offer_discussion: z.string().nullable().optional(), product_description: z.string().nullable().optional(), product_selling_points: z.string().nullable().optional(), word_counts: z.record(z.string(), z.number().int()).nullable().optional(), name_source: z.string().nullable().optional()}); export const automationMessagesTemplateResponse = z.object({automation_id: z.number().int(), automation_name: z.string().nullable().optional(), automation_type: z.string().nullable().optional(), tc_invite_message: z.string().nullable().optional(), message_sequence: z.array(z.unknown()).optional(), follow_up_steps: z.array(z.unknown()).optional(), ai_custom_qa: z.array(z.unknown()).optional(), personalization: automationPersonalizationConfig.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 }; /** * Automation message templates * * GET /automations/{automation_id}/messages */ export const getApiAutomationsAutomationIdMessages = async (args: GetApiAutomationsAutomationIdMessagesArgs, options?: GetApiAutomationsAutomationIdMessagesOptions) => { const {baseUrl = "https://api.reacherapp.com/public/v1", fetch: fetchFn = fetch, ApiKeyAuth, ShopId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (ShopId !== undefined) headers.set("x-shop-id", ShopId) const url = `${baseUrl.replace(/\/+$/, '')}/automations/${encodeURIComponent(String(args.automation_id))}/messages${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(automationMessagesTemplateResponse, await res.json()) };