import {z} from 'zod' export type ApexDomain = string; export type IdentifierString = string; export type ResponseFormat = 'html' | 'json' | 'xml'; export type GetApiV1DomainArgs = {domain: ApexDomain, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined, format?: ResponseFormat | undefined}; export type GetApiV1DomainOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const domainProfile = z.object({response: z.object({registrant: z.object({name: z.string().optional(), domain: z.number().int().optional(), domains: z.number().int().optional(), product_url: z.string().optional()}).optional(), server: z.object({ip_address: z.string().optional(), other_domains: z.number().int().optional(), product_url: z.string().optional()}).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.object({server: z.string().optional(), product_url: z.string().optional()})).optional(), history: z.object({registrar: z.object({earliest_event: z.string().optional(), events: z.number().int().optional(), product_url: z.string().optional()}).optional(), name_server: z.object({events: z.number().int().optional(), timespan_in_years: z.number().int().optional(), product_url: z.string().optional()}).optional(), ip_address: z.object({events: z.number().int().optional(), timespan_in_years: z.number().int().optional(), product_url: z.string().optional()}).optional(), whois: z.object({records: z.number().int().optional(), earliest_event: z.string().optional(), product_url: z.string().optional()}).optional()}).optional(), seo: z.object({score: z.union([z.number().int(), z.string()]).optional(), product_url: z.string().optional()}).optional(), website_data: z.object({response_code: z.union([z.number().int(), z.string()]).optional(), title: z.string().optional(), server: z.string().optional(), meta: z.array(z.string()).optional(), product_url: z.string().optional()}).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 }; /** * Domain Profile * * GET /v1/{domain}/ * * Also accepts auth: open_key_auth, hmac_auth */ export const getApiV1Domain = async (args: GetApiV1DomainArgs, options?: GetApiV1DomainOptions) => { 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.domain))}/${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(domainProfile, await res.json()) };