import {z} from 'zod' export type GetApiEmailsIdEventsArgs = {id: string}; export type GetApiEmailsIdEventsOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const emailEvent = z.object({id: z.number().int(), emailId: z.number().int(), eventType: z.enum(["queued", "sent", "delivered", "deferred", "bounced", "soft_bounced", "opened", "clicked", "complained", "unsubscribed", "failed"]), metadata: z.object({}).nullable().optional(), geoCountry: z.string().nullable().optional(), geoRegion: z.string().nullable().optional(), geoCity: z.string().nullable().optional(), uaClientName: z.string().nullable().optional(), uaClientType: z.string().nullable().optional(), uaOs: z.string().nullable().optional(), uaDeviceType: z.string().nullable().optional(), isPrefetch: z.boolean().optional(), createdAt: z.string()}); export const getApiEmailsIdEventsResponse = z.object({events: z.array(emailEvent)}); 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 }; /** * List an email’s events * * GET /emails/{id}/events * * Also accepts auth: bearerOAuth */ export const getApiEmailsIdEvents = async (args: GetApiEmailsIdEventsArgs, options?: GetApiEmailsIdEventsOptions) => { 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 (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/emails/${encodeURIComponent(String(args.id))}/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(getApiEmailsIdEventsResponse, await res.json()) };