import {z} from 'zod' export type GetApiV1AnalyticsOverviewArgs = {from?: string | undefined, to?: string | undefined, source?: string | undefined, automationId?: string | undefined, emailId?: string | undefined, audienceId?: string | undefined, triggerEventId?: string | undefined, domain?: string | undefined}; export type GetApiV1AnalyticsOverviewOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const analyticsOverviewResponse = z.object({totals: z.object({accepted: z.number().int().gte(0).optional(), sent: z.number().int().gte(0), delivered: z.number().int().gte(0), opened: z.number().int().gte(0), openedTotal: z.number().int().gte(0), clicked: z.number().int().gte(0), clickedTotal: z.number().int().gte(0), bounced: z.number().int().gte(0), complained: z.number().int().gte(0), unsubscribed: z.number().int().gte(0), failed: z.number().int().gte(0), providerSuppressed: z.number().int().gte(0).optional(), suppressed: z.number().int().gte(0), quotaSkipped: z.number().int().gte(0).optional(), preSendSkipped: z.number().int().gte(0).optional(), pending: z.number().int().gte(0).optional(), deliveryDelayed: z.number().int().gte(0)}), rates: z.object({deliveryRate: z.number().gte(0).lte(1), openRate: z.number().gte(0).lte(1), clickRate: z.number().gte(0).lte(1), bounceRate: z.number().gte(0).lte(1), complaintRate: z.number().gte(0).lte(1), unsubscribeRate: z.number().gte(0).lte(1)}), buckets: z.array(z.object({at: z.string(), sent: z.number().int().gte(0), delivered: z.number().int().gte(0), deliveryDelayed: z.number().int().gte(0), opened: z.number().int().gte(0), clicked: z.number().int().gte(0), bounced: z.number().int().gte(0), complained: z.number().int().gte(0), failed: z.number().int().gte(0), providerSuppressed: z.number().int().gte(0).optional(), suppressed: z.number().int().gte(0), quotaSkipped: z.number().int().gte(0).optional(), unsubscribed: z.number().int().gte(0)})), granularity: z.enum(["5m", "1h", "1d"]), timeZone: z.string(), range: z.object({from: z.string(), to: z.string()}), truncated: z.boolean()}); 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 }; /** * Brand overview (totals, rates, timeseries) * * GET /v1/analytics/overview * * Also accepts auth: apiKeyAuth */ export const getApiV1AnalyticsOverview = async (args: GetApiV1AnalyticsOverviewArgs, options?: GetApiV1AnalyticsOverviewOptions) => { 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.source != null) query.append("source", String(args.source)) if (args.automationId != null) query.append("automationId", String(args.automationId)) if (args.emailId != null) query.append("emailId", String(args.emailId)) if (args.audienceId != null) query.append("audienceId", String(args.audienceId)) if (args.triggerEventId != null) query.append("triggerEventId", String(args.triggerEventId)) if (args.domain != null) query.append("domain", String(args.domain)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/overview${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(analyticsOverviewResponse, await res.json()) };