import {z} from 'zod' export type DnsRecordType = 'A' | 'AAAA' | 'ALIAS' | 'CAA' | 'CNAME' | 'DNSKEY' | 'DS' | 'HTTPS' | 'IPSECKEY' | 'MX' | 'NAPTR' | 'NS' | 'PTR' | 'SOA' | 'SPF' | 'SRV' | 'SSHFP' | 'SVCB' | 'TLSA' | 'TXT'; export type GetApiDomainsNameDnsRecordsArgs = {name: string, $cursor?: string | undefined, type?: Array | undefined, subName?: Array | undefined}; export type GetApiDomainsNameDnsRecordsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dnsRecordType = z.enum(["A", "AAAA", "ALIAS", "CAA", "CNAME", "DNSKEY", "DS", "HTTPS", "IPSECKEY", "MX", "NAPTR", "NS", "PTR", "SOA", "SPF", "SRV", "SSHFP", "SVCB", "TLSA", "TXT"]); export const domainDnsRecordReadonlyResponse = z.object({state: z.boolean(), reasons: z.array(z.enum(["ADMIN", "HOSTING", "DNS_PROVIDER"]))}); export const domainDnsRecordResponse = z.object({"@type": z.literal("unstoppabledomains.com/partner.v3.DomainDnsRecord"), subName: z.string().optional(), type: dnsRecordType, values: z.array(z.string()), ttl: z.number(), readonly: domainDnsRecordReadonlyResponse.optional(), id: z.string()}); export const cursorListResponseNext = z.object({$cursor: z.string().optional()}); export const domainDnsRecordsResponse = z.object({"@type": z.literal("unstoppabledomains.com/partner.v3.CursorList"), items: z.array(domainDnsRecordResponse), next: cursorListResponseNext.nullable()}); 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 DNS records * * GET /domains/{name}/dns/records */ export const getApiDomainsNameDnsRecords = async (args: GetApiDomainsNameDnsRecordsArgs, options?: GetApiDomainsNameDnsRecordsOptions) => { const {baseUrl = "https://api.unstoppabledomains.com/partner/v3", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.$cursor != null) query.append("$cursor", String(args.$cursor)) for (const item of args.type ?? []) query.append("type", String(item)) for (const item of args.subName ?? []) query.append("subName", String(item)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/domains/${encodeURIComponent(String(args.name))}/dns/records${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(domainDnsRecordsResponse, await res.json()) };