import {z} from 'zod' export type GetApiLogsArgs = {page?: number | undefined, perPage?: number | undefined, requestId?: string | undefined, status?: ('success' | 'error') | undefined, statusCode?: number | undefined, endpoint?: string | undefined, credentialId?: string | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiLogsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const logEntry = z.object({requestId: z.string(), timestamp: z.string(), method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]), path: z.string(), endpoint: z.string(), statusCode: z.number().int(), durationMs: z.number().int(), error: z.object({code: z.string().optional(), message: z.string().nullable().optional()}).nullable().optional(), context: z.object({ipAddress: z.string().nullable().optional(), userAgent: z.string().nullable().optional(), idempotencyKey: z.string().nullable().optional(), queryParams: z.record(z.string(), z.unknown()).nullable().optional()}), sdk: z.object({name: z.string().optional(), version: z.string().nullable().optional()}).nullable().optional(), credential: z.object({id: z.string(), keyPrefix: z.string().nullable().optional()}), email: z.object({id: z.string().optional(), recipientCount: z.number().int().nullable().optional()}).nullable().optional(), rateLimit: z.object({limited: z.boolean().optional(), limit: z.number().int().nullable().optional(), remaining: z.number().int().nullable().optional(), reset: z.number().int().nullable().optional()})}); export const apiMeta = z.object({requestId: z.string()}); export const logListResponse = z.object({data: z.array(logEntry), page: z.number().int(), perPage: z.number().int(), total: z.number().int(), totalPages: z.number().int(), meta: apiMeta}); 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 request logs * * GET /logs */ export const getApiLogs = async (args: GetApiLogsArgs, options?: GetApiLogsOptions) => { const {baseUrl = "https://api.arkhq.io/v1", fetch: fetchFn = fetch, apiKey} = 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.perPage != null) query.append("perPage", String(args.perPage)) if (args.requestId != null) query.append("requestId", String(args.requestId)) if (args.status != null) query.append("status", String(args.status)) if (args.statusCode != null) query.append("statusCode", String(args.statusCode)) if (args.endpoint != null) query.append("endpoint", String(args.endpoint)) if (args.credentialId != null) query.append("credentialId", String(args.credentialId)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(logListResponse, await res.json()) };