import {z} from 'zod' export type EventClass = 'payments' | 'refunds' | 'disputes' | 'mandates' | 'payouts' | 'subscriptions'; export type EventType = 'payment_succeeded' | 'payment_failed' | 'payment_processing' | 'payment_cancelled' | 'payment_cancelled_post_capture' | 'payment_authorized' | 'payment_partially_authorized' | 'payment_captured' | 'payment_expired' | 'action_required' | 'refund_succeeded' | 'refund_failed' | 'dispute_opened' | 'dispute_expired' | 'dispute_accepted' | 'dispute_cancelled' | 'dispute_challenged' | 'dispute_won' | 'dispute_lost' | 'mandate_active' | 'mandate_revoked' | 'payout_success' | 'payout_failed' | 'payout_initiated' | 'payout_processing' | 'payout_cancelled' | 'payout_expired' | 'payout_reversed' | 'invoice_paid' | 'surcharge_payment_succeeded' | 'surcharge_refund_succeeded'; export type EventRecipient = 'merchant' | 'connector'; export type EventListConstraints = {created_after?: (string | null) | undefined, created_before?: (string | null) | undefined, limit?: (number | null) | undefined, offset?: (number | null) | undefined, object_id?: (string | null) | undefined, event_id?: (string | null) | undefined, profile_id?: (string | null) | undefined, event_classes?: (Array | null) | undefined, event_types?: (Array | null) | undefined, is_delivered?: (boolean | null) | undefined, recipient?: (EventRecipient | null) | undefined}; export type CreateApiEventsMerchantIdArgs = {merchant_id: string, body: EventListConstraints}; export type CreateApiEventsMerchantIdOptions = {baseUrl: string, adminApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const eventType = z.enum(["payment_succeeded", "payment_failed", "payment_processing", "payment_cancelled", "payment_cancelled_post_capture", "payment_authorized", "payment_partially_authorized", "payment_captured", "payment_expired", "action_required", "refund_succeeded", "refund_failed", "dispute_opened", "dispute_expired", "dispute_accepted", "dispute_cancelled", "dispute_challenged", "dispute_won", "dispute_lost", "mandate_active", "mandate_revoked", "payout_success", "payout_failed", "payout_initiated", "payout_processing", "payout_cancelled", "payout_expired", "payout_reversed", "invoice_paid", "surcharge_payment_succeeded", "surcharge_refund_succeeded"]); export const eventClass = z.enum(["payments", "refunds", "disputes", "mandates", "payouts", "subscriptions"]); export const eventListItemResponse = z.object({event_id: z.string().max(64), merchant_id: z.string().max(64), profile_id: z.string().max(64), object_id: z.string().max(64), event_type: eventType, event_class: eventClass, is_delivery_successful: z.boolean().nullable().optional(), initial_attempt_id: z.string().max(64), processor_merchant_id: z.string().max(64).nullable().optional(), created: z.string()}); export const totalEventsResponse = z.object({events: z.array(eventListItemResponse), total_count: z.number().int()}); 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 }; /** * Events - List * * POST /events/{merchant_id} */ export const createApiEventsMerchantId = async (args: CreateApiEventsMerchantIdArgs, options: CreateApiEventsMerchantIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, adminApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (adminApiKey !== undefined) headers.set("api-key", adminApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/events/${encodeURIComponent(String(args.merchant_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(totalEventsResponse, await res.json()) };