import {z} from 'zod' export type CreateApiApiV1WebhooksDeliveriesDeliveryIdReplayArgs = {delivery_id: string}; export type CreateApiApiV1WebhooksDeliveriesDeliveryIdReplayOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookDeliveryResponse = z.object({id: z.string(), organization_id: z.string(), replayable: z.boolean().optional(), replay_unavailable_reason: z.string().nullable().optional(), webhook_subscription_id: z.string().nullable().optional(), phone_number_id: z.string().nullable().optional(), event_id: z.string(), event_type: z.string(), url: z.string(), request_payload: z.string(), response_status: z.number().int().nullable().optional(), response_body: z.string().nullable().optional(), error_detail: z.string().nullable().optional(), duration_ms: z.number().int().nullable().optional(), is_replay: z.boolean(), created_at: z.string()}); 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 }; /** * Replay Delivery * * POST /api/v1/webhooks/deliveries/{delivery_id}/replay * * Also accepts auth: BearerAuth */ export const createApiApiV1WebhooksDeliveriesDeliveryIdReplay = async (args: CreateApiApiV1WebhooksDeliveriesDeliveryIdReplayArgs, options: CreateApiApiV1WebhooksDeliveriesDeliveryIdReplayOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webhooks/deliveries/${encodeURIComponent(String(args.delivery_id))}/replay${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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()) };