import {z} from 'zod' export type ChannelPrivacy = 'private' | 'authenticated' | 'public'; export type ChannelType = 'default' | 'virtual'; export type ChannelIndexingBehavior = 'default' | 'frozen'; export type VirtualChannelSource = {name: string, parent_name?: string | undefined}; export type FilterLicenses = Array<'agpl' | 'gpl2' | 'gpl3' | 'lgpl' | 'bsd' | 'mit' | 'apache' | 'psf' | 'public_domain' | 'proprietary' | 'other' | 'none'>; export type FilterIncludeSpecs = Array; export type FilterExcludeSpecs = Array; export type ChannelFilters = {conda?: ({include_licenses?: FilterLicenses | undefined, exclude_licenses?: FilterLicenses | undefined, include_specs?: FilterIncludeSpecs | undefined, exclude_specs?: FilterExcludeSpecs | undefined}) | undefined, cran?: ({include_licenses?: FilterLicenses | undefined, exclude_licenses?: FilterLicenses | undefined, include_specs?: FilterIncludeSpecs | undefined, exclude_specs?: FilterExcludeSpecs | undefined}) | undefined, python?: ({include_licenses?: FilterLicenses | undefined, exclude_licenses?: FilterLicenses | undefined, include_specs?: FilterIncludeSpecs | undefined, exclude_specs?: FilterExcludeSpecs | undefined}) | undefined}; export type ChannelSettings = {cve_notifications?: ({cve_score_increase?: boolean | undefined, cve_score_decrease?: boolean | undefined, cve_status_change?: boolean | undefined, cve_score_threshold?: number | undefined}) | undefined}; export type ChannelCreateRequest = {name: string, description?: string | undefined, privacy?: ChannelPrivacy | undefined, channel_type?: ChannelType | undefined, indexing_behavior?: ChannelIndexingBehavior | undefined, sources?: Array | undefined, filters?: ChannelFilters | undefined, settings?: ChannelSettings | undefined}; export type CreateApiChannelsArgs = {body: ChannelCreateRequest}; export type CreateApiChannelsOptions = {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 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 }; /** * Create a new channel * * POST /channels * * Also accepts auth: user_token */ export const createApiChannels = async (args: CreateApiChannelsArgs, options?: CreateApiChannelsOptions) => { const {baseUrl = "https://api.anaconda.cloud/api", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/channels${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(channel, await res.json()) };