import {z} from 'zod' export type GetApiV0WebhookEventsArgs = {after?: string | undefined, eventType?: ('account.internal.created' | 'deposit-order.created' | 'deposit-order.funded' | 'deposit-order.executed' | 'deposit-order.canceled' | 'deposit-order.retargeted' | 'withdrawal-order.pending' | 'withdrawal-order.executed' | 'withdrawal-order.canceled' | 'transfer-order.created' | 'transfer-order.executed' | 'swap-order.created' | 'swap-order.executed' | 'swap-order.canceled' | 'investor.created' | 'investor.updated' | 'investor.activated' | 'investor.made-compliant' | 'investor.made-non-compliant' | 'investor.archived' | 'bank-account.created' | 'bank-account.validated' | 'bank-account.invalidated' | 'bank-account.pending-validation' | 'bank-account.archived' | 'investor-invitation.accepted' | 'yield.credited' | 'subscription-order.created' | 'subscription-order.executed' | 'subscription-order.canceled' | 'subscription-order.wire-validated' | 'redemption-order.created' | 'redemption-order.executed' | 'redemption-order.canceled') | undefined, limit?: string | undefined}; export type GetApiV0WebhookEventsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const nonNegativeInt = z.number().int().gte(0).lte(9007199254740991); export const getApiV0WebhookEventsResponse = z.array(z.union([z.object({id: uUID, sequenceNumber: z.number(), webhookConfigurationId: uUID, distributorId: uUID, eventType: z.enum(["account.internal.created", "deposit-order.created", "deposit-order.funded", "deposit-order.executed", "deposit-order.canceled", "deposit-order.retargeted", "withdrawal-order.pending", "withdrawal-order.executed", "withdrawal-order.canceled", "transfer-order.created", "transfer-order.executed", "swap-order.created", "swap-order.executed", "swap-order.canceled", "investor.created", "investor.updated", "investor.activated", "investor.made-compliant", "investor.made-non-compliant", "investor.archived", "bank-account.created", "bank-account.validated", "bank-account.invalidated", "bank-account.pending-validation", "bank-account.archived", "investor-invitation.accepted", "yield.credited", "subscription-order.created", "subscription-order.executed", "subscription-order.canceled", "subscription-order.wire-validated", "redemption-order.created", "redemption-order.executed", "redemption-order.canceled"]), payload: z.unknown(), retryCount: nonNegativeInt, createdAt: z.string(), status: z.literal("pending"), httpStatusCode: z.number().int().gte(100).lte(599).nullable(), lastAttemptAt: z.string().nullable(), deliveredAt: z.string().nullable()}), z.object({id: uUID, sequenceNumber: z.number(), webhookConfigurationId: uUID, distributorId: uUID, eventType: z.enum(["account.internal.created", "deposit-order.created", "deposit-order.funded", "deposit-order.executed", "deposit-order.canceled", "deposit-order.retargeted", "withdrawal-order.pending", "withdrawal-order.executed", "withdrawal-order.canceled", "transfer-order.created", "transfer-order.executed", "swap-order.created", "swap-order.executed", "swap-order.canceled", "investor.created", "investor.updated", "investor.activated", "investor.made-compliant", "investor.made-non-compliant", "investor.archived", "bank-account.created", "bank-account.validated", "bank-account.invalidated", "bank-account.pending-validation", "bank-account.archived", "investor-invitation.accepted", "yield.credited", "subscription-order.created", "subscription-order.executed", "subscription-order.canceled", "subscription-order.wire-validated", "redemption-order.created", "redemption-order.executed", "redemption-order.canceled"]), payload: z.unknown(), retryCount: nonNegativeInt, createdAt: z.string(), status: z.literal("delivered"), httpStatusCode: z.number().int().gte(100).lte(599), lastAttemptAt: z.string(), deliveredAt: z.string()}), z.object({id: uUID, sequenceNumber: z.number(), webhookConfigurationId: uUID, distributorId: uUID, eventType: z.enum(["account.internal.created", "deposit-order.created", "deposit-order.funded", "deposit-order.executed", "deposit-order.canceled", "deposit-order.retargeted", "withdrawal-order.pending", "withdrawal-order.executed", "withdrawal-order.canceled", "transfer-order.created", "transfer-order.executed", "swap-order.created", "swap-order.executed", "swap-order.canceled", "investor.created", "investor.updated", "investor.activated", "investor.made-compliant", "investor.made-non-compliant", "investor.archived", "bank-account.created", "bank-account.validated", "bank-account.invalidated", "bank-account.pending-validation", "bank-account.archived", "investor-invitation.accepted", "yield.credited", "subscription-order.created", "subscription-order.executed", "subscription-order.canceled", "subscription-order.wire-validated", "redemption-order.created", "redemption-order.executed", "redemption-order.canceled"]), payload: z.unknown(), retryCount: nonNegativeInt, createdAt: z.string(), status: z.literal("failed"), httpStatusCode: z.number().int().gte(100).lte(599).nullable(), lastAttemptAt: z.string().nullable(), deliveredAt: z.string().nullable(), failureReason: z.enum(["configuration-circuit-broken", "configuration-disabled", "delivery-attempts-exhausted", "permanent-failure", "retry-scheduling-failed"]).nullable()})])); 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 }; /** * Poll webhook events * * GET /v0/webhook-events */ export const getApiV0WebhookEvents = async (args: GetApiV0WebhookEventsArgs, options?: GetApiV0WebhookEventsOptions) => { const {baseUrl = "https://distributor-api.spiko.io", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.eventType != null) query.append("eventType", String(args.eventType)) if (args.limit != null) query.append("limit", String(args.limit)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v0/webhook-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(getApiV0WebhookEventsResponse, await res.json()) };