import {z} from 'zod' export type GetApiLogSearchArgs = {query?: string | undefined, period?: ('24h' | '7d' | 'context') | undefined, source?: ('all' | 'external_api_log' | 'whatsapp_webhook_event' | 'flow_event' | 'webhook_delivery') | undefined, problems_only?: boolean | undefined, errors_only?: boolean | undefined, limit?: number | undefined, cursor?: string | undefined, around?: string | undefined, highlight_event_id?: string | undefined, highlight_resource_id?: string | undefined}; export type GetApiLogSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const logSearchWindow = z.object({since: z.string().optional(), until: z.string().optional()}); export const logSearchEvent = z.object({id: z.string(), source: z.enum(["external_api_log", "whatsapp_webhook_event", "flow_event", "webhook_delivery"]), timestamp: z.string().nullable().optional(), occurred_at: z.string().nullable().optional(), ingested_at: z.string().nullable().optional(), level: z.string().nullable().optional(), status: z.string().nullable().optional(), message: z.string().nullable().optional(), resource_type: z.string().nullable().optional(), resource_id: z.string().nullable().optional(), http_method: z.string().nullable().optional(), endpoint: z.string().nullable().optional(), api_key_id: z.string().nullable().optional(), api_key_name: z.string().nullable().optional(), response_status: z.number().int().nullable().optional(), response_time_ms: z.number().nullable().optional(), event_type: z.string().nullable().optional(), field: z.string().nullable().optional(), event: z.string().nullable().optional(), message_type: z.string().nullable().optional(), phone_number_id: z.string().nullable().optional(), display_phone_number: z.string().nullable().optional(), flow_name: z.string().nullable().optional(), flow_id: z.string().nullable().optional(), flow_execution_id: z.string().nullable().optional(), flow_event_id: z.string().nullable().optional(), step_identifier: z.string().nullable().optional(), step_type: z.string().nullable().optional(), webhook_id: z.string().nullable().optional(), webhook_url: z.string().nullable().optional(), error_class: z.string().nullable().optional(), error_message: z.string().nullable().optional(), attributes: z.record(z.string(), z.unknown()).nullable().optional(), payload: z.record(z.string(), z.unknown()).nullable().optional()}).and(z.record(z.string(), z.unknown())); export const logSearchPagination = z.object({has_more: z.boolean().optional(), next_cursor: z.string().nullable().optional()}); export const logSearchResponse = z.object({data: z.object({available: z.boolean(), source: z.union([z.string(), z.array(z.string())]).nullable().optional(), window: logSearchWindow.optional(), events: z.array(logSearchEvent), pagination: logSearchPagination.optional(), error: z.string().nullable().optional(), code: z.string().nullable().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 }; /** * Search log events * * GET /log_search */ export const getApiLogSearch = async (args: GetApiLogSearchArgs, options?: GetApiLogSearchOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.period != null) query.append("period", String(args.period)) if (args.source != null) query.append("source", String(args.source)) if (args.problems_only != null) query.append("problems_only", String(args.problems_only)) if (args.errors_only != null) query.append("errors_only", String(args.errors_only)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.around != null) query.append("around", String(args.around)) if (args.highlight_event_id != null) query.append("highlight_event_id", String(args.highlight_event_id)) if (args.highlight_resource_id != null) query.append("highlight_resource_id", String(args.highlight_resource_id)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/log_search${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(logSearchResponse, await res.json()) };