import {z} from 'zod' export type GetApiAssessmentsArgs = {name?: string | undefined, status?: string | undefined, lastModifiedFrom?: string | undefined, lastModifiedUntil?: string | undefined, templateId?: string | undefined, templateVersion?: string | undefined, assetId?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiAssessmentsOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const assessmentStatus = z.string(); export const originAssessment = z.object({id: z.string(), status: assessmentStatus}); export const asset = z.object({id: z.string()}); export const assetType = z.object({id: z.string()}); export const retakePermission = z.string(); export const template = z.object({id: z.string(), name: z.string(), version: z.number().int(), status: z.string(), assetType: assetType.optional(), notification: z.boolean().optional(), retakePermission: retakePermission.optional()}); export const user = z.object({id: z.string()}); export const userAssignee = z.object({type: z.string(), id: z.string()}); export const provider = z.string(); export const groupMapping = z.object({provider: provider, identity: z.string()}); export const groupAssignee = z.object({type: z.string(), id: z.string(), createdBy: z.string(), createdOn: z.number().int(), lastModifiedBy: z.string(), lastModifiedOn: z.number().int(), name: z.string(), mappings: z.array(groupMapping)}); export const userOrGroupAssignee = z.discriminatedUnion("type", [userAssignee, groupAssignee]); export const item = z.object({id: z.string(), value: z.string().optional()}); export const userAnswer = z.object({type: z.string(), id: z.string()}); export const groupAnswer = z.object({type: z.string(), id: z.string(), createdBy: z.string(), createdOn: z.number().int(), lastModifiedBy: z.string(), lastModifiedOn: z.number().int(), name: z.string(), mappings: z.array(groupMapping)}); export const userOrGroupAnswer = z.discriminatedUnion("type", [userAnswer, groupAnswer]); export const assessmentAttachment = z.object({id: z.string(), fileName: z.string().optional(), createdBy: z.string().optional(), createdOn: z.string().optional()}); export const answer = z.discriminatedUnion("type", [z.object({type: z.string(), value: z.string()}), z.object({type: z.string(), value: z.string()}), z.object({type: z.string(), value: z.string()}), z.object({type: z.string(), value: z.boolean()}), z.object({type: z.string(), value: z.array(item)}), z.object({type: z.string(), value: z.number()}), z.object({type: z.string(), value: z.string()}), z.object({type: z.string(), value: z.array(asset)}), z.object({type: z.string(), value: z.array(userOrGroupAnswer)}), z.object({type: z.string(), value: z.array(assessmentAttachment)})]); export const questionAndAnswer = z.object({id: z.string(), name: z.string().optional(), description: z.string().optional(), answer: answer.optional(), comments: z.string().optional()}); export const assessment = z.object({id: z.string(), originAssessment: originAssessment.optional(), name: z.string(), asset: asset.optional(), status: assessmentStatus, template: template, owner: user, assignees: z.array(userOrGroupAssignee), isVisibleToEveryone: z.boolean().optional(), content: z.array(questionAndAnswer), createdOn: z.string(), createdBy: user, lastModifiedOn: z.string(), lastModifiedBy: user, submittedOn: z.string().optional(), submittedBy: user.optional(), assessmentReview: asset.optional()}); export const pagedAssessments = z.object({nextCursor: z.string().optional(), results: z.array(assessment)}); 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 assessments * * GET /assessments * * Also accepts auth: jwtAuth, oauth2, oauth2 */ export const getApiAssessments = async (args: GetApiAssessmentsArgs, options: GetApiAssessmentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.status != null) query.append("status", String(args.status)) if (args.lastModifiedFrom != null) query.append("lastModifiedFrom", String(args.lastModifiedFrom)) if (args.lastModifiedUntil != null) query.append("lastModifiedUntil", String(args.lastModifiedUntil)) if (args.templateId != null) query.append("templateId", String(args.templateId)) if (args.templateVersion != null) query.append("templateVersion", String(args.templateVersion)) if (args.assetId != null) query.append("assetId", String(args.assetId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/assessments${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(pagedAssessments, await res.json()) };