import {z} from 'zod' export type GetApiPoliciesPolicyIdArgs = {policyId: string}; export type GetApiPoliciesPolicyIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const policyStatus = z.enum(["OK", "NEEDS_REMEDIATION"]); export const policyVersionStatus = z.enum(["NOT_STARTED", "DRAFT", "PENDING_APPROVAL", "APPROVED", "RENEW_SOON", "EXPIRED"]); export const policyLatestVersion = z.object({status: policyVersionStatus}); export const policyLocale = z.enum(["CS", "CY", "DA", "DE", "EN", "ES", "FI", "FR", "HU", "IS", "IT", "JA", "KO", "NL", "NO", "NOT_SPECIFIED", "PL", "PT", "ET", "RO", "AR", "SK", "SV", "TR", "ZH"]); export const policyDocument = z.object({language: policyLocale.nullable(), slugId: z.string(), url: z.string()}); export const policyLatestApprovedVersion = z.object({versionId: z.string(), documents: z.array(policyDocument)}); export const policy = z.object({id: z.string(), name: z.string(), description: z.string(), status: policyStatus, approvedAtDate: z.string().nullable(), latestVersion: policyLatestVersion, latestApprovedVersion: policyLatestApprovedVersion.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 }; /** * Get policy by ID * * GET /policies/{policyId} */ export const getApiPoliciesPolicyId = async (args: GetApiPoliciesPolicyIdArgs, options?: GetApiPoliciesPolicyIdOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/policies/${encodeURIComponent(String(args.policyId))}${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(policy, await res.json()) };