import {z} from 'zod' export type GetApiV1AccountsAccountIdBalancesArgs = {account_id: string, Accept: 'application/json'}; export type GetApiV1AccountsAccountIdBalancesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const balanceResponse = z.object({balances: z.object({option_short_value: z.number().optional(), total_equity: z.number().optional(), account_number: z.string().optional(), account_type: z.enum(["margin", "cash"]).optional(), close_pl: z.number().optional(), current_requirement: z.number().optional(), equity: z.number().optional(), long_market_value: z.number().optional(), market_value: z.number().optional(), open_pl: z.number().optional(), option_long_value: z.number().optional(), option_requirement: z.number().optional(), pending_orders_count: z.number().int().optional(), short_market_value: z.number().optional(), stock_long_value: z.number().optional(), total_cash: z.number().optional(), uncleared_funds: z.number().optional(), pending_cash: z.number().optional(), margin: z.object({fed_call: z.number().optional(), maintenance_call: z.number().optional(), option_buying_power: z.number().optional(), stock_buying_power: z.number().optional(), stock_short_value: z.number().optional(), sweep: z.number().optional()}).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 }; /** * Get Account Balance * * GET /v1/accounts/{account_id}/balances */ export const getApiV1AccountsAccountIdBalances = async (args: GetApiV1AccountsAccountIdBalancesArgs, options?: GetApiV1AccountsAccountIdBalancesOptions) => { const {baseUrl = "https://api.tradier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/balances${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(balanceResponse, await res.json()) };