import {z} from 'zod' export type PageSize = number; export type PageCursor = string; export type GetApiKnowledgeBaseAnswerLibraryArgs = {pageSize?: PageSize | undefined, pageCursor?: PageCursor | undefined, q?: string | undefined, lastUpdatedAfter?: string | undefined, lastUpdatedBefore?: string | undefined, matchesTags?: string | undefined, expiresBefore?: string | undefined, expiresAfter?: string | undefined}; export type GetApiKnowledgeBaseAnswerLibraryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const answerLibraryActorAssignment = z.object({type: z.enum(["User", "Team"]), id: z.string(), displayName: z.string().nullable()}); export const tagInput = z.object({categoryId: z.string(), tagId: z.string()}); export const knowledgeBaseAnswerLibraryEntryOutput = z.object({id: z.string(), question: z.string(), answer: z.string(), expirationStatus: z.enum(["CURRENT", "EXPIRED"]), ownerAssignment: answerLibraryActorAssignment.nullable(), expirationDate: z.string().nullable(), lastUpdated: z.string(), lastVerified: z.string().nullable(), tags: z.array(tagInput)}); export const pageInfo = z.object({endCursor: z.string().nullable(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().nullable()}); export const paginatedResponseKnowledgeBaseAnswerLibraryEntryOutput = z.object({results: z.object({data: z.array(knowledgeBaseAnswerLibraryEntryOutput), 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 Answer Library entries * * GET /knowledge-base/answer-library */ export const getApiKnowledgeBaseAnswerLibrary = async (args: GetApiKnowledgeBaseAnswerLibraryArgs, options?: GetApiKnowledgeBaseAnswerLibraryOptions) => { 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)) 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/answer-library${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(paginatedResponseKnowledgeBaseAnswerLibraryEntryOutput, await res.json()) };