import {z} from 'zod' export type ArtifactFamily = 'anaconda_project' | 'anaconda_env' | 'cve' | 'conda' | 'python' | 'cran' | 'notebook' | 'docker' | 'sbom'; export type GetApiArtifactsArgs = {offset?: number | undefined, limit?: number | undefined, q?: string | undefined, only_names?: Array | undefined, sort?: (Array<'common_name' | '-common_name' | 'download_count' | '-download_count' | 'updated_at' | '-updated_at'>) | undefined, artifact_family?: Array | undefined, platform?: string | undefined, created?: number | undefined, updated?: number | undefined}; export type GetApiArtifactsOptions = {baseUrl?: 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 getApiArtifactsResponse = z.object({total_count: z.number().optional(), items: z.array(artifact).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 all artifacts * * GET /artifacts */ export const getApiArtifacts = async (args: GetApiArtifactsArgs, options?: GetApiArtifactsOptions) => { const {baseUrl = "https://api.anaconda.cloud/api", fetch: fetchFn = fetch} = 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.only_names ?? []) query.append("only_names", String(item)) for (const item of args.sort ?? []) query.append("sort", String(item)) for (const item of args.artifact_family ?? []) query.append("artifact_family", String(item)) if (args.platform != null) query.append("platform", String(args.platform)) if (args.created != null) query.append("created", String(args.created)) if (args.updated != null) query.append("updated", String(args.updated)) const url = `${baseUrl.replace(/\/+$/, '')}/artifacts${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(getApiArtifactsResponse, await res.json()) };