import {z} from 'zod' export type GetApiPlatformWebhooksDeliveriesArgs = {webhookId?: string | undefined, tenantId?: string | undefined, event?: ('MessageSent' | 'MessageDelayed' | 'MessageDeliveryFailed' | 'MessageHeld' | 'MessageBounced' | 'MessageLinkClicked' | 'MessageLoaded' | 'DomainDNSError') | undefined, success?: boolean | undefined, before?: number | undefined, after?: number | undefined, page?: number | undefined, perPage?: number | undefined}; export type GetApiPlatformWebhooksDeliveriesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const platformWebhookDeliverySummary = z.object({id: z.string(), webhookId: z.string(), tenantId: 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(), willRetry: z.boolean(), timestamp: z.string()}); export const apiMeta = z.object({requestId: z.string()}); export const listPlatformWebhookDeliveriesResponse = z.object({success: z.literal(true), data: z.array(platformWebhookDeliverySummary), 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 platform webhook deliveries * * GET /platform/webhooks/deliveries */ export const getApiPlatformWebhooksDeliveries = async (args: GetApiPlatformWebhooksDeliveriesArgs, options?: GetApiPlatformWebhooksDeliveriesOptions) => { 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.webhookId != null) query.append("webhookId", String(args.webhookId)) if (args.tenantId != null) query.append("tenantId", String(args.tenantId)) if (args.event != null) query.append("event", String(args.event)) if (args.success != null) query.append("success", String(args.success)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/platform/webhooks/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(listPlatformWebhookDeliveriesResponse, await res.json()) };