import {z} from 'zod' export type ID = string; export type ClientBeneficiaryType = 'INDIVIDUAL' | 'BUSINESS'; export type AddressInput = {addressLine1: string, addressLine2?: (string | null) | undefined, city: string, region: string, postalCode?: (string | null) | undefined, country: string}; export type ClientBankingInstruction = {beneficiaryType: ClientBeneficiaryType, currency: string, beneficiaryAddress: AddressInput, beneficiaryBankName: string, beneficiaryBankAddress: AddressInput, beneficiaryBankBic?: (string | null) | undefined, routingCode?: (string | null) | undefined, beneficiaryAccountNumber: string, beneficiaryBankMemo?: (string | null) | undefined, intermediaryBankName?: (string | null) | undefined, intermediaryBankAddress?: AddressInput | undefined, intermediaryBankBic?: (string | null) | undefined, intermediaryAccountNumber?: (string | null) | undefined, beneficiaryGivenNames?: string | undefined, beneficiarySurnames?: string | undefined, beneficiaryLegalName?: (string | null) | undefined}; export type BankAccountInformationStatus = 'PENDING' | 'ACTIVE' | 'FAILED' | 'REMOVED' | 'COOLING_OFF' | 'COOLED_OFF'; export type StoredBankAccountInformation = {bankAccountId: string, bankAccountInformation: ClientBankingInstruction, status: BankAccountInformationStatus, createdAt?: string | undefined, statusReason?: (string | null) | undefined}; export type CreateApiV1WithdrawalsArgs = {'X-Request-Id'?: string | undefined, body: {accountId: ID, currency: string, quantity: string, bankAccount?: StoredBankAccountInformation | undefined, clientMemo?: (string | null) | undefined}}; export type CreateApiV1WithdrawalsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionType = z.enum(["DEPOSIT", "WITHDRAWAL", "TRANSFER"]); export const transactionStatus = z.enum(["FAILED", "PROCESSING", "CONFIRMING", "COMPLETED"]); export const iD = z.string(); export const financial = z.string(); export const withdrawalFeeType = z.enum(["FIXED", "VARIABLE", "LIVE"]); export const withdrawal = z.object({transactionType: transactionType, transactionStatus: transactionStatus.optional(), withdrawalId: iD, customerId: iD, accountId: iD, withdrawalCurrency: z.string(), netWithdrawalQuantity: financial, withdrawalFeeType: withdrawalFeeType, withdrawalFeeRate: financial, withdrawalFeeQuantity: financial, withdrawalFeeCurrency: z.string(), memo: z.string().nullable().optional(), txHash: z.string().nullable().optional(), clientMemo: z.string().nullable().optional(), grossWithdrawalQuantity: financial, requestedAt: z.string(), completedAt: 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 }; /** * Request withdrawal. * * POST /v1/withdrawals */ export const createApiV1Withdrawals = async (args: CreateApiV1WithdrawalsArgs, options?: CreateApiV1WithdrawalsOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Request-Id'] != null) headers.set("X-Request-Id", String(args['X-Request-Id'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/withdrawals${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(withdrawal, await res.json()) };