import {z} from 'zod' export type FundingType = 'margin' | 'funded'; export type GetApiApiPrimeTradingV1AccountsAccountIdTradesArgs = {accountId: string, offset?: number | undefined, limit?: number | undefined, orderId?: string | undefined, dateGte?: string | undefined, dateLt?: string | undefined, creationDateGte?: string | undefined, creationDateLt?: string | undefined, isSettled?: boolean | undefined, fundingType?: FundingType | undefined}; export type GetApiApiPrimeTradingV1AccountsAccountIdTradesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const side = z.enum(["buy", "sell"]); export const fundingType = z.enum(["margin", "funded"]); export const accountId = z.string(); export const primeTrade = z.object({id: z.string(), orderId: z.string(), time: z.string(), creationDate: z.string(), product: z.string(), side: side, price: z.string(), quoteQuantity: z.string(), quantity: z.string(), fundingType: fundingType, settled: z.boolean(), settleDate: z.string().nullable().optional(), accountId: accountId, enterpriseId: z.string()}); export const trades = z.array(primeTrade); export const getApiApiPrimeTradingV1AccountsAccountIdTradesResponse = z.object({data: trades}); 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 Trades * * GET /api/prime/trading/v1/accounts/{accountId}/trades */ export const getApiApiPrimeTradingV1AccountsAccountIdTrades = async (args: GetApiApiPrimeTradingV1AccountsAccountIdTradesArgs, options: GetApiApiPrimeTradingV1AccountsAccountIdTradesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.orderId != null) query.append("orderId", String(args.orderId)) if (args.dateGte != null) query.append("dateGte", String(args.dateGte)) if (args.dateLt != null) query.append("dateLt", String(args.dateLt)) if (args.creationDateGte != null) query.append("creationDateGte", String(args.creationDateGte)) if (args.creationDateLt != null) query.append("creationDateLt", String(args.creationDateLt)) if (args.isSettled != null) query.append("isSettled", String(args.isSettled)) if (args.fundingType != null) query.append("fundingType", String(args.fundingType)) const url = `${baseUrl.replace(/\/+$/, '')}/api/prime/trading/v1/accounts/${encodeURIComponent(String(args.accountId))}/trades${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(getApiApiPrimeTradingV1AccountsAccountIdTradesResponse, await res.json()) };