import {z} from 'zod' export type GetApiWebhooksIdDeliveriesDeliveryIdArgs = {id: string, deliveryId: string, team_id: string}; export type GetApiWebhooksIdDeliveriesDeliveryIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookDeliveryDetail = z.object({object: z.string().optional(), id: z.string().optional(), event_id: z.string().optional(), event_type: z.string().optional(), status: z.enum(["pending", "delivered", "failed", "retrying"]).optional(), attempt_count: z.number().int().optional(), max_attempts: z.number().int().optional(), response_status: z.number().int().nullable().optional(), response_time_ms: z.number().int().nullable().optional(), error_message: z.string().nullable().optional(), created_at: z.string().optional(), last_attempt_at: z.string().nullable().optional(), delivered_at: z.string().nullable().optional(), next_retry_at: z.string().nullable().optional(), contact: z.object({id: z.string().optional(), email: z.string().optional(), name: z.string().nullable().optional()}).nullable().optional(), campaign: z.object({id: z.string().optional(), name: z.string().optional()}).nullable().optional(), payload: z.object({}).optional(), response_body: z.string().nullable().optional(), response_headers: z.object({}).nullable().optional()}); export const webhookDeliveryResponse = z.object({success: z.boolean().optional(), request_id: z.string().optional(), data: webhookDeliveryDetail.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 Webhook Delivery * * GET /webhooks/{id}/deliveries/{deliveryId} */ export const getApiWebhooksIdDeliveriesDeliveryId = async (args: GetApiWebhooksIdDeliveriesDeliveryIdArgs, options?: GetApiWebhooksIdDeliveriesDeliveryIdOptions) => { const {baseUrl = "https://api.supersend.io/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.team_id != null) query.append("team_id", String(args.team_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/${encodeURIComponent(String(args.id))}/deliveries/${encodeURIComponent(String(args.deliveryId))}${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(webhookDeliveryResponse, await res.json()) };