import {z} from 'zod' export type Currency = 'EUR' | 'USD' | 'GBP'; export type CreateWithdrawalRequest = {amount: number | string, currency: Currency, funding_account: string}; export type CreateApiAccountsAccountIdWithdrawalsArgs = {account_id: string, body: CreateWithdrawalRequest}; export type CreateApiAccountsAccountIdWithdrawalsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.enum(["EUR", "USD", "GBP"]); export const fundingAccount = z.object({bank_code: z.string(), created_at: z.string(), currency: currency, iban: z.string(), id: z.string(), is_verified: z.boolean().optional(), modified_at: z.string(), nickname: z.string().nullable().optional(), owner_name: z.string()}); export const withdrawalStatus = z.enum(["created", "completed"]); export const withdrawal = z.object({account: z.string(), amount: z.string(), completed_at: z.string().nullable().optional(), created_at: z.string(), currency: currency, funding_account: fundingAccount, id: z.string(), modified_at: z.string(), status: withdrawalStatus}); 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 }; /** * Create a withdrawal * * POST /accounts/{account_id}/withdrawals */ export const createApiAccountsAccountIdWithdrawals = async (args: CreateApiAccountsAccountIdWithdrawalsArgs, options?: CreateApiAccountsAccountIdWithdrawalsOptions) => { const {baseUrl = "https://api.joinsophic.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/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()) };