import {z} from 'zod' export type BasicString = string; export type IdentifierString = string; export type ResponseFormat = 'html' | 'json' | 'xml'; export type GetApiV1RegistrantAlertArgs = {query: BasicString, exclude?: BasicString | undefined, days_back?: number | undefined, limit?: number | undefined, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined, format?: ResponseFormat | undefined}; export type GetApiV1RegistrantAlertOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const registrantMonitor = z.object({response: z.object({query: z.string().optional(), limit: z.number().int().optional(), total: z.number().int().optional(), date: z.string().optional(), alerts: z.array(z.object({domain: z.string().optional(), match_type: z.enum(["added", "removed"]).optional(), current_owner: z.string().optional(), created: z.string().optional(), modified: z.string().optional(), last_owner: 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 }; /** * Registrant Monitor * * GET /v1/registrant-alert/ * * Also accepts auth: open_key_auth, hmac_auth */ export const getApiV1RegistrantAlert = async (args: GetApiV1RegistrantAlertArgs, options?: GetApiV1RegistrantAlertOptions) => { 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.query != null) query.append("query", String(args.query)) if (args.exclude != null) query.append("exclude", String(args.exclude)) if (args.days_back != null) query.append("days_back", String(args.days_back)) if (args.limit != null) query.append("limit", String(args.limit)) 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/registrant-alert/${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(registrantMonitor, await res.json()) };