import {z} from 'zod' export type GetApiEmailsIdArgs = {id: string}; export type GetApiEmailsIdOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const spamSymbol = z.object({name: z.string(), score: z.number(), description: z.string().optional()}); 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 getApiEmailsIdResponse = z.object({email: z.object({id: z.number().int(), publicId: z.string(), domainId: z.number().int().nullable().optional(), fromAddress: z.string(), fromName: z.string().nullable().optional(), toAddresses: z.array(z.string()), ccAddresses: z.array(z.string()).nullable().optional(), bccAddresses: z.array(z.string()).nullable().optional(), replyTo: z.array(z.string()).nullable().optional(), subject: z.string().optional(), headers: z.object({}).nullable().optional(), tags: z.array(z.string()).nullable().optional(), status: z.enum(["queued", "sending", "deferred", "delivered", "bounced", "complained", "failed"]), testMode: z.boolean().optional(), recipientProvider: z.string().nullable().optional(), unsubscribeEnabled: z.boolean().optional(), attachmentCount: z.number().int().optional(), spamScore: z.string().nullable().optional(), spamWarnings: z.array(spamSymbol).nullable().optional(), scheduledAt: z.string().nullable().optional(), sentAt: z.string().nullable().optional(), createdAt: z.string(), htmlBody: z.string().nullable().optional(), textBody: z.string().nullable().optional(), attachments: z.array(z.object({filename: z.string(), content_type: z.string().nullable(), size: z.number().int()})).nullable().optional(), events: z.array(emailEvent).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 }; /** * Get an email * * GET /emails/{id} * * Also accepts auth: bearerOAuth */ export const getApiEmailsId = async (args: GetApiEmailsIdArgs, options?: GetApiEmailsIdOptions) => { 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))}${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(getApiEmailsIdResponse, await res.json()) };