import {z} from 'zod' export type CreateApiDomainsIdVerifyArgs = {id: string}; export type CreateApiDomainsIdVerifyOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dnsRecord = z.object({id: z.number().int(), domainId: z.number().int().optional(), recordType: z.enum(["TXT", "CNAME", "MX", "SRV"]), name: z.string(), value: z.string(), purpose: z.enum(["spf", "dkim", "dkim_rotation", "dmarc", "return_path", "mx", "tracking", "bimi", "imap", "submission", "postmaster"]), verified: z.boolean(), lastCheckedAt: z.string().nullable().optional(), lastCheckCode: z.string().nullable().optional(), lastCheckHint: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const domain = z.object({id: z.number().int(), publicId: z.string(), name: z.string(), region: z.enum(["eu-west-1", "us-east-1", "sa-east-1", "ap-northeast-1"]).optional(), status: z.enum(["pending", "verified", "failed"]), dkimSelector: z.string().nullable().optional(), dkimPublicKey: z.string().nullable().optional(), customReturnPath: z.string().nullable().optional(), openTracking: z.boolean().optional(), clickTracking: z.boolean().optional(), trackingDomain: z.string().nullable().optional(), tlsMode: z.enum(["opportunistic", "enforced"]).optional(), inboundEnabled: z.boolean().optional(), catchAll: z.boolean().optional(), catchAllMailboxId: z.number().int().nullable().optional(), bimiLogoUrl: z.string().nullable().optional(), ipAddressId: z.number().int().nullable().optional(), verifiedAt: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), dnsRecords: z.array(dnsRecord).optional()}); export const createApiDomainsIdVerifyResponse = z.object({queued: z.literal(true), domain: domain}); 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 }; /** * Queue a DNS verification * * POST /domains/{id}/verify * * Also accepts auth: bearerOAuth */ export const createApiDomainsIdVerify = async (args: CreateApiDomainsIdVerifyArgs, options?: CreateApiDomainsIdVerifyOptions) => { 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(/\/+$/, '')}/domains/${encodeURIComponent(String(args.id))}/verify${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiDomainsIdVerifyResponse, await res.json()) };