import {z} from 'zod' export type CreateApiCvesReportArgs = {use_blob?: boolean | undefined, body: {sha256s: Array}}; export type CreateApiCvesReportOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const cVEsReport = z.object({sha256: z.string().optional(), cves: z.array(z.object({id: z.string().optional(), score: z.number().optional(), status: z.string().optional(), curated: z.boolean().optional(), published_at: z.string().optional(), nvd_modified_at: z.string().optional(), anaconda_modified_at: z.string().optional()})).optional()}); export const createApiCvesReportResponse = z.object({items: z.array(cVEsReport).optional()}); 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 list of the CVEs with status for provided list of packages. Using Post as Get * * POST /cves-report * * Also accepts auth: user_token */ export const createApiCvesReport = async (args: CreateApiCvesReportArgs, options?: CreateApiCvesReportOptions) => { const {baseUrl = "https://api.anaconda.cloud/api", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.use_blob != null) query.append("use_blob", String(args.use_blob)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/cves-report${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiCvesReportResponse, await res.json()) };