import {z} from 'zod' export type PageSize = number; export type PageCursor = string; export type IssueStatus = 'NOT_STARTED' | 'IN_PROGRESS' | 'CLOSED'; export type IssueSeverity = 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW' | 'NO_SEVERITY'; export type SourceType = 'AUDIT' | 'AUDIT_EXTERNAL' | 'INCIDENT' | 'EXTERNAL_PARTY' | 'SELF_ASSESSMENT' | 'OTHER'; export type StandardIssueType = 'AREA_OF_CONCERN' | 'MAJOR_NONCONFORMITY' | 'MINOR_NONCONFORMITY' | 'OPP_FOR_IMPROVEMENT' | 'EXCEPTION' | 'PROCESS_FOR_IMPROVEMENT'; export type IssueTemplate = 'STANDARD_ISSUE' | 'STANDARD_POAM'; export type ClosedReason = 'RESOLVED' | 'DUPLICATE' | 'ACCEPTED' | 'OTHER'; export type IssueSortField = 'dueDate' | 'createdDate' | 'detectedDate' | 'lastModifiedDate' | 'status' | 'severity'; export type OrderDirection = 'asc' | 'desc'; export type GetApiIssuesArgs = {pageSize?: PageSize | undefined, pageCursor?: PageCursor | undefined, search?: string | undefined, readableIssueIdMatchesAny?: Array | undefined, statusMatchesAny?: Array | undefined, severityMatchesAny?: Array | undefined, sourceMatchesAny?: Array | undefined, typeMatchesAny?: Array | undefined, ownerIdMatchesAny?: Array | undefined, templateMatchesAny?: Array | undefined, closeReasonMatchesAny?: Array | undefined, closedAfterDate?: string | undefined, closedBeforeDate?: string | undefined, includeIssuesWithoutDueDate?: boolean | undefined, includeOnlyIssuesWithoutDueDate?: boolean | undefined, dueAfterDate?: string | undefined, dueBeforeDate?: string | undefined, detectedAfterDate?: string | undefined, detectedBeforeDate?: string | undefined, createdAfterDate?: string | undefined, createdBeforeDate?: string | undefined, auditIdMatchesAny?: Array | undefined, controlIdMatchesAny?: Array | undefined, orderBy?: IssueSortField | undefined, orderDirection?: OrderDirection | undefined}; export type GetApiIssuesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const actorType = z.enum(["USER", "WORKFLOW_GENERATED"]); export const actor = z.object({actorType: actorType, actorId: z.string()}); export const ownerType = z.enum(["USER", "TEAM"]); export const issueOwner = z.object({ownerType: ownerType, ownerId: z.string()}); export const issueSeverity = z.enum(["CRITICAL", "HIGH", "MEDIUM", "LOW", "NO_SEVERITY"]); export const issueStatus = z.enum(["NOT_STARTED", "IN_PROGRESS", "CLOSED"]); export const sourceType = z.enum(["AUDIT", "AUDIT_EXTERNAL", "INCIDENT", "EXTERNAL_PARTY", "SELF_ASSESSMENT", "OTHER"]); export const source = z.object({sourceType: sourceType, sourceId: z.string().optional()}); export const closedReason = z.enum(["RESOLVED", "DUPLICATE", "ACCEPTED", "OTHER"]); export const closedMetadata = z.object({reason: closedReason, comment: z.string(), closedAt: z.string()}); export const issueCustomField = z.object({label: z.string(), value: z.union([z.string(), z.array(z.string())])}); export const extractIssueTemplateSTANDARDISSUE = z.literal("STANDARD_ISSUE"); export const standardIssueType = z.enum(["AREA_OF_CONCERN", "MAJOR_NONCONFORMITY", "MINOR_NONCONFORMITY", "OPP_FOR_IMPROVEMENT", "EXCEPTION", "PROCESS_FOR_IMPROVEMENT"]); export const standardIssue = z.object({id: z.string(), readableIssueId: z.string(), createdDate: z.string(), createdBy: actor, lastModifiedBy: actor, lastModifiedDate: z.string(), title: z.string(), description: z.string(), owners: z.array(issueOwner), severity: issueSeverity, status: issueStatus, rootCause: z.string().nullable(), correctiveAction: z.string().nullable(), dueDate: z.string().nullable(), source: source.nullable(), controlDomain: z.string().nullable(), closedMetadata: closedMetadata.nullable(), detectedDate: z.string(), mappedControlIds: z.array(z.string()), mappedRiskScenarioIds: z.array(z.string()), mappedPolicyIds: z.array(z.string()), customFields: z.array(issueCustomField), template: extractIssueTemplateSTANDARDISSUE, type: standardIssueType.nullable()}); export const extractIssueTemplateSTANDARDPOAM = z.literal("STANDARD_POAM"); export const standardPOAM = z.object({id: z.string(), readableIssueId: z.string(), createdDate: z.string(), createdBy: actor, lastModifiedBy: actor, lastModifiedDate: z.string(), title: z.string(), description: z.string(), owners: z.array(issueOwner), severity: issueSeverity, status: issueStatus, rootCause: z.string().nullable(), correctiveAction: z.string().nullable(), dueDate: z.string().nullable(), source: source.nullable(), controlDomain: z.string().nullable(), closedMetadata: closedMetadata.nullable(), detectedDate: z.string(), mappedControlIds: z.array(z.string()), mappedRiskScenarioIds: z.array(z.string()), mappedPolicyIds: z.array(z.string()), customFields: z.array(issueCustomField), template: extractIssueTemplateSTANDARDPOAM, requiredResources: z.string().nullable(), systemsDescription: z.string().nullable()}); export const issue = z.union([standardIssue, standardPOAM]); export const pageInfo = z.object({endCursor: z.string().nullable(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().nullable()}); export const paginatedResponseIssue = z.object({results: z.object({data: z.array(issue), 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 issues * * GET /issues */ export const getApiIssues = async (args: GetApiIssuesArgs, options?: GetApiIssuesOptions) => { 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)) for (const item of args.readableIssueIdMatchesAny ?? []) query.append("readableIssueIdMatchesAny", String(item)) for (const item of args.statusMatchesAny ?? []) query.append("statusMatchesAny", String(item)) for (const item of args.severityMatchesAny ?? []) query.append("severityMatchesAny", String(item)) for (const item of args.sourceMatchesAny ?? []) query.append("sourceMatchesAny", String(item)) for (const item of args.typeMatchesAny ?? []) query.append("typeMatchesAny", String(item)) for (const item of args.ownerIdMatchesAny ?? []) query.append("ownerIdMatchesAny", String(item)) for (const item of args.templateMatchesAny ?? []) query.append("templateMatchesAny", String(item)) for (const item of args.closeReasonMatchesAny ?? []) query.append("closeReasonMatchesAny", String(item)) if (args.closedAfterDate != null) query.append("closedAfterDate", String(args.closedAfterDate)) if (args.closedBeforeDate != null) query.append("closedBeforeDate", String(args.closedBeforeDate)) if (args.includeIssuesWithoutDueDate != null) query.append("includeIssuesWithoutDueDate", String(args.includeIssuesWithoutDueDate)) if (args.includeOnlyIssuesWithoutDueDate != null) query.append("includeOnlyIssuesWithoutDueDate", String(args.includeOnlyIssuesWithoutDueDate)) if (args.dueAfterDate != null) query.append("dueAfterDate", String(args.dueAfterDate)) if (args.dueBeforeDate != null) query.append("dueBeforeDate", String(args.dueBeforeDate)) if (args.detectedAfterDate != null) query.append("detectedAfterDate", String(args.detectedAfterDate)) if (args.detectedBeforeDate != null) query.append("detectedBeforeDate", String(args.detectedBeforeDate)) if (args.createdAfterDate != null) query.append("createdAfterDate", String(args.createdAfterDate)) if (args.createdBeforeDate != null) query.append("createdBeforeDate", String(args.createdBeforeDate)) for (const item of args.auditIdMatchesAny ?? []) query.append("auditIdMatchesAny", String(item)) for (const item of args.controlIdMatchesAny ?? []) query.append("controlIdMatchesAny", String(item)) 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(/\/+$/, '')}/issues${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(paginatedResponseIssue, await res.json()) };