import {z} from 'zod' export type GetApiDomainsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const domainStatus = z.object({domain: z.string(), verification_method: z.literal("dns_txt").optional(), txt_name: z.string().optional(), txt_value: z.string().optional(), created_at: z.number().int().optional(), verified_at: z.number().int().optional(), claimed: z.boolean(), zone_apex: z.boolean().nullable(), ownership: z.object({status: z.enum(["verified", "lapsed"]).optional(), last_checked_at: z.number().int().nullable().optional(), last_ok_at: z.number().int().nullable().optional(), lapse_started_at: z.number().int().nullable().optional()}), email: z.object({sending: z.object({status: z.enum(["not_requested", "pending_dns", "ready", "degraded", "failed", "paused"]).optional(), ready_at: z.number().int().nullable().optional(), last_checked_at: z.number().int().nullable().optional(), error: z.string().nullable().optional()}).optional(), receiving: z.object({status: z.enum(["not_requested", "pending_dns", "probing", "ready", "degraded"]).optional(), ready_at: z.number().int().nullable().optional(), last_checked_at: z.number().int().nullable().optional(), error: z.string().nullable().optional()}).optional(), mail_from: z.object({status: z.enum(["off", "pending", "ready", "failed"]).optional(), domain: z.string().nullable().optional()}).optional()}), web: z.object({status: z.enum(["not_requested", "pending", "ready", "moved"]).optional(), target: z.string().optional(), last_checked_at: z.number().int().nullable().optional()}), email_sending_ready: z.boolean(), email_receiving_ready: z.boolean(), web_ready: z.boolean(), help_desk_selectable: z.boolean(), notify_selectable: z.boolean(), records: z.array(z.object({id: z.string(), host: z.string(), type: z.enum(["TXT", "CNAME", "MX"]), value: z.string(), purpose: z.string(), status: z.enum(["ok", "propagating", "missing", "wrong", "proxied", "unreachable", "not_checked"])})), diagnosis: z.object({}).nullable()}); export const domainList = z.object({domains: z.array(domainStatus), email_setup_available: z.boolean()}); export const responseMeta = z.object({api_version: z.string().optional(), request_id: z.string().optional(), response_ms: z.number().int().optional(), timestamp: z.number().int().optional(), pools: z.object({}).optional(), billing: z.object({past_due: z.boolean().optional()}).optional(), quota: z.object({limit: z.number().int().optional(), used: z.number().int().optional(), remaining: z.number().int().optional(), resets_at: z.number().int().optional()}).optional()}); export const getApiDomainsResponse = z.object({data: domainList, meta: responseMeta.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 }; /** * List your Company Domains * * GET /domains * * Also accepts auth: BearerAuth */ export const getApiDomains = async (options?: GetApiDomainsOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/domains${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(getApiDomainsResponse, await res.json()) };