import {z} from 'zod' export type GetApiAccountChannelsArgs = {permission?: ('read' | 'write' | 'manage') | undefined, offset?: number | undefined, limit?: number | undefined, include_subchannels?: boolean | undefined, sort?: (Array<'name' | '-name' | 'updated_at' | '-updated_at'>) | undefined}; export type GetApiAccountChannelsOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const channelPrivacy = z.enum(["private", "authenticated", "public"]); export const channelType = z.enum(["default", "virtual"]); export const channelIndexingBehavior = z.enum(["default", "frozen"]); export const virtualChannelSource = z.object({name: z.string(), parent_name: z.string().optional()}); export const filterLicenses = z.array(z.enum(["agpl", "gpl2", "gpl3", "lgpl", "bsd", "mit", "apache", "psf", "public_domain", "proprietary", "other", "none"])); export const filterIncludeSpecs = z.array(z.string()); export const filterExcludeSpecs = z.array(z.string()); export const channelFilters = z.object({conda: z.object({include_licenses: filterLicenses.optional(), exclude_licenses: filterLicenses.optional(), include_specs: filterIncludeSpecs.optional(), exclude_specs: filterExcludeSpecs.optional()}).optional(), cran: z.object({include_licenses: filterLicenses.optional(), exclude_licenses: filterLicenses.optional(), include_specs: filterIncludeSpecs.optional(), exclude_specs: filterExcludeSpecs.optional()}).optional(), python: z.object({include_licenses: filterLicenses.optional(), exclude_licenses: filterLicenses.optional(), include_specs: filterIncludeSpecs.optional(), exclude_specs: filterExcludeSpecs.optional()}).optional()}); export const channel = z.object({id: z.string().optional(), name: z.string(), parent: z.string().optional(), description: z.string().optional(), privacy: channelPrivacy.optional(), channel_type: channelType.optional(), indexing_behavior: channelIndexingBehavior.optional(), sources: z.array(virtualChannelSource).optional(), created: z.string().optional(), updated: z.string().optional(), artifact_count: z.number().optional(), download_count: z.number().optional(), filters: channelFilters.optional()}); export const getApiAccountChannelsResponse = z.object({total_count: z.number().optional(), items: z.array(channel).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 channels the user has access to * * GET /account/channels * * Also accepts auth: user_token */ export const getApiAccountChannels = async (args: GetApiAccountChannelsArgs, options?: GetApiAccountChannelsOptions) => { 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.permission != null) query.append("permission", String(args.permission)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.include_subchannels != null) query.append("include_subchannels", String(args.include_subchannels)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/account/channels${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(getApiAccountChannelsResponse, await res.json()) };