import {z} from 'zod' export type IPv4Address = string; export type BooleanOptInFlag = boolean; export type IdentifierString = string; export type ResponseFormat = 'html' | 'json' | 'xml'; export type GetApiV1ReverseIpWhoisArgs = {ip?: IPv4Address | undefined, query?: string | undefined, country?: string | undefined, server?: ('whois.arin.net' | 'whois.apnic.net' | 'whois.ripe.net' | 'whois.lacnic.net' | 'whois.afrinic.net') | undefined, include_total_count?: BooleanOptInFlag | undefined, page?: number | undefined, ip_version?: (4 | 6) | undefined, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined, format?: ResponseFormat | undefined}; export type GetApiV1ReverseIpWhoisOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reverseIpWhoisIp = z.object({response: z.object({ip_from: z.string().optional(), ip_to: z.string().optional(), ip_from_alloc: z.string().optional(), ip_to_alloc: z.string().optional(), record_ip: z.string().optional(), record_date: z.string().optional(), server: z.string().optional(), organization: z.string().optional(), country: z.string().optional(), range: z.string().optional(), whois_record: z.string().optional()}).optional()}); export const reverseIpWhoisQuery = z.object({response: z.object({has_more_pages: z.boolean().optional(), page: z.number().int().optional(), total_count: z.number().int().optional(), record_count: z.number().int().optional(), records: z.array(z.object({ip_from: z.string().optional(), ip_to: z.string().optional(), record_ip: z.string().optional(), record_date: z.string().optional(), server: z.string().optional(), organization: z.string().optional(), country: z.string().optional(), range: z.string().optional()})).optional()}).optional()}); export const getApiV1ReverseIpWhoisResponse = z.union([reverseIpWhoisIp, reverseIpWhoisQuery]); 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 }; /** * Reverse IP WHOIS * * GET /v1/reverse-ip-whois/ * * Also accepts auth: open_key_auth, hmac_auth */ export const getApiV1ReverseIpWhois = async (args: GetApiV1ReverseIpWhoisArgs, options?: GetApiV1ReverseIpWhoisOptions) => { 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.ip != null) query.append("ip", String(args.ip)) if (args.query != null) query.append("query", String(args.query)) if (args.country != null) query.append("country", String(args.country)) if (args.server != null) query.append("server", String(args.server)) if (args.include_total_count != null) query.append("include_total_count", String(args.include_total_count)) if (args.page != null) query.append("page", String(args.page)) if (args.ip_version != null) query.append("ip_version", String(args.ip_version)) 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/reverse-ip-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(getApiV1ReverseIpWhoisResponse, await res.json()) };