import {z} from 'zod' export type GetApiV1AccountsAccountIdHistoryArgs = {account_id: string, Accept: 'application/json', page?: number | undefined, limit?: number | undefined, type?: ('trade' | 'option' | 'ach' | 'wire' | 'dividend' | 'fee' | 'tax' | 'journal' | 'check' | 'transfer' | 'adjustment') | undefined}; export type GetApiV1AccountsAccountIdHistoryOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const historyEvent = z.object({date: z.string().optional(), type: z.enum(["trade", "option", "ach", "wire", "dividend", "fee", "tax", "journal", "check", "transfer", "adjustment"]).optional(), symbol: z.string().optional(), quantity: z.number().optional(), price: z.number().optional(), amount: z.number().optional(), description: z.string().optional(), commission: z.number().optional()}); export const historyResponse = z.object({history: z.object({event: z.array(historyEvent).optional(), page: z.number().int().optional(), total_pages: z.number().int().optional(), total_events: z.number().int().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 History * * GET /v1/accounts/{account_id}/history */ export const getApiV1AccountsAccountIdHistory = async (args: GetApiV1AccountsAccountIdHistoryArgs, options?: GetApiV1AccountsAccountIdHistoryOptions) => { 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 (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.type != null) query.append("type", String(args.type)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/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(historyResponse, await res.json()) };