import {z} from 'zod' export type GetApiV1AnalyticsEventsArgs = {from?: string | undefined, to?: string | undefined, recipientEmail?: string | undefined, eventType?: string | undefined, automationId?: string | undefined, sendId?: string | undefined, source?: string | undefined, audienceId?: string | undefined, emailId?: string | undefined, domain?: string | undefined, triggerEventId?: string | undefined, includeMachineClicks?: boolean | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiV1AnalyticsEventsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eventsAnalyticsResponse = z.object({data: z.array(z.object({id: z.string().min(1), occurredAt: z.string(), domain: z.enum(["email", "automation", "trigger", "inbound"]), eventType: z.string().min(1), recipientEmail: z.string().optional(), sendId: z.string().optional(), emailId: z.string().optional(), emailName: z.string().optional(), automationId: z.string().optional(), automationName: z.string().optional(), nodeId: z.string().optional(), triggerEventId: z.string().optional(), provider: z.string().optional(), mode: z.enum(["live", "test"]).optional(), summary: z.string().optional(), sendSource: z.enum(["audience", "api", "automation_manual", "automation_integration", "automation_custom"]).optional(), sendContext: z.string().optional(), triggerProvider: z.string().optional(), triggerTitle: z.string().optional()})), pagination: z.object({limit: z.number().int().gte(1).lte(100), cursor: z.string().nullable(), hasMore: z.boolean()}), range: z.object({from: z.string(), to: 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 }; /** * Unified events feed * * GET /v1/analytics/events * * Also accepts auth: apiKeyAuth */ export const getApiV1AnalyticsEvents = async (args: GetApiV1AnalyticsEventsArgs, options?: GetApiV1AnalyticsEventsOptions) => { const {baseUrl = "https://brew.new/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.recipientEmail != null) query.append("recipientEmail", String(args.recipientEmail)) if (args.eventType != null) query.append("eventType", String(args.eventType)) if (args.automationId != null) query.append("automationId", String(args.automationId)) if (args.sendId != null) query.append("sendId", String(args.sendId)) if (args.source != null) query.append("source", String(args.source)) if (args.audienceId != null) query.append("audienceId", String(args.audienceId)) if (args.emailId != null) query.append("emailId", String(args.emailId)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.triggerEventId != null) query.append("triggerEventId", String(args.triggerEventId)) if (args.includeMachineClicks != null) query.append("includeMachineClicks", String(args.includeMachineClicks)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/events${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(eventsAnalyticsResponse, await res.json()) };