import {z} from 'zod' export type PageSize = number; export type PageCursor = string; export type RiskOrderBy = 'inherentRisk' | 'riskId' | 'riskScenario' | 'treatment' | 'residualRisk' | 'reviewStatus' | 'owner' | 'categories' | 'ciaCategories' | 'identified'; export type OrderDirection = 'asc' | 'desc'; export type GetApiAuditsAuditIdRisksSnapshotIdArgs = {auditId: string, snapshotId: string, pageSize?: PageSize | undefined, pageCursor?: PageCursor | undefined, search?: string | undefined, orderBy?: RiskOrderBy | undefined, orderDirection?: OrderDirection | undefined}; export type GetApiAuditsAuditIdRisksSnapshotIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const auditRiskTreatment = z.enum(["ACCEPT", "AVOID", "MITIGATE", "TRANSFER"]); export const auditRiskTreatmentStatus = z.enum(["IN_PROGRESS", "DONE"]); export const auditRiskReviewStatus = z.enum(["APPROVED", "DRAFT", "NOT_REVIEWED", "AWAITING_SUBMISSION", "PENDING_APPROVAL", "REQUESTED_CHANGES"]); export const auditRiskOwner = z.object({displayName: z.string(), imageUrl: z.string().nullable()}); export const auditRiskCia = z.enum(["AVAILABILITY", "CONFIDENTIALITY", "INTEGRITY"]); export const auditRisk = z.object({id: z.string(), riskId: z.string().nullable(), riskScenario: z.string().nullable().optional(), likelihood: z.number().nullable().optional(), impact: z.number().nullable().optional(), inherentRisk: z.number().nullable().optional(), treatment: auditRiskTreatment.nullable().optional(), treatmentStatus: auditRiskTreatmentStatus.nullable().optional(), residualRisk: z.number().nullable().optional(), reviewStatus: auditRiskReviewStatus.nullable().optional(), owner: auditRiskOwner.nullable().optional(), categories: z.array(z.string()).optional(), ciaCategories: z.array(auditRiskCia).optional(), linkedControlIds: z.array(z.string()).optional(), identified: z.string().nullable().optional()}); export const pageInfo = z.object({endCursor: z.string().nullable(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().nullable()}); export const paginatedResponseAuditRisk = z.object({results: z.object({data: z.array(auditRisk), pageInfo: pageInfo})}); 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 }; /** * List risks for an audit * * GET /audits/{auditId}/risks/{snapshotId} */ export const getApiAuditsAuditIdRisksSnapshotId = async (args: GetApiAuditsAuditIdRisksSnapshotIdArgs, options?: GetApiAuditsAuditIdRisksSnapshotIdOptions) => { const {baseUrl = "https://api.vanta.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageCursor != null) query.append("pageCursor", String(args.pageCursor)) if (args.search != null) query.append("search", String(args.search)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.orderDirection != null) query.append("orderDirection", String(args.orderDirection)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/audits/${encodeURIComponent(String(args.auditId))}/risks/${encodeURIComponent(String(args.snapshotId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseAuditRisk, await res.json()) };