import {z} from 'zod' export type ArtifactFamily = 'anaconda_project' | 'anaconda_env' | 'cve' | 'conda' | 'python' | 'cran' | 'notebook' | 'docker' | 'sbom'; export type GetApiArtifactsArtifactFamilyArgs = {artifact_family: ArtifactFamily, offset?: number | undefined, limit?: number | undefined, q?: string | undefined, sort?: (Array<'common_name' | '-common_name' | 'download_count' | '-download_count' | 'updated_at' | '-updated_at'>) | undefined}; export type GetApiArtifactsArtifactFamilyOptions = {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 channelPrivacy = z.enum(["private", "authenticated", "public"]); export const artifact = z.object({name: z.string().optional(), family: artifactFamily.optional(), channel: z.string().optional(), subchannel: z.string().optional(), download_count: z.number().optional(), privacy: channelPrivacy.optional(), file_count: z.number().optional(), cve_count: z.number().optional(), metadata: z.object({}).optional(), updated_at: z.string().optional()}); export const getApiArtifactsArtifactFamilyResponse = z.array(artifact); 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 all the artifacts of a specific type * * GET /artifacts/{artifact_family}/ */ export const getApiArtifactsArtifactFamily = async (args: GetApiArtifactsArtifactFamilyArgs, options?: GetApiArtifactsArtifactFamilyOptions) => { 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.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.q != null) query.append("q", String(args.q)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/artifacts/${encodeURIComponent(String(args.artifact_family))}/${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(getApiArtifactsArtifactFamilyResponse, await res.json()) };