import {z} from 'zod' export type CVEStatus = 'reported' | 'active' | 'mitigated' | 'cleared' | 'disputed' | 'unknown'; export type CVEsNotificationEventTypes = 'score_increased' | 'score_decreased' | 'status_changed'; export type GetApiChannelsChannelNameCvesHistoryArgs = {channel_name: string, from_date?: string | undefined, to_date?: string | undefined, min_download_count?: number | undefined, status?: Array | undefined, event_types?: Array | undefined, q?: string | undefined, max_score?: number | undefined, min_score?: number | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiChannelsChannelNameCvesHistoryOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const artifactFamily = z.enum(["anaconda_project", "anaconda_env", "cve", "conda", "python", "cran", "notebook", "docker", "sbom"]); export const cVENotificationChanges = z.array(z.unknown()); export const cVENotificationsHistory = z.object({file_name: z.string().optional(), platform: z.string().optional(), artifact_family: artifactFamily.optional(), cves_count: z.number().optional(), old_max_score: z.number().optional(), new_max_score: z.number().optional(), notification_changes: z.array(cVENotificationChanges).optional(), number_of_downloads: z.number().optional(), created_at: z.string().optional()}); export const channelCveHistory = z.array(cVENotificationsHistory); 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 the cve history logs of the channel * * GET /channels/{channel_name}/cves_history * * Also accepts auth: user_token */ export const getApiChannelsChannelNameCvesHistory = async (args: GetApiChannelsChannelNameCvesHistoryArgs, options?: GetApiChannelsChannelNameCvesHistoryOptions) => { const {baseUrl = "https://api.anaconda.cloud/api", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.min_download_count != null) query.append("min_download_count", String(args.min_download_count)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.event_types ?? []) query.append("event_types", String(item)) if (args.q != null) query.append("q", String(args.q)) if (args.max_score != null) query.append("max_score", String(args.max_score)) if (args.min_score != null) query.append("min_score", String(args.min_score)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/channels/${encodeURIComponent(String(args.channel_name))}/cves_history${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(channelCveHistory, await res.json()) };