import {z} from 'zod' export type GetApiOsaLibrariesArgs = {scanId: string, page?: number | undefined, itemsPerPage?: number | undefined}; export type GetApiOsaLibrariesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const cxOsaApplicationDTOsMatchTypeDto = z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().optional()}); export const cxOsaApplicationDTOsSeverityDTO = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const cxOsaApplicationDTOsResponsesLibraryLocationDto = z.object({path: z.string().optional(), matchType: cxOsaApplicationDTOsMatchTypeDto.optional()}); export const cxOsaApplicationDTOsResponsesV3ScannedLibraryDTO = z.object({id: z.string().optional(), name: z.string().optional(), version: z.string().optional(), releaseDate: z.string().optional(), highUniqueVulnerabilityCount: z.number().int().optional(), mediumUniqueVulnerabilityCount: z.number().int().optional(), lowUniqueVulnerabilityCount: z.number().int().optional(), notExploitableVulnerabilityCount: z.number().int().optional(), newestVersion: z.string().optional(), newestVersionReleaseDate: z.string().optional(), numberOfVersionsSinceLastUpdate: z.number().int().optional(), confidenceLevel: z.number().int().optional(), matchType: cxOsaApplicationDTOsMatchTypeDto.optional(), licenses: z.array(z.string()).optional(), outdated: z.boolean().optional(), severity: cxOsaApplicationDTOsSeverityDTO.optional(), riskScore: z.number().optional(), locations: z.array(cxOsaApplicationDTOsResponsesLibraryLocationDto).optional(), codeUsageStatus: z.string().optional(), codeReferenceCount: z.number().int().optional(), packageRepository: z.string().optional()}); export const getApiOsaLibrariesResponse = z.array(cxOsaApplicationDTOsResponsesV3ScannedLibraryDTO); 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 }; /** * Returns all the used libraries for the specified scan Id * * GET /osa/libraries */ export const getApiOsaLibraries = async (args: GetApiOsaLibrariesArgs, options?: GetApiOsaLibrariesOptions) => { const {baseUrl = "https://desktop-rmvpboc/cxrestapi/help", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;v=3.0") if (args.scanId != null) query.append("scanId", String(args.scanId)) if (args.page != null) query.append("page", String(args.page)) if (args.itemsPerPage != null) query.append("itemsPerPage", String(args.itemsPerPage)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/osa/libraries${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(getApiOsaLibrariesResponse, await res.json()) };