import {z} from 'zod' export type GetApiLogsRequestIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const logEntryDetail = 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()}), request: z.object({bodySize: z.number().int().nullable().optional(), body: z.union([z.record(z.string(), z.unknown()), z.string()]).nullable().optional()}).optional(), response: z.object({bodySize: z.number().int().nullable().optional(), body: z.union([z.record(z.string(), z.unknown()), z.string()]).nullable().optional()}).optional()}); export const apiMeta = z.object({requestId: z.string()}); export const logDetailResponse = z.object({success: z.literal(true), data: logEntryDetail, 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 }; /** * Get API request log details * * GET /logs/{requestId} */ export const getApiLogsRequestId = async (options?: GetApiLogsRequestIdOptions) => { 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 (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/logs/${encodeURIComponent(String(args.requestId))}${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(logDetailResponse, await res.json()) };