import {z} from 'zod' export type GetApiChannelsChannelNameHistoryArgs = {channel_name: string, event_types?: (Array<'channel_created' | 'channel_updated' | 'channel_deleted' | 'mirror_registered' | 'mirror_deleted' | 'mirroring_completed' | 'mirroring_failed' | 'artifact_registered' | 'artifact_removed' | 'artifact_copied' | 'artifact_generated' | 'artifact_metadata_updated' | 'artifact_downloaded'>) | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiChannelsChannelNameHistoryOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const channelHistory = z.object({items: z.array(z.object({event_type: z.string().optional(), data: z.object({}).optional(), meta: z.object({}).optional(), created: z.number().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 the history logs of the channel * * GET /channels/{channel_name}/history * * Also accepts auth: user_token */ export const getApiChannelsChannelNameHistory = async (args: GetApiChannelsChannelNameHistoryArgs, options?: GetApiChannelsChannelNameHistoryOptions) => { 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") for (const item of args.event_types ?? []) query.append("event_types", String(item)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/channels/${encodeURIComponent(String(args.channel_name))}/history${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(channelHistory, await res.json()) };