import {z} from 'zod' export type IdentifierString = string; export type ResponseFormat = 'html' | 'json' | 'xml'; export type GetApiV1QueryWhoisArgs = {query: string, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined, format?: ResponseFormat | undefined}; export type GetApiV1QueryWhoisOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whoisLookup = z.object({response: z.object({registrant: z.string().optional(), registration: z.object({created: z.string().optional(), expires: z.string().optional(), updated: z.string().optional(), registrar: z.string().optional(), statuses: z.array(z.string()).optional()}).optional(), name_servers: z.array(z.string()).optional(), whois: z.object({date: z.string().optional(), record: z.string().optional()}).optional(), record_source: z.string().optional()}).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 }; /** * WHOIS Lookup * * GET /v1/{query}/whois/ * * Also accepts auth: open_key_auth, hmac_auth */ export const getApiV1QueryWhois = async (args: GetApiV1QueryWhoisArgs, options?: GetApiV1QueryWhoisOptions) => { 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 (args.format != null) query.append("format", String(args.format)) if (headerAuth !== undefined) headers.set("X-Api-Key", headerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.query))}/whois/${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(whoisLookup, await res.json()) };