import {z} from 'zod' export type GetApiTldsTldDnsSecurityArgs = {tld: string}; export type GetApiTldsTldDnsSecurityOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dnssecAlgorithm = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(10), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(17), z.literal(23), z.literal(252), z.literal(253), z.literal(254)]); export const tldDnsSecurityConfigurationDnssecAlgorithmResponse = z.object({id: dnssecAlgorithm, name: z.string()}); export const dnssecDigestType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)]); export const tldDnsSecurityConfigurationDnssecDigestTypeResponse = z.object({id: dnssecDigestType, name: z.string()}); export const tldDnsSecurityConfigurationDnssecResponse = z.object({supported: z.boolean(), algorithms: z.array(tldDnsSecurityConfigurationDnssecAlgorithmResponse), digestTypes: z.array(tldDnsSecurityConfigurationDnssecDigestTypeResponse)}); export const tldDnsSecurityConfigurationResponse = z.object({"@type": z.literal("unstoppabledomains.com/partner.v3.TldDnsSecurityConfiguration"), dnssec: tldDnsSecurityConfigurationDnssecResponse}); 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 TLD DNSSEC configuration * * GET /tlds/{tld}/dns/security */ export const getApiTldsTldDnsSecurity = async (args: GetApiTldsTldDnsSecurityArgs, options?: GetApiTldsTldDnsSecurityOptions) => { 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 (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/tlds/${encodeURIComponent(String(args.tld))}/dns/security${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(tldDnsSecurityConfigurationResponse, await res.json()) };