import {z} from 'zod' export type GetApiV2RiskRegisterFetchArgs = {page?: number | undefined, page_size?: number | undefined, status?: string | undefined, entity?: string | undefined, name?: string | undefined, authorization: string}; export type GetApiV2RiskRegisterFetchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const servicesRiskSchemaRiskEntityResponse = z.object({id: z.string(), name: z.string(), logo: z.string()}); export const servicesRiskSchemaRiskUserResponse = z.object({id: z.string(), name: z.string(), email: z.string()}); export const riskStatus = z.enum(["NOT_STARTED", "IN_PROGRESS", "COMPLETED", "CLOSED"]); export const riskAssessmentResponse = z.object({id: z.string(), name: z.string(), entity: servicesRiskSchemaRiskEntityResponse, owner: servicesRiskSchemaRiskUserResponse.optional(), status: riskStatus, start_date: z.string().optional(), end_date: z.string().optional(), created_by: servicesRiskSchemaRiskUserResponse}); export const fetchRiskAssessmentResponse = z.object({page: z.number().int(), page_size: z.number().int(), total: z.number().int(), data: z.array(riskAssessmentResponse)}); 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 }; /** * Fetch Risk Register * * GET /v2/risk/register/fetch */ export const getApiV2RiskRegisterFetch = async (args: GetApiV2RiskRegisterFetchArgs, options: GetApiV2RiskRegisterFetchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.status != null) query.append("status", String(args.status)) if (args.entity != null) query.append("entity", String(args.entity)) if (args.name != null) query.append("name", String(args.name)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/risk/register/fetch${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(fetchRiskAssessmentResponse, await res.json()) };