import {z} from 'zod' export type GetApiDomainsCheckDeliverabilityArgs = {from: string}; export type GetApiDomainsCheckDeliverabilityOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiDomainsCheckDeliverabilityResponse = z.object({from: z.string().optional(), domain: z.string().optional(), domain_id: z.number().int().optional(), domain_verified: z.boolean().optional(), domain_status: z.string().optional(), dkim: z.object({configured: z.boolean().optional(), verified: z.boolean().optional()}).optional(), spf: z.object({configured: z.boolean().optional(), verified: z.boolean().optional()}).optional(), dmarc: z.object({configured: z.boolean().optional(), verified: z.boolean().optional()}).optional(), return_path: z.object({configured: z.boolean().optional(), verified: z.boolean().optional()}).optional(), volume_30d: z.object({}).optional(), bounce_rate: z.number().optional(), complaint_rate: z.number().optional(), warnings: z.array(z.object({severity: z.string().optional(), code: z.string().optional(), message: z.string().optional()})).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 }; /** * Check a from-address before sending * * GET /domains/check-deliverability * * Also accepts auth: bearerOAuth */ export const getApiDomainsCheckDeliverability = async (args: GetApiDomainsCheckDeliverabilityArgs, options?: GetApiDomainsCheckDeliverabilityOptions) => { 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.from != null) query.append("from", String(args.from)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/domains/check-deliverability${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(getApiDomainsCheckDeliverabilityResponse, await res.json()) };