import {z} from 'zod' export type GetApiMirrorsArgs = {offset?: number | undefined, limit?: number | undefined, sort?: (Array<'mirror_name' | '-mirror_name' | 'created_at' | '-created_at' | 'routes_mirrored' | '-routes_mirrored' | 'mirror_type' | '-mirror_type' | 'updated_at' | '-updated_at' | 'state' | '-state'>) | undefined}; export type GetApiMirrorsOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const mirrorSyncState = z.object({count: z.object({removed: z.number().int().optional(), passive: z.number().int().optional(), active: z.number().int().optional(), active_planned: z.number().int().optional(), failed: z.number().int().optional(), package_stat: z.object({primary: z.number().int().optional(), dependencies: z.number().int().optional()}).optional(), mirror_filtered: z.object({cve: z.number().int().optional(), date: z.number().int().optional(), signed: z.number().int().optional(), license: z.number().int().optional(), only_specs: z.number().int().optional(), exclude_specs: z.number().int().optional(), duplicated_legacy_conda: z.number().int().optional()}).optional(), channel_filtered: z.object({license: z.number().int().optional(), exclude_specs: z.number().int().optional()}).optional()}).optional(), last_ckey: z.string().optional(), last_exception: z.string().optional(), total_steps_count: z.number().int().optional(), current_step_number: z.number().int().optional(), current_step_description: z.string().optional(), current_step_percentage: z.number().int().optional(), bytes_transfered: z.number().int().optional()}); export const channelPrivacy = z.enum(["private", "authenticated", "public"]); export const mirrorWithChannel = z.object({id: z.string(), name: z.string(), source_root: z.string(), type: z.enum(["conda", "python_simple", "cran", "cve", "sbom"]), mode: z.enum(["active", "passive", "redirect"]), state: z.enum(["pending", "running", "completed", "failed", "deleted", "stopping", "stopped"]), sync_state: mirrorSyncState.optional(), last_run_at: z.string(), created_at: z.string(), updated_at: z.string(), channel: z.object({name: z.string().optional(), privacy: channelPrivacy.optional()}), subchannel: z.object({name: z.string().optional(), privacy: channelPrivacy.optional()}).optional()}); export const getApiMirrorsResponse = z.object({total_count: z.number().optional(), mirrors: z.array(mirrorWithChannel).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 mirrors * * GET /mirrors * * Also accepts auth: user_token */ export const getApiMirrors = async (args: GetApiMirrorsArgs, options?: GetApiMirrorsOptions) => { 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)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/mirrors${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(getApiMirrorsResponse, await res.json()) };