import {z} from 'zod' export type GetApiPortfolioAccountIdLedgerArgs = {accountId: string}; export type GetApiPortfolioAccountIdLedgerOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const ledger = z.object({commoditymarketvalue: z.number().optional(), futuremarketvalue: z.number().optional(), settledcash: z.number().optional(), exchangerate: z.number().optional(), sessionid: z.number().int().optional(), cashbalance: z.number().optional(), corporatebondsmarketvalue: z.number().optional(), warrantsmarketvalue: z.number().optional(), netliquidationvalue: z.number().optional(), interest: z.number().optional(), unrealizedpnl: z.number().optional(), stockmarketvalue: z.number().optional(), moneyfunds: z.number().optional(), currency: z.string().optional(), realizedpnl: z.number().optional(), funds: z.number().optional(), acctcode: z.string().optional(), issueroptionsmarketvalue: z.number().optional(), key: z.string().optional(), timestamp: z.number().int().optional(), severity: z.number().int().optional()}); export const getApiPortfolioAccountIdLedgerResponse = z.object({BASE: ledger.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 }; /** * Account Ledger * * GET /portfolio/{accountId}/ledger */ export const getApiPortfolioAccountIdLedger = async (args: GetApiPortfolioAccountIdLedgerArgs, options?: GetApiPortfolioAccountIdLedgerOptions) => { const {baseUrl = "http://localhost:5000/v1/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/portfolio/${encodeURIComponent(String(args.accountId))}/ledger${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(getApiPortfolioAccountIdLedgerResponse, await res.json()) };