import {z} from 'zod' export type GetApiBroadcastsPerformanceArgs = {metric?: ('open_rate' | 'click_rate' | 'delivered') | undefined, since?: string | undefined, limit?: number | undefined}; export type GetApiBroadcastsPerformanceOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const broadcastPerformance = z.object({id: z.string().optional(), subject: z.string().optional(), status: z.string().optional(), sent_at: z.string().nullable().optional(), total_recipients: z.number().int().optional(), delivered: z.number().int().optional(), opened: z.number().int().optional(), clicked: z.number().int().optional(), bounced: z.number().int().optional(), open_rate: z.number().optional(), click_rate: z.number().optional()}); export const getApiBroadcastsPerformanceResponse = z.object({metric: z.string().optional(), since: z.string().nullable().optional(), broadcasts: z.array(broadcastPerformance).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 }; /** * Rank sent broadcasts by a metric * * GET /broadcasts/performance * * Also accepts auth: bearerOAuth */ export const getApiBroadcastsPerformance = async (args: GetApiBroadcastsPerformanceArgs, options?: GetApiBroadcastsPerformanceOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.metric != null) query.append("metric", String(args.metric)) if (args.since != null) query.append("since", String(args.since)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/broadcasts/performance${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(getApiBroadcastsPerformanceResponse, await res.json()) };