import {z} from 'zod' export type GetApiWebhooksMetricsArgs = {period_start_at?: string | undefined, period_end_at?: string | undefined, granularity?: ('hour' | 'day') | undefined, time_zone?: string | undefined, group_by?: 'endpoint_id' | undefined, endpoint_id?: string | undefined}; export type GetApiWebhooksMetricsOptions = {baseUrl: string, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookDeliveryMetricPoint = z.object({avg_latency_ms: z.number().nullable(), endpoint_id: z.string().optional(), error_rate: z.number().gte(0).lte(1), failed: z.number().int(), p50_latency_ms: z.number().nullable(), p95_latency_ms: z.number().nullable(), p99_latency_ms: z.number().nullable(), period_start_at: z.string(), total: z.number().int()}); export const webhookDeliveryMetricsResponse = z.object({$schema: z.string().optional(), points: z.array(webhookDeliveryMetricPoint).nullable()}); 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 delivery metrics * * GET /webhooks/metrics */ export const getApiWebhooksMetrics = async (args: GetApiWebhooksMetricsArgs, options: GetApiWebhooksMetricsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period_start_at != null) query.append("period_start_at", String(args.period_start_at)) if (args.period_end_at != null) query.append("period_end_at", String(args.period_end_at)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.time_zone != null) query.append("time_zone", String(args.time_zone)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (args.endpoint_id != null) query.append("endpoint_id", String(args.endpoint_id)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/metrics${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(webhookDeliveryMetricsResponse, await res.json()) };