import {z} from 'zod' export type GetApiV2StorageEventsArgs = {sinceId?: string | undefined, maxId?: string | undefined, component?: string | undefined, configurationId?: string | undefined, runId?: string | undefined, q?: string | undefined, limit?: number | undefined, offset?: number | undefined, sortOrder?: ('asc' | 'desc') | undefined}; export type GetApiV2StorageEventsOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const eventAttachmentResponse = z.object({id: z.number().int(), isSliced: z.boolean(), uploadType: z.string(), isExpired: z.boolean(), created: z.string(), name: z.string(), url: z.string(), sizeBytes: z.number().int().nullable()}); export const eventResponse = z.object({uuid: z.string(), event: z.string(), component: z.string(), message: z.string(), description: z.string(), type: z.enum(["info", "success", "warn", "error"]), runId: z.string().nullable(), created: z.string(), configurationId: z.string().nullable(), objectId: z.union([z.string(), z.number().int()]), objectName: z.string(), objectType: z.string(), context: z.union([z.object({remoteAddr: z.string().nullable().optional(), httpReferer: z.string().nullable().optional(), httpUserAgent: z.string().nullable().optional(), httpUserAgentOriginal: z.string().nullable().optional(), authorization: z.string().nullable().optional(), apiVersion: z.string().nullable().optional()}), z.array(z.unknown())]), params: z.record(z.string(), z.unknown()), results: z.record(z.string(), z.unknown()), performance: z.record(z.string(), z.unknown()), token: z.object({id: z.number().int(), name: z.string().nullable()}), idBranch: z.number().int().nullable(), uri: z.string(), attachments: z.array(eventAttachmentResponse)}); export const eventsListResponse = z.array(eventResponse); 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 }; /** * Events list * * GET /v2/storage/events * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageEvents = async (args: GetApiV2StorageEventsArgs, options?: GetApiV2StorageEventsOptions) => { const {baseUrl = "https://connection.keboola.com", fetch: fetchFn = fetch, StorageKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sinceId != null) query.append("sinceId", String(args.sinceId)) if (args.maxId != null) query.append("maxId", String(args.maxId)) if (args.component != null) query.append("component", String(args.component)) if (args.configurationId != null) query.append("configurationId", String(args.configurationId)) if (args.runId != null) query.append("runId", String(args.runId)) if (args.q != null) query.append("q", String(args.q)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/events${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(eventsListResponse, await res.json()) };