import {z} from 'zod' export type GetApiAvailableRisksV2Args = {category?: string | undefined, severity?: ('info' | 'low' | 'medium' | 'high' | 'critical') | undefined}; export type GetApiAvailableRisksV2Options = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const availableRiskV2 = z.object({category: z.string().optional(), description: z.string().optional(), finding: z.string().optional(), generic: z.boolean().optional(), group: z.string().optional(), id: z.string().optional(), remediation: z.string().optional(), risk: z.string().optional(), riskDetails: z.string().optional(), riskSubtype: z.string().optional(), riskType: z.string().optional(), severity: z.enum(["pass", "info", "low", "medium", "high", "critical"]).optional()}); export const getApiAvailableRisksV2Response = z.array(availableRiskV2); 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 a list of available risks in the platform * * GET /available_risks/v2 */ export const getApiAvailableRisksV2 = async (args: GetApiAvailableRisksV2Args, options?: GetApiAvailableRisksV2Options) => { const {baseUrl = "https://cyber-risk.upguard.com/api/public", fetch: fetchFn = fetch, APIKeyInHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.category != null) query.append("category", String(args.category)) if (args.severity != null) query.append("severity", String(args.severity)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/available_risks/v2${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(getApiAvailableRisksV2Response, await res.json()) };