import {z} from 'zod' export type GetApiOsaVulnerabilitiesArgs = {scanId: string, page?: number | undefined, itemsPerPage?: number | undefined, libraryId?: Array | undefined, stateId?: Array | undefined, comment?: string | undefined, since?: number | undefined, until?: number | undefined}; export type GetApiOsaVulnerabilitiesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const cxOsaApplicationDTOsSeverityDTO = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const cxOsaApplicationDTOsResponsesVulnerabilityStateDto = z.object({id: z.number().int().optional(), actionType: z.string().optional(), name: z.string().optional()}); export const cxOsaApplicationDTOsResponsesV2VulnerabilityDto = z.object({id: z.string().optional(), cveName: z.string().optional(), score: z.number().optional(), severity: cxOsaApplicationDTOsSeverityDTO.optional(), publishDate: z.string().optional(), url: z.string().optional(), description: z.string().optional(), recommendations: z.string().optional(), sourceFileName: z.string().optional(), libraryId: z.string().optional(), state: cxOsaApplicationDTOsResponsesVulnerabilityStateDto.optional(), commentsAmount: z.number().int().optional(), similarityId: z.string().optional(), fixUrl: z.string().optional()}); export const getApiOsaVulnerabilitiesResponse = z.array(cxOsaApplicationDTOsResponsesV2VulnerabilityDto); 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 vulnerabilities for the specified scan Id. Each vulnerability returned is unique to its library or file. * * GET /osa/vulnerabilities */ export const getApiOsaVulnerabilities = async (args: GetApiOsaVulnerabilitiesArgs, options?: GetApiOsaVulnerabilitiesOptions) => { 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=2.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)) for (const item of args.libraryId ?? []) query.append("libraryId", String(item)) for (const item of args.stateId ?? []) query.append("stateId", String(item)) if (args.comment != null) query.append("comment", String(args.comment)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/osa/vulnerabilities${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(getApiOsaVulnerabilitiesResponse, await res.json()) };