import {z} from 'zod' export type GetApiSystemHistoryArgs = {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, user_id?: string | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiSystemHistoryOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const accountHistory = z.object({items: z.array(z.object({event_id: z.string().optional(), 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 full history of all system events * * GET /system/history */ export const getApiSystemHistory = async (args: GetApiSystemHistoryArgs, options?: GetApiSystemHistoryOptions) => { 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.user_id != null) query.append("user_id", String(args.user_id)) 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(/\/+$/, '')}/system/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(accountHistory, await res.json()) };