import {z} from 'zod' export type AutomationsUpdateRequest = {name?: string | undefined, description?: string | undefined, nodes?: (Array<{id: string, label: string, description?: string | undefined, type: 'trigger', config: {actionType?: string | undefined, mode?: ('event' | 'manualAudience') | undefined, triggerEventId?: string | undefined, audienceId?: string | undefined, eventName?: string | undefined}} | {id: string, label: string, description?: string | undefined, type: 'sendEmail', config: {actionType?: string | undefined, emailId: string, emailVersionId: string, domainId: string, subject: string, previewText: string, messageClass?: ('marketing' | 'transactional') | undefined, fromName?: string | undefined, replyTo?: string | undefined, emailTitle?: string | undefined, fromAddress?: string | undefined}} | {id: string, label: string, description?: string | undefined, type: 'wait', config: {actionType?: string | undefined, duration: number, unit: 'ms' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks'}} | {id: string, label: string, description?: string | undefined, type: 'filter', config: {actionType?: string | undefined, logicalOperator: 'AND' | 'OR', conditions: Array<{field: string, type: 'string', operator: 'equals' | 'not_equals' | 'contains' | 'not_contains' | 'starts_with' | 'ends_with', value: string} | {field: string, type: 'number', operator: 'equals' | 'not_equals' | 'gt' | 'gte' | 'lt' | 'lte', value: number} | {field: string, type: 'date', operator: 'equals' | 'not_equals' | 'gt' | 'gte' | 'lt' | 'lte', value: string | number} | {field: string, type: 'string', operator: 'in' | 'not_in' | 'contains_any' | 'not_contains_any', value: Array} | {field: string, type: 'number', operator: 'in' | 'not_in', value: Array} | {field: string, type: 'number', operator: 'between', value: Array} | {field: string, type: 'date', operator: 'between', value: Array} | {field: string, type: 'string', operator: 'is_empty' | 'is_not_empty'} | {field: string, type: 'number', operator: 'is_empty' | 'is_not_empty'} | {field: string, type: 'date', operator: 'is_empty' | 'is_not_empty'} | {field: string, type: 'bool', operator: 'is_true' | 'is_false'}>}} | {id: string, label: string, description?: string | undefined, type: 'split', config: {actionType?: string | undefined, mode: 'percentage', leftLabel: string, rightLabel: string, leftPercentage: number, seed?: string | undefined} | {actionType?: string | undefined, mode: 'condition', leftLabel: string, rightLabel: string, logicalOperator: 'AND' | 'OR', conditions: Array<{field: string, type: 'string', operator: 'equals' | 'not_equals' | 'contains' | 'not_contains' | 'starts_with' | 'ends_with', value: string} | {field: string, type: 'number', operator: 'equals' | 'not_equals' | 'gt' | 'gte' | 'lt' | 'lte', value: number} | {field: string, type: 'date', operator: 'equals' | 'not_equals' | 'gt' | 'gte' | 'lt' | 'lte', value: string | number} | {field: string, type: 'string', operator: 'in' | 'not_in' | 'contains_any' | 'not_contains_any', value: Array} | {field: string, type: 'number', operator: 'in' | 'not_in', value: Array} | {field: string, type: 'number', operator: 'between', value: Array} | {field: string, type: 'date', operator: 'between', value: Array} | {field: string, type: 'string', operator: 'is_empty' | 'is_not_empty'} | {field: string, type: 'number', operator: 'is_empty' | 'is_not_empty'} | {field: string, type: 'date', operator: 'is_empty' | 'is_not_empty'} | {field: string, type: 'bool', operator: 'is_true' | 'is_false'}>}}>) | undefined, connections?: (Array<{from: string, to: string, branch?: ('left' | 'right') | undefined}>) | undefined, triggerEventId?: string | undefined, dryRun?: boolean | undefined, published?: boolean | undefined, automationVersionId?: string | undefined, stop_in_flight?: boolean | undefined, paused?: boolean | undefined}; export type PatchApiV1AutomationsAutomationIdArgs = {automationId: string, body: AutomationsUpdateRequest}; export type PatchApiV1AutomationsAutomationIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const automationRow = z.object({automationId: z.string().min(1).max(64), automationVersionId: z.string().min(1).max(64), triggerEventId: z.string().min(1).max(256).optional(), name: z.string().min(1).max(120), description: z.string().max(2000).optional(), version: z.union([z.number().int().gte(0), z.literal("latest")]), published: z.boolean(), paused: z.boolean().optional(), pausedAt: z.number().optional(), nodes: z.array(z.union([z.object({id: z.string().min(1), label: z.string().min(1), description: z.string().optional(), type: z.literal("trigger"), config: z.object({actionType: z.string().optional(), mode: z.enum(["event", "manualAudience"]).optional(), triggerEventId: z.string().optional(), audienceId: z.string().optional(), eventName: z.string().optional()})}), z.object({id: z.string().min(1), label: z.string().min(1), description: z.string().optional(), type: z.literal("sendEmail"), config: z.object({actionType: z.string().optional(), emailId: z.string().optional(), emailVersionId: z.string().optional(), emailTitle: z.string().optional(), subject: z.string().optional(), previewText: z.string().optional(), messageClass: z.enum(["marketing", "transactional"]).optional(), fromName: z.string().optional(), fromAddress: z.string().optional(), domainId: z.string().optional(), replyTo: z.string().optional(), to: z.unknown(), html: z.string().optional(), variables: z.record(z.string(), z.unknown()).optional()})}), z.object({id: z.string().min(1), label: z.string().min(1), description: z.string().optional(), type: z.literal("wait"), config: z.object({actionType: z.string().optional(), duration: z.number(), unit: z.enum(["ms", "seconds", "minutes", "hours", "days", "weeks"])})}), z.object({id: z.string().min(1), label: z.string().min(1), description: z.string().optional(), type: z.literal("filter"), config: z.object({actionType: z.string().optional(), logicalOperator: z.enum(["AND", "OR"]), conditions: z.array(z.object({field: z.string(), operator: z.string(), value: z.union([z.string(), z.number(), z.boolean(), z.array(z.union([z.string(), z.number()]))]).optional(), type: z.enum(["string", "number", "date", "bool"]).optional()}))})}), z.object({id: z.string().min(1), label: z.string().min(1), description: z.string().optional(), type: z.literal("split"), config: z.union([z.object({actionType: z.string().optional(), mode: z.literal("percentage"), leftLabel: z.string(), rightLabel: z.string(), leftPercentage: z.number(), seed: z.string().optional()}), z.object({actionType: z.string().optional(), mode: z.literal("condition"), leftLabel: z.string(), rightLabel: z.string(), logicalOperator: z.enum(["AND", "OR"]), conditions: z.array(z.object({field: z.string(), operator: z.string(), value: z.union([z.string(), z.number(), z.boolean(), z.array(z.union([z.string(), z.number()]))]).optional(), type: z.enum(["string", "number", "date", "bool"]).optional()}))})])})])).optional(), connections: z.array(z.object({from: z.string().min(1), to: z.string().min(1), branch: z.enum(["left", "right"]).optional()})).optional(), emailIds: z.array(z.string().min(1).max(64)), createdBy: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), publishedAt: z.string().optional(), versions: z.array(z.object({version: z.union([z.number().int().gte(0), z.literal("latest")]), automationVersionId: z.string().min(1).max(64)})).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 or publish an automation * * PATCH /v1/automations/{automationId} * * Also accepts auth: apiKeyAuth */ export const patchApiV1AutomationsAutomationId = async (args: PatchApiV1AutomationsAutomationIdArgs, options?: PatchApiV1AutomationsAutomationIdOptions) => { const {baseUrl = "https://brew.new/api", 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(/\/+$/, '')}/v1/automations/${encodeURIComponent(String(args.automationId))}${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(automationRow, await res.json()) };