import {z} from 'zod' export type CreateApiThreatmonitoringThreatCloseArgs = {uuid: string, reason: 'false_positive' | 'risk_accepted' | 'remediated', justification?: string | undefined, remediation_reason?: ('content_removed_or_privatised' | 'affected_users_credentials_reset' | 'system_patched' | 'requested_takedown' | 'compensating_controls' | 'other') | undefined, remediation_description?: string | undefined}; export type CreateApiThreatmonitoringThreatCloseOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const closeThreatV1ResponsePayloadBody = z.object({reason: z.string().optional(), status: z.string().optional(), uuid: z.string().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 }; /** * Close threat * * POST /threatmonitoring/threat/close */ export const createApiThreatmonitoringThreatClose = async (args: CreateApiThreatmonitoringThreatCloseArgs, options?: CreateApiThreatmonitoringThreatCloseOptions) => { const {baseUrl = "https://cyber-risk.upguard.com/api/public", fetch: fetchFn = fetch, APIKeyInHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.uuid != null) query.append("uuid", String(args.uuid)) if (args.reason != null) query.append("reason", String(args.reason)) if (args.justification != null) query.append("justification", String(args.justification)) if (args.remediation_reason != null) query.append("remediation_reason", String(args.remediation_reason)) if (args.remediation_description != null) query.append("remediation_description", String(args.remediation_description)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/threatmonitoring/threat/close${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(closeThreatV1ResponsePayloadBody, await res.json()) };