import {z} from 'zod' export type GetApiV1AccountsAccountIdOrdersArgs = {account_id: string, Accept: 'application/json', status?: string | undefined, limit?: number | undefined, page?: number | undefined, includeTags?: boolean | undefined}; export type GetApiV1AccountsAccountIdOrdersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const order = z.object({id: z.number().int().optional(), type: z.enum(["market", "limit", "stop", "stop_limit", "debit", "credit", "even"]).optional(), symbol: z.string().optional(), side: z.enum(["buy", "buy_to_cover", "sell", "sell_short", "buy_to_open", "buy_to_close", "sell_to_open", "sell_to_close"]).optional(), quantity: z.number().optional(), status: z.enum(["pending", "open", "partially_filled", "filled", "expired", "canceled", "rejected", "pending_cancel"]).optional(), duration: z.enum(["day", "gtc", "pre", "post"]).optional(), avg_fill_price: z.number().optional(), exec_quantity: z.number().optional(), create_date: z.string().optional(), transaction_date: z.string().optional(), class: z.enum(["equity", "option", "multileg", "combo"]).optional()}); export const ordersResponse = z.object({orders: z.object({order: z.array(order).optional(), page: z.number().int().optional(), total_pages: z.number().int().optional(), total_orders: 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 Orders * * GET /v1/accounts/{account_id}/orders */ export const getApiV1AccountsAccountIdOrders = async (args: GetApiV1AccountsAccountIdOrdersArgs, options?: GetApiV1AccountsAccountIdOrdersOptions) => { 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.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.includeTags != null) query.append("includeTags", String(args.includeTags)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/orders${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(ordersResponse, await res.json()) };