import {z} from 'zod' export type GetApiScanRegistryNameLayersIdArgs = {'X-Auth-Token': string, name: string, id: string}; export type GetApiScanRegistryNameLayersIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rESTVulnerability = z.object({name: z.string(), score: z.number(), severity: z.string(), vectors: z.string(), description: z.string(), package_name: z.string(), package_version: z.string(), fixed_version: z.string(), link: z.string(), score_v3: z.number(), vectors_v3: z.string(), published_timestamp: z.number().int(), last_modified_timestamp: z.number().int(), cpes: z.array(z.string()).optional(), cves: z.array(z.string()).optional(), feed_rating: z.string(), in_base_image: z.boolean(), tags: z.array(z.string()).optional()}); export const rESTScanLayer = z.object({digest: z.string(), cmds: z.string(), vulnerabilities: z.array(rESTVulnerability), size: z.number().int()}); export const rESTScanLayersReport = z.object({layers: z.array(rESTScanLayer)}); export const rESTScanLayersReportData = z.object({report: rESTScanLayersReport}); 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 }; /** * Show registry layers report * * GET /scan/registry/{name}/layers/{id} */ export const getApiScanRegistryNameLayersId = async (args: GetApiScanRegistryNameLayersIdArgs, options: GetApiScanRegistryNameLayersIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Auth-Token'] != null) headers.set("X-Auth-Token", String(args['X-Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/scan/registry/${encodeURIComponent(String(args.name))}/layers/${encodeURIComponent(String(args.id))}${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(rESTScanLayersReportData, await res.json()) };