import {z} from 'zod' export type GetApiLogsArgs = {'Accept-Language'?: ('es' | 'en') | undefined, 'X-Child-Company-Id'?: string | undefined, limit?: number | undefined, search?: string | undefined, next?: string | undefined, previous?: string | undefined, 'method.in[]'?: Array | undefined}; export type GetApiLogsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const logsResponseData = z.object({created_at: z.number().int().optional(), id: z.string().optional(), ip_address: z.string().optional(), livemode: z.boolean().optional(), loggable_id: z.string().nullable().optional(), loggable_type: z.string().nullable().optional(), method: z.string().optional(), oauth_token_id: z.string().nullable().optional(), query_string: z.record(z.string(), z.unknown()).optional(), related: z.string().optional(), request_body: z.object({}).optional(), request_headers: z.record(z.string(), z.string()).optional(), response_body: z.object({}).optional(), response_headers: z.record(z.string(), z.string()).optional(), searchable_tags: z.array(z.string()).optional(), status: z.string().optional(), updated_at: z.string().optional(), url: z.string().optional(), user_account_id: z.string().optional(), version: z.string().optional()}); export const logsResponseForRequest = z.object({has_more: z.boolean().optional(), object: z.string().optional(), next_page_url: z.string().nullable().optional(), previous_page_url: z.string().nullable().optional(), data: z.array(logsResponseData).nullable().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 List Of Logs * * GET /logs */ export const getApiLogs = async (args: GetApiLogsArgs, options?: GetApiLogsOptions) => { const {baseUrl = "https://api.conekta.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.conekta-v2.3.0+json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['X-Child-Company-Id'] != null) headers.set("X-Child-Company-Id", String(args['X-Child-Company-Id'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.search != null) query.append("search", String(args.search)) if (args.next != null) query.append("next", String(args.next)) if (args.previous != null) query.append("previous", String(args.previous)) for (const item of args['method.in[]'] ?? []) query.append("method.in[]", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) 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(logsResponseForRequest, await res.json()) };