import {z} from 'zod' export type GetApiV2UserAccountsServerAccountIdPortfolioOrdersArgs = {server: 'oanda' | 'oanda-demo' | 'otms' | 'v20' | 'v20-demo' | 'oj' | 'oj-demo' | 'opt' | 'ftmo-1', accountId: string, limit?: number | undefined, offset?: number | undefined, type?: ('HISTORY' | 'PENDING') | undefined, sort?: ('asc' | 'desc') | undefined, from?: string | undefined, to?: string | undefined, status?: ('FILLED' | 'PENDING' | 'CANCELED' | 'PARTIALLY_FILLED' | 'REPLACED' | 'REJECTED' | 'EXPIRED' | 'REQUEST_ADDED' | 'REQUEST_MODIFY' | 'REQUEST_CANCEL') | undefined, direction?: ('LONG' | 'SHORT') | undefined}; export type GetApiV2UserAccountsServerAccountIdPortfolioOrdersOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2UserAccountsServerAccountIdPortfolioOrdersResponse = z.array(z.object({accountId: z.string(), id: z.string(), parentId: z.string().optional(), instrument: z.string(), type: z.enum(["MARKET", "STOP", "LIMIT", "TAKE_PROFIT", "STOP_LOSS", "TRAILING_STOP_LOSS", "GUARANTEED_STOP_LOSS", "MARKET_IF_TOUCHED"]).optional(), price: z.number(), quantity: z.number(), executionTime: z.number().optional(), expiryTime: z.number().optional(), expiryType: z.enum(["GTC", "SPECIFIED", "DAY"]).nullable().optional(), creationTime: z.number().optional(), stopLoss: z.number().optional(), takeProfit: z.number().optional(), status: z.enum(["PENDING", "NEW", "PARTIALLY_FILLED", "FILLED", "CANCELED", "REPLACED", "REJECTED", "EXPIRED", "REQUEST_ADDED", "REQUEST_MODIFY", "REQUEST_CANCEL"]), direction: z.enum(["LONG", "SHORT"]), trailingStop: z.number().optional(), trailingStopValue: z.number().optional(), guaranteedStopLoss: z.number().optional(), commission: z.number().optional(), reason: z.string().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 /v2/user/accounts/{server}/{accountId}/portfolio/orders */ export const getApiV2UserAccountsServerAccountIdPortfolioOrders = async (args: GetApiV2UserAccountsServerAccountIdPortfolioOrdersArgs, options: GetApiV2UserAccountsServerAccountIdPortfolioOrdersOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.type != null) query.append("type", String(args.type)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.status != null) query.append("status", String(args.status)) if (args.direction != null) query.append("direction", String(args.direction)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/user/accounts/${encodeURIComponent(String(args.server))}/${encodeURIComponent(String(args.accountId))}/portfolio/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(getApiV2UserAccountsServerAccountIdPortfolioOrdersResponse, await res.json()) };