import {z} from 'zod' export type TriggersPatchRequest = {title?: string | undefined, description?: string | undefined, payloadSchema?: ({type: 'object', fields: Array<{key: string, type: 'string' | 'int' | 'boolean', required: boolean, fallbackValue?: (string | number | boolean) | undefined, pii?: ('none' | 'low' | 'high') | undefined}>}) | undefined}; export type PatchApiV1AutomationsTriggersTriggerEventIdArgs = {triggerEventId: string, body: TriggersPatchRequest}; export type PatchApiV1AutomationsTriggersTriggerEventIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const triggerRow = z.object({triggerEventId: z.string().min(1).max(256), title: z.string().min(1).max(120), description: z.string().max(2000).optional(), provider: z.enum(["brew_api", "clerk", "stripe", "shopify", "stytch", "supabase", "workos", "revenuecat", "custom"]), providerEventKey: z.string().optional(), payloadSchema: z.object({type: z.literal("object"), fields: z.array(z.object({key: z.string().min(1), type: z.enum(["string", "int", "boolean"]), required: z.boolean(), fallbackValue: z.union([z.string(), z.number(), z.boolean()]).optional(), pii: z.enum(["none", "low", "high"]).optional()}))}), createdAt: z.string(), updatedAt: z.string()}); 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 a trigger * * PATCH /v1/automations/triggers/{triggerEventId} * * Also accepts auth: apiKeyAuth */ export const patchApiV1AutomationsTriggersTriggerEventId = async (args: PatchApiV1AutomationsTriggersTriggerEventIdArgs, options?: PatchApiV1AutomationsTriggersTriggerEventIdOptions) => { 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/triggers/${encodeURIComponent(String(args.triggerEventId))}${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(triggerRow, await res.json()) };