import {z} from 'zod' export type IdentifierString = string; export type WatchlistState = {watchlist_domain_ids?: Array | undefined, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined, state?: ('watched' | 'ignored') | undefined}; export type PatchApiV1IrisDetectDomainsArgs = {body: WatchlistState}; export type PatchApiV1IrisDetectDomainsOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const watchlist = z.object({watchlist_domains: z.array(z.object({state: z.enum(["watched", "ignored"]).optional(), domain: z.string().optional(), discovered_date: z.string().optional(), changed_date: z.string().optional(), id: z.string().optional(), assigned_by: z.string().optional(), assigned_date: 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 }; /** * Add and remove domains from Watchlist or Ignored lists. * * PATCH /v1/iris-detect/domains/ * * Also accepts auth: open_key_auth, hmac_auth */ export const patchApiV1IrisDetectDomains = async (args: PatchApiV1IrisDetectDomainsArgs, options?: PatchApiV1IrisDetectDomainsOptions) => { 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/domains/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(watchlist, await res.json()) };