import {z} from 'zod' export type GetApiTenantsTenantIdWebhooksWebhookIdDeliveriesArgs = {page?: number | undefined, perPage?: number | undefined, success?: boolean | undefined, event?: ('MessageSent' | 'MessageDelayed' | 'MessageDeliveryFailed' | 'MessageHeld' | 'MessageBounced' | 'MessageLinkClicked' | 'MessageLoaded' | 'DomainDNSError') | undefined, before?: number | undefined, after?: number | undefined}; export type GetApiTenantsTenantIdWebhooksWebhookIdDeliveriesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookDeliverySummary = z.object({id: z.string(), webhookId: z.string(), event: z.enum(["MessageSent", "MessageDelayed", "MessageDeliveryFailed", "MessageHeld", "MessageBounced", "MessageLinkClicked", "MessageLoaded", "DomainDNSError"]), url: z.string(), statusCode: z.number().int().nullable(), success: z.boolean(), attempt: z.number().int().gte(1), willRetry: z.boolean(), timestamp: z.string()}); export const apiMeta = z.object({requestId: z.string()}); export const listWebhookDeliveriesResponse = z.object({data: z.array(webhookDeliverySummary), 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 webhook deliveries * * GET /tenants/{tenantId}/webhooks/{webhookId}/deliveries */ export const getApiTenantsTenantIdWebhooksWebhookIdDeliveries = async (args: GetApiTenantsTenantIdWebhooksWebhookIdDeliveriesArgs, options?: GetApiTenantsTenantIdWebhooksWebhookIdDeliveriesOptions) => { 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.success != null) query.append("success", String(args.success)) if (args.event != null) query.append("event", String(args.event)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/tenants/${encodeURIComponent(String(args.tenantId))}/webhooks/${encodeURIComponent(String(args.webhookId))}/deliveries${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(listWebhookDeliveriesResponse, await res.json()) };