import {z} from 'zod' export type CIA = 'Confidentiality' | 'Integrity' | 'Availability'; export type Treatment = 'Mitigate' | 'Transfer' | 'Avoid' | 'Accept'; export type CustomAttribute = {label: string, value: string | Array}; export type RiskScenarioType = 'Risk Scenario' | 'Enterprise Risk'; export type UpdateRiskScenarioInput = {description?: string | undefined, detailedDescription?: string | undefined, isSensitive?: boolean | undefined, likelihood?: number | undefined, impact?: number | undefined, residualLikelihood?: number | undefined, residualImpact?: number | undefined, categories?: Array | undefined, ciaCategories?: Array | undefined, treatment?: Treatment | undefined, owner?: (string | null) | undefined, note?: string | undefined, riskRegister?: string | undefined, customFields?: Array | undefined, type?: RiskScenarioType | undefined, identificationDate?: string | undefined}; export type PatchApiRiskScenariosRiskScenarioIdArgs = {riskScenarioId: string, body: UpdateRiskScenarioInput}; export type PatchApiRiskScenariosRiskScenarioIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cIA = z.enum(["Confidentiality", "Integrity", "Availability"]); export const treatment = z.enum(["Mitigate", "Transfer", "Avoid", "Accept"]); export const customAttribute = z.object({label: z.string(), value: z.union([z.string(), z.array(z.string())])}); export const reviewStatus = z.enum(["APPROVED", "DRAFT", "NOT_REVIEWED", "AWAITING_SUBMISSION", "PENDING_APPROVAL", "REQUESTED_CHANGES"]); export const riskScenarioType = z.enum(["Risk Scenario", "Enterprise Risk"]); export const riskScenario = z.object({riskId: z.string(), description: z.string(), detailedDescription: z.string().nullable().optional(), isSensitive: z.boolean().nullable(), likelihood: z.number().int().nullable(), impact: z.number().int().nullable(), residualLikelihood: z.number().int().nullable(), residualImpact: z.number().int().nullable(), categories: z.array(z.string()), ciaCategories: z.array(cIA), treatment: treatment.nullable(), owner: z.string().nullable(), note: z.string().nullable(), riskRegister: z.string().nullable(), customFields: z.array(customAttribute), isArchived: z.boolean(), reviewStatus: reviewStatus, requiredApprovers: z.array(z.string()), type: riskScenarioType, identificationDate: z.string()}); 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 }; /** * Update risk scenario * * PATCH /risk-scenarios/{riskScenarioId} */ export const patchApiRiskScenariosRiskScenarioId = async (args: PatchApiRiskScenariosRiskScenarioIdArgs, options?: PatchApiRiskScenariosRiskScenarioIdOptions) => { const {baseUrl = "https://api.vanta.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/risk-scenarios/${encodeURIComponent(String(args.riskScenarioId))}${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(riskScenario, await res.json()) };