import {z} from 'zod' export type GetApiApiLogsArgs = {page?: number | undefined, per_page?: number | undefined, from_date?: string | undefined, to_date?: string | undefined, 'http_methods[]'?: (Array<'post' | 'put' | 'patch' | 'delete'>) | undefined, 'http_statuses[]'?: (Array<'succeeded' | 'failed' | number>) | undefined, api_version?: string | undefined, 'request_paths[]'?: Array | undefined, 'clients[]'?: Array | undefined}; export type GetApiApiLogsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiLogObject = z.object({api_version: z.string(), client: z.string(), http_method: z.enum(["post", "put", "patch", "delete"]), http_status: z.number().int(), logged_at: z.string(), request_body: z.record(z.string(), z.unknown()), request_origin: z.string(), request_path: z.string(), created_at: z.string(), request_id: z.string(), request_response: z.record(z.string(), z.unknown()).nullable()}); export const paginationMeta = z.object({current_page: z.number().int(), next_page: z.number().int().nullable().optional(), prev_page: z.number().int().nullable().optional(), total_pages: z.number().int(), total_count: z.number().int()}); export const apiLogsPaginated = z.object({api_logs: z.array(apiLogObject), meta: paginationMeta}); 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 all API logs * * GET /api_logs */ export const getApiApiLogs = async (args: GetApiApiLogsArgs, options?: GetApiApiLogsOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) for (const item of args['http_methods[]'] ?? []) query.append("http_methods[]", String(item)) for (const item of args['http_statuses[]'] ?? []) query.append("http_statuses[]", String(item)) if (args.api_version != null) query.append("api_version", String(args.api_version)) for (const item of args['request_paths[]'] ?? []) query.append("request_paths[]", String(item)) for (const item of args['clients[]'] ?? []) query.append("clients[]", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) 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(apiLogsPaginated, await res.json()) };