import {z} from 'zod' export type GetApiLogSearchCatalogOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const logSearchCatalogResponse = z.object({data: z.object({available: z.boolean(), catalog: z.object({sources: z.array(z.object({value: z.string().optional(), label: z.string().optional(), color: z.string().optional(), aliases: z.array(z.string()).optional()})).optional(), filter_suggestions: z.array(z.object({key: z.string().optional(), label: z.string().optional(), description: z.string().optional(), param_key: z.string().optional(), sources: z.array(z.string()).optional()})).optional(), detail_fields: z.array(z.string()).optional()}).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 log search catalog * * GET /log_search/catalog */ export const getApiLogSearchCatalog = async (options?: GetApiLogSearchCatalogOptions) => { 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 (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/log_search/catalog${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(logSearchCatalogResponse, await res.json()) };