import {z} from 'zod' export type GetApiWebhooksWebhookIdLogsArgs = {webhookId: string, ResponseCode?: number | undefined, Success?: boolean | undefined, From?: string | undefined, To?: string | undefined, Filters?: string | undefined, Sorts?: string | undefined, Page?: number | undefined, PageSize?: number | undefined}; export type GetApiWebhooksWebhookIdLogsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookExecutionLog = z.object({id: z.string(), dateCreated: z.string(), url: z.string().max(2048), signature: z.string().max(128), requestBody: z.string().max(8192), responseCode: z.number().int().nullable().optional(), responseBody: z.string().max(8192).nullable().optional(), webhookId: z.string().nullable().optional(), userId: z.string().max(36).nullable().optional(), retryCount: z.number().int(), userAgent: z.string().max(512).nullable().optional(), orderId: z.string().nullable().optional()}); export const webhookExecutionLogPaginatedResponse = z.object({success: z.boolean(), pages: z.number().int(), data: z.array(webhookExecutionLog)}); 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 paginated execution logs for a webhook. * * GET /webhooks/{webhookId}/logs */ export const getApiWebhooksWebhookIdLogs = async (args: GetApiWebhooksWebhookIdLogsArgs, options?: GetApiWebhooksWebhookIdLogsOptions) => { const {baseUrl = "https://api.komerza.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ResponseCode != null) query.append("ResponseCode", String(args.ResponseCode)) if (args.Success != null) query.append("Success", String(args.Success)) if (args.From != null) query.append("From", String(args.From)) if (args.To != null) query.append("To", String(args.To)) if (args.Filters != null) query.append("Filters", String(args.Filters)) if (args.Sorts != null) query.append("Sorts", String(args.Sorts)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/${encodeURIComponent(String(args.webhookId))}/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(webhookExecutionLogPaginatedResponse, await res.json()) };