import {z} from 'zod' export type CreateMonitorRequest = {term: string, match_substring_variations?: boolean | undefined, nameserver_exclusions?: Array | undefined, text_exclusions?: Array | undefined}; export type CreateApiV1IrisDetectMonitorsArgs = {body: CreateMonitorRequest}; export type CreateApiV1IrisDetectMonitorsOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const monitorDomainCounts = z.object({new: z.number().int().optional(), watched: z.number().int().optional(), escalated: z.number().int().optional(), changed: z.number().int().optional()}); export const monitor = z.object({term: z.string().optional(), match_substring_variations: z.boolean().optional(), nameserver_exclusions: z.array(z.string()).optional(), text_exclusions: z.array(z.string()).optional(), id: z.string().optional(), created_date: z.string().optional(), updated_date: z.string().optional(), state: z.enum(["active", "inactive"]).optional(), status: z.string().optional(), domain_counts: monitorDomainCounts.optional(), created_by: z.string().optional()}); export const createMonitorResponse = z.object({status: z.string().optional(), monitor: monitor.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 }; /** * Create a new monitor to track lookalike domains for a specific term. * * POST /v1/iris-detect/monitors/ * * Also accepts auth: open_key_auth, hmac_auth */ export const createApiV1IrisDetectMonitors = async (args: CreateApiV1IrisDetectMonitorsArgs, options?: CreateApiV1IrisDetectMonitorsOptions) => { const {baseUrl = "https://api.domaintools.com", fetch: fetchFn = fetch, headerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (headerAuth !== undefined) headers.set("X-Api-Key", headerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/iris-detect/monitors/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createMonitorResponse, await res.json()) };