import {z} from 'zod' export type ControlDomain = 'ARTIFICIAL_&_AUTONOMOUS_TECHNOLOGY' | 'ASSET_MANAGEMENT' | 'BUSINESS_CONTINUITY_&_DISASTER_RECOVERY' | 'CAPACITY_&_PERFORMANCE_PLANNING' | 'CHANGE_MANAGEMENT' | 'CLOUD_SECURITY' | 'COMPLIANCE' | 'CONFIGURATION_MANAGEMENT' | 'CONTINUOUS_MONITORING' | 'CRYPTOGRAPHIC_PROTECTIONS' | 'DATA_CLASSIFICATION_&_HANDLING' | 'EMBEDDED_TECHNOLOGY' | 'ENDPOINT_SECURITY' | 'HUMAN_RESOURCES_SECURITY' | 'IDENTIFICATION_&_AUTHENTICATION' | 'INCIDENT_RESPONSE' | 'INFORMATION_ASSURANCE' | 'MAINTENANCE' | 'MOBILE_DEVICE_MANAGEMENT' | 'NETWORK SECURITY' | 'PHYSICAL_&_ENVIRONMENTAL_SECURITY' | 'PRIVACY' | 'PROJECT_&_RESOURCE MANAGEMENT' | 'RISK_MANAGEMENT' | 'SECURE_ENGINEERING_&_ARCHITECTURE' | 'SECURITY_AWARENESS_&_TRAINING' | 'SECURITY_OPERATIONS' | 'SECURITY_&_PRIVACY_GOVERNANCE' | 'TECHNOLOGY_DEVELOPMENT_&_ACQUISITION' | 'THIRD-PARTY_MANAGEMENT' | 'THREAT_MANAGEMENT' | 'VULNERABILITY_&_PATCH_MANAGEMENT' | 'WEB_SECURITY' | 'ADMINISTRATIVE' | 'PHYSICAL' | 'TECHNICAL' | 'BASIC' | 'DERIVED'; export type CustomField = {label: string, value: string | Array}; export type EditControlMetadataInput = {name?: string | undefined, externalId?: string | undefined, description?: string | undefined, domain?: ControlDomain | undefined, note?: string | undefined, customFields?: Array | undefined}; export type PatchApiControlsControlIdArgs = {controlId: string, body: EditControlMetadataInput}; export type PatchApiControlsControlIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const controlSource = z.enum(["Vanta", "Custom"]); export const owner = z.object({id: z.string(), displayName: z.string(), emailAddress: z.string()}); export const customField = z.object({label: z.string(), value: z.union([z.string(), z.array(z.string())])}); export const controlStatus = z.enum(["NO_EVIDENCE_MAPPED", "NOT_STARTED", "IN_PROGRESS", "COMPLETED"]); export const controlDetail = z.object({id: z.string(), externalId: z.string().nullable(), name: z.string(), description: z.string(), source: controlSource, domains: z.array(z.string()), owner: owner.nullable(), role: z.string().nullable().optional(), customFields: z.array(customField), creationDate: z.string().nullable(), modificationDate: z.string().nullable(), numDocumentsPassing: z.number(), numDocumentsTotal: z.number(), numTestsPassing: z.number(), numTestsTotal: z.number(), status: controlStatus, note: z.string().nullable()}); 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 a control's metadata * * PATCH /controls/{controlId} */ export const patchApiControlsControlId = async (args: PatchApiControlsControlIdArgs, options?: PatchApiControlsControlIdOptions) => { 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(/\/+$/, '')}/controls/${encodeURIComponent(String(args.controlId))}${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(controlDetail, await res.json()) };