import {z} from 'zod' export type GetApiApiV1BalancesHistoryArgs = {'x-request-id': string, 'x-api-key': string, 'x-user-key': string, displayCurrency?: string | undefined, fromDate?: string | undefined, toDate?: string | undefined, accountTypes?: string | undefined}; export type GetApiApiV1BalancesHistoryOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const balanceAggregatorApiAccountType = z.enum(["Trading", "Cash", "Options", "Crypto", "MoneyFarm", "Spaceship"]); export const historicalAccountData = z.object({accountId: z.string().nullable().optional(), accountType: balanceAggregatorApiAccountType.optional(), currency: z.string().nullable().optional(), cash: z.number().optional(), investedAmount: z.number().optional(), pnl: z.number().optional(), total: z.number().optional(), usdRate: z.number().optional(), displayCash: z.number().optional(), displayInvestedAmount: z.number().optional(), displayPnl: z.number().optional(), displayTotal: z.number().optional(), exchangeRate: z.number().optional()}); export const historicalDailySnapshotData = z.object({date: z.string().optional(), totalCurrencyIso: z.string().nullable().optional(), totalCash: z.number().optional(), totalInvestedAmount: z.number().optional(), totalPnl: z.number().optional(), totalBalance: z.number().optional(), displayTotalCash: z.number().optional(), displayTotalInvestedAmount: z.number().optional(), displayTotalPnl: z.number().optional(), displayTotalBalance: z.number().optional(), totalExchangeRate: z.number().optional(), accountSnapshots: z.array(historicalAccountData).nullable().optional()}); export const getHistoricalBalancesResponse = z.object({gcid: z.number().int().optional(), displayCurrency: z.string().nullable().optional(), fromDate: z.string().optional(), toDate: z.string().optional(), snapshots: z.array(historicalDailySnapshotData).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 }; /** * Get historical balance snapshots * * GET /api/v1/balances/history */ export const getApiApiV1BalancesHistory = async (args: GetApiApiV1BalancesHistoryArgs, options?: GetApiApiV1BalancesHistoryOptions) => { const {baseUrl = "https://public-api.etoro.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-request-id'] != null) headers.set("x-request-id", String(args['x-request-id'])) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['x-user-key'] != null) headers.set("x-user-key", String(args['x-user-key'])) if (args.displayCurrency != null) query.append("displayCurrency", String(args.displayCurrency)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.accountTypes != null) query.append("accountTypes", String(args.accountTypes)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/balances/history${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(getHistoricalBalancesResponse, await res.json()) };