import {z} from 'zod' export type GetApiInboundIdArgs = {id: number}; export type GetApiInboundIdOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const inboundEmail = z.object({id: z.number().int(), domainId: z.number().int().optional(), fromAddress: z.string(), fromName: z.string().nullable().optional(), toAddress: z.string(), toAddresses: z.array(z.string()).nullable().optional(), ccAddresses: z.array(z.string()).nullable().optional(), messageId: z.string().nullable().optional(), subject: z.string().nullable().optional(), htmlBody: z.string().nullable().optional(), textBody: z.string().nullable().optional(), headers: z.object({}).nullable().optional(), mailboxHash: z.string().nullable().optional(), spamScore: z.number().nullable().optional(), spamVerdict: z.string().nullable().optional(), strippedReply: z.string().nullable().optional(), replyCount: z.number().int().optional(), createdAt: z.string()}); export const inboundReplySummary = z.object({id: z.string(), subject: z.string(), toAddresses: z.array(z.string()), status: z.string(), testMode: z.boolean(), createdAt: z.string()}); export const getApiInboundIdResponse = z.object({inboundEmail: inboundEmail, replies: z.array(inboundReplySummary)}); 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 a received email * * GET /inbound/{id} * * Also accepts auth: bearerOAuth */ export const getApiInboundId = async (args: GetApiInboundIdArgs, options?: GetApiInboundIdOptions) => { 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(/\/+$/, '')}/inbound/${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(getApiInboundIdResponse, await res.json()) };