import {z} from 'zod' export type WebhookEventDeliveryStatus = 'FAILED' | 'PROCESSING' | 'DELIVERED' | 'NOT_SUBSCRIBED'; export type GetApiV2WebhooksEventsArgs = {event_name?: string | undefined, status?: WebhookEventDeliveryStatus | undefined, account_id?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, size?: number | undefined, cursor?: string | undefined}; export type GetApiV2WebhooksEventsOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEventDeliveryStatus = z.enum(["FAILED", "PROCESSING", "DELIVERED", "NOT_SUBSCRIBED"]); export const webhookSubscriptionDeliveryStatus = z.enum(["FAILED", "PROCESSING", "DELIVERED"]); export const webhookEventSubscription = z.object({id: z.string(), status: webhookSubscriptionDeliveryStatus, active: z.boolean()}); export const webhookRecoveryEvent = z.object({id: z.string(), name: z.string(), account_id: z.string(), created_at: z.string(), payload: z.unknown(), status: webhookEventDeliveryStatus, subscriptions: z.array(webhookEventSubscription)}); export const webhookRecoveryEventList = z.object({events: z.array(webhookRecoveryEvent), 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 */ export const getApiV2WebhooksEvents = async (args: GetApiV2WebhooksEventsArgs, options?: GetApiV2WebhooksEventsOptions) => { 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.event_name != null) query.append("event_name", String(args.event_name)) if (args.status != null) query.append("status", String(args.status)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) 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${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(webhookRecoveryEventList, await res.json()) };