import {z} from 'zod' export type PageSize = number; export type PageCursor = string; export type KnowledgeBaseResourceTypeFilter = 'FILE' | 'URL'; export type GetApiKnowledgeBaseResourcesArgs = {pageSize?: PageSize | undefined, pageCursor?: PageCursor | undefined, q?: string | undefined, typeMatchesAny?: Array | undefined, lastUpdatedAfter?: string | undefined, lastUpdatedBefore?: string | undefined, matchesTags?: string | undefined, expiresBefore?: string | undefined, expiresAfter?: string | undefined}; export type GetApiKnowledgeBaseResourcesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const knowledgeBaseResourceActorAssignment = z.object({type: z.enum(["User", "Team"]), id: z.string(), displayName: z.string().nullable()}); export const knowledgeBaseExpirationStatus = z.enum(["CURRENT", "EXPIRED"]); export const tagInput = z.object({categoryId: z.string(), tagId: z.string()}); export const knowledgeBaseDocumentResourceOutput = z.object({id: z.string(), type: z.literal("FILE"), title: z.string(), description: z.string().nullable(), fileUrl: z.string(), customerVisibility: z.enum(["PRIVATE", "SHAREABLE", "REQUEST_ACCESS", "PUBLIC", "null"]).nullable(), downloadPermission: z.enum(["VIEW_ONLY", "VIEW_AND_DOWNLOAD", "null"]).nullable(), isUsedInQuestionnaires: z.boolean().nullable(), ownerAssignment: knowledgeBaseResourceActorAssignment.nullable(), expirationStatus: knowledgeBaseExpirationStatus, expirationDate: z.string().nullable(), lastUpdated: z.string(), lastVerified: z.string().nullable(), tags: z.array(tagInput), categoryId: z.string().nullable()}); export const knowledgeBaseWebpageResourceOutput = z.object({id: z.string(), type: z.literal("URL"), title: z.string(), description: z.string().nullable(), url: z.string(), customerVisibility: z.enum(["PRIVATE", "SHAREABLE", "REQUEST_ACCESS", "PUBLIC", "null"]).nullable(), includeSubPages: z.boolean().nullable(), isUsedInQuestionnaires: z.boolean().nullable(), ownerAssignment: knowledgeBaseResourceActorAssignment.nullable(), expirationStatus: knowledgeBaseExpirationStatus, expirationDate: z.string().nullable(), lastUpdated: z.string(), lastVerified: z.string().nullable(), tags: z.array(tagInput), categoryId: z.string().nullable()}); export const trustKnowledgeBaseResourceOutput = z.union([knowledgeBaseDocumentResourceOutput, knowledgeBaseWebpageResourceOutput]); export const pageInfo = z.object({endCursor: z.string().nullable(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().nullable()}); export const paginatedResponseTrustKnowledgeBaseResourceOutput = z.object({results: z.object({data: z.array(trustKnowledgeBaseResourceOutput), 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 Knowledge Base resources * * GET /knowledge-base/resources */ export const getApiKnowledgeBaseResources = async (args: GetApiKnowledgeBaseResourcesArgs, options?: GetApiKnowledgeBaseResourcesOptions) => { 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.q != null) query.append("q", String(args.q)) for (const item of args.typeMatchesAny ?? []) query.append("typeMatchesAny", String(item)) if (args.lastUpdatedAfter != null) query.append("lastUpdatedAfter", String(args.lastUpdatedAfter)) if (args.lastUpdatedBefore != null) query.append("lastUpdatedBefore", String(args.lastUpdatedBefore)) if (args.matchesTags != null) query.append("matchesTags", String(args.matchesTags)) if (args.expiresBefore != null) query.append("expiresBefore", String(args.expiresBefore)) if (args.expiresAfter != null) query.append("expiresAfter", String(args.expiresAfter)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/knowledge-base/resources${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(paginatedResponseTrustKnowledgeBaseResourceOutput, await res.json()) };