import {z} from 'zod' export type EnterpriseIdString = string; export type GetApiApiV2EnterpriseEnterpriseIdWebhookWebhookIdBankaccountBankaccountIdSimulateArgs = {enterpriseId: EnterpriseIdString, webhookId: string, bankaccountId: string}; export type GetApiApiV2EnterpriseEnterpriseIdWebhookWebhookIdBankaccountBankaccountIdSimulateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const webhookType = z.enum(["txRequest", "txRequestTransaction", "transfer", "transaction", "transactionRemoved", "transactionExpire", "pendingapproval", "block", "admin", "address_confirmation", "lowFee", "circuitBreaker", "lowFeeAddressBalance", "wallet_confirmation", "bankAccount", "userKycState", "enterpriseKycState", "identityStatus", "idvStatus", "accessToken", "accountCreated", "contractSigned", "kycResult", "policyBalanceChange", "reversal", "mintOrder", "transaction_finality_on_l1", "stuckTx", "tradeOrder", "unspentExpiry", "twoStepTransfer", "twoStepDeposit", "twoStepWithdrawal", "endInvestorOnboardingInvite", "endInvestorOnboardingAccepted", "pendingDecryption", "whitelistApproved", "whitelistRejected", "allocationRequest", "allocationAccepted", "allocationSettlementFailed"]); export const webhookNotificationState = z.enum(["unconfirmed", "new", "pending", "processed", "failed"]); export const simulateEnterpriseWebhookResponseBody = z.object({webhookNotifications: z.array(z.object({id: z.string(), type: webhookType.optional(), wallet: z.string().optional(), safeId: z.string().optional(), url: z.string().optional(), hash: z.string().optional(), coin: z.string().optional(), coinChain: z.string().optional(), transfer: z.string().optional(), state: webhookNotificationState.optional(), simulation: z.boolean().optional(), retries: z.number().optional(), webhook: z.string().optional(), updatedAt: z.string().optional(), nextAttempt: z.string().optional(), version: z.number().optional(), allowBlockedHosts: z.boolean().optional(), heightToConfirm: z.number().optional(), payload: z.string().optional(), response: z.object({code: z.union([z.number(), z.string()]).optional(), type: z.string().optional(), body: z.string().optional(), error: z.string().optional()}).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 }; /** * Simulate bank account webhook * * GET /api/v2/enterprise/{enterpriseId}/webhook/{webhookId}/bankaccount/{bankaccountId}/simulate */ export const getApiApiV2EnterpriseEnterpriseIdWebhookWebhookIdBankaccountBankaccountIdSimulate = async (args: GetApiApiV2EnterpriseEnterpriseIdWebhookWebhookIdBankaccountBankaccountIdSimulateArgs, options: GetApiApiV2EnterpriseEnterpriseIdWebhookWebhookIdBankaccountBankaccountIdSimulateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/enterprise/${encodeURIComponent(String(args.enterpriseId))}/webhook/${encodeURIComponent(String(args.webhookId))}/bankaccount/${encodeURIComponent(String(args.bankaccountId))}/simulate${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(simulateEnterpriseWebhookResponseBody, await res.json()) };