import {z} from 'zod' export type IdentifierString = string; export type EscalationTypeEnum = 'blocked' | 'google_safe'; export type WatchlistEscalation = {watchlist_domain_ids?: Array | undefined, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined, escalation_type?: EscalationTypeEnum | undefined}; export type CreateApiV1IrisDetectEscalationsArgs = {body: WatchlistEscalation}; export type CreateApiV1IrisDetectEscalationsOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const escalations = z.object({watchlist_domains: z.array(z.object({state: z.string().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 }; /** * Escalate internally and externally. * * POST /v1/iris-detect/escalations/ * * Also accepts auth: open_key_auth, hmac_auth */ export const createApiV1IrisDetectEscalations = async (args: CreateApiV1IrisDetectEscalationsArgs, options?: CreateApiV1IrisDetectEscalationsOptions) => { 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/escalations/${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(escalations, await res.json()) };