import {z} from 'zod' export type ApexDomain = string; export type IdentifierString = string; export type GetApiV1DomainRdapParsedArgs = {domain: ApexDomain, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined}; export type GetApiV1DomainRdapParsedOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const parsedDomainRdapResponse = z.object({response: z.object({domain_rdap: z.array(z.object({source: z.enum(["registry", "registrar"]), timestamp: z.string(), link: z.string(), record: z.string()})), parsed_domain_rdap: z.object({domain: z.string(), handle: z.string().optional(), domain_statuses: z.array(z.string()).optional(), creation_date: z.string().optional(), last_changed_date: z.string().optional(), expiration_date: z.string().optional(), registrar: z.object({name: z.string().optional(), iana_id: z.string().optional(), contacts: z.array(z.object({name: z.string().optional(), org: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), roles: z.array(z.enum(["abuse", "technical", "administrative", "billing"])).optional()})).optional()}).optional(), contacts: z.array(z.object({handle: z.string().optional(), name: z.string().optional(), org: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), fax: z.string().optional(), street: z.string().optional(), city: z.string().optional(), postal: z.string().optional(), region: z.string().optional(), country: z.string().optional(), roles: z.array(z.enum(["registrant", "administrative", "technical", "billing"])).optional()})).optional(), dnssec: z.object({signed: z.boolean().optional()}).optional(), nameservers: z.array(z.string()).optional(), conformance: z.array(z.string()).optional(), emails: z.array(z.string()).optional(), email_domains: z.array(z.string()).optional(), unclassified_emails: z.array(z.string()).optional()}), record_source: z.string()}).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 }; /** * Parsed Domain RDAP * * GET /v1/{domain}/rdap/parsed/ * * Also accepts auth: open_key_auth, hmac_auth */ export const getApiV1DomainRdapParsed = async (args: GetApiV1DomainRdapParsedArgs, options?: GetApiV1DomainRdapParsedOptions) => { const {baseUrl = "https://api.domaintools.com", fetch: fetchFn = fetch, headerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.app_partner != null) query.append("app_partner", String(args.app_partner)) if (args.app_name != null) query.append("app_name", String(args.app_name)) if (args.app_version != null) query.append("app_version", String(args.app_version)) if (headerAuth !== undefined) headers.set("X-Api-Key", headerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.domain))}/rdap/parsed/${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(parsedDomainRdapResponse, await res.json()) };