import {z} from 'zod' export type TriggerFireRequest = {payload: {[key: string]: unknown}, idempotencyKey?: string | undefined}; export type CreateApiV1AutomationsTriggersTriggerEventIdFireArgs = {'Idempotency-Key'?: string | undefined, triggerEventId: string, body: TriggerFireRequest}; export type CreateApiV1AutomationsTriggersTriggerEventIdFireOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const triggerFireResponse = z.object({success: z.boolean(), status: z.enum(["triggered", "idempotent_replay", "ready", "invalid_api_key", "invalid_json", "failed", "forbidden", "payload_mismatch", "trigger_event_not_found"]), code: z.string(), message: z.string(), triggerEventId: z.string().optional(), receivedAt: z.string(), details: z.object({resolvedPayload: z.record(z.string(), z.unknown()).optional(), warnings: z.array(z.unknown()).optional(), idempotencyKey: z.string().optional(), triggerInstanceId: z.string().optional(), publishedTransactionalEmails: z.array(z.object({emailId: z.string()}).and(z.record(z.string(), z.unknown()))).optional(), publishedAutomations: z.array(z.object({automationId: z.string(), title: z.string().optional()}).and(z.record(z.string(), z.unknown()))).optional(), automationRunIds: z.array(z.string()).optional(), counts: z.object({transactionalEmails: z.number().int(), automations: z.number().int()}).optional()}).and(z.record(z.string(), z.unknown())).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 }; /** * Fire a trigger * * POST /v1/automations/triggers/{triggerEventId}/fire * * Also accepts auth: apiKeyAuth */ export const createApiV1AutomationsTriggersTriggerEventIdFire = async (args: CreateApiV1AutomationsTriggersTriggerEventIdFireArgs, options?: CreateApiV1AutomationsTriggersTriggerEventIdFireOptions) => { 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 (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/automations/triggers/${encodeURIComponent(String(args.triggerEventId))}/fire${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(triggerFireResponse, await res.json()) };