import {z} from 'zod' export type GetApiApiLogsArgs = {endpoint?: string | undefined, status_code?: number | undefined, errors_only?: boolean | undefined, period?: ('24h' | '7d' | '30d') | undefined, limit?: number | undefined, before?: string | undefined, after?: string | undefined}; export type GetApiApiLogsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const externalApiLog = z.object({id: z.string(), endpoint: z.string(), http_method: z.string(), response_status: z.number().int(), response_time_ms: z.number(), ip_address: z.string().nullable().optional(), error_message: z.string().nullable().optional(), created_at: z.string(), api_key_id: z.string().nullable().optional(), api_key_name: z.string().nullable().optional()}); export const paginationCursor = z.object({before: z.string().optional(), after: z.string().optional()}); export const paging = z.object({cursors: paginationCursor.optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional()}); export const externalApiLogListResponse = z.object({data: z.array(externalApiLog), paging: paging}); 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 }; /** * List API logs * * GET /api_logs */ export const getApiApiLogs = async (args: GetApiApiLogsArgs, options?: GetApiApiLogsOptions) => { 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.endpoint != null) query.append("endpoint", String(args.endpoint)) if (args.status_code != null) query.append("status_code", String(args.status_code)) if (args.errors_only != null) query.append("errors_only", String(args.errors_only)) if (args.period != null) query.append("period", String(args.period)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api_logs${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(externalApiLogListResponse, await res.json()) };