import {z} from 'zod' export type GetApiAccountsAccountIdWithdrawalsArgs = {account_id: string, cursor?: string | undefined, limit?: number | undefined, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiAccountsAccountIdWithdrawalsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const withdrawalStatus = z.enum(["accepted", "canceled", "confirmed", "created", "processed", "rejected"]); export const bankAccountResponse = z.object({iban: z.string(), bic: z.string(), name: z.string().optional()}); export const strongCustomerAuthenticationRequest = z.object({required: z.boolean(), challenge: z.string()}); export const statusChangeWithdrawalStatus = z.object({status: withdrawalStatus, timestamp: z.string()}); export const withdrawalResponse = z.object({id: z.string(), status: withdrawalStatus, amount: z.union([z.number().gt(0), z.string()]), currency: z.string().min(3).max(3), receiving_account: bankAccountResponse, sca: strongCustomerAuthenticationRequest.optional(), history: z.array(statusChangeWithdrawalStatus), created_at: z.string().optional()}); export const encodedPaginationResponse = z.object({next_cursor: z.string().optional()}); export const encodedPaginatedCollectionResponseWithdrawalResponse = z.object({data: z.array(withdrawalResponse), pagination: encodedPaginationResponse}); 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 }; /** * List Withdrawals * * GET /accounts/{account_id}/withdrawals */ export const getApiAccountsAccountIdWithdrawals = async (args: GetApiAccountsAccountIdWithdrawalsArgs, options?: GetApiAccountsAccountIdWithdrawalsOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/withdrawals${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(encodedPaginatedCollectionResponseWithdrawalResponse, await res.json()) };