import {z} from 'zod' export type GetApiV2WebhooksEventsEventIdDeliveriesArgs = {event_id: string, delivery_id?: string | undefined, subscription_id?: string | undefined, size?: number | undefined, cursor?: string | undefined}; export type GetApiV2WebhooksEventsEventIdDeliveriesOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookDeliveryTrigger = z.enum(["AUTOMATIC", "MANUAL"]); export const webhookRecoveryDeliveryStatus = z.enum(["PENDING", "PROCESSING", "RETRYING", "SUCCESS", "PERMANENTLY_FAILED"]); export const webhookRecoveryDiagnosticType = z.enum(["HTTP_ERROR", "TIMEOUT", "NETWORK_ERROR", "SUBSCRIPTION_UNAVAILABLE", "INTERNAL_ERROR", "UNKNOWN"]); export const webhookRecoveryDeliveryError = z.object({type: webhookRecoveryDiagnosticType, description: z.string()}); export const webhookRecoveryDelivery = z.object({id: z.string(), subscription_id: z.string(), trigger: webhookDeliveryTrigger, status: webhookRecoveryDeliveryStatus, next_retry_at: z.string().nullable().optional(), created_at: z.string(), last_attempt_at: z.string().nullable().optional(), last_http_status: z.number().int().nullable().optional(), last_response_body: z.string().nullable().optional(), last_response_body_truncated: z.boolean().nullable().optional(), last_error: webhookRecoveryDeliveryError.nullable().optional()}); export const webhookRecoveryDeliveryList = z.object({deliveries: z.array(webhookRecoveryDelivery), has_more: z.boolean(), next_cursor: z.string().nullable().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 /v2/webhooks/events/{event_id}/deliveries */ export const getApiV2WebhooksEventsEventIdDeliveries = async (args: GetApiV2WebhooksEventsEventIdDeliveriesArgs, options?: GetApiV2WebhooksEventsEventIdDeliveriesOptions) => { const {baseUrl = "https://api.1money.com", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.delivery_id != null) query.append("delivery_id", String(args.delivery_id)) if (args.subscription_id != null) query.append("subscription_id", String(args.subscription_id)) if (args.size != null) query.append("size", String(args.size)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/webhooks/events/${encodeURIComponent(String(args.event_id))}/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(webhookRecoveryDeliveryList, await res.json()) };