import {z} from 'zod' export type GetApiV2UserAccountsServerAccountIdInstrumentsArgs = {server: 'oanda' | 'oanda-demo' | 'otms' | 'v20' | 'v20-demo' | 'oj' | 'oj-demo' | 'opt' | 'ftmo-1', accountId: string, category?: string | undefined, ids?: Array | undefined, limit: number, offset: number}; export type GetApiV2UserAccountsServerAccountIdInstrumentsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2UserAccountsServerAccountIdInstrumentsResponse = z.object({count: z.number(), limit: z.number(), offset: z.number(), data: z.array(z.object({id: z.string(), name: z.string(), description: z.string(), category: z.enum(["CURRENCY", "STOCK", "FUTURES", "CRYPTO", "INDEX", "BOND", "COMMODITY", "EQUITY", "EQUITY_CZ", "EQUITY_UK", "EQUITY_ES", "EQUITY_PL", "EQUITY_DE", "EQUITY_NL", "EQUITY_FR", "EQUITY_BE", "EQUITY_DK", "EQUITY_FI", "EQUITY_PT", "EQUITY_SE", "EQUITY_US", "ETF"]), currency: z.string(), baseCurrency: z.string().optional(), pipLocation: z.number(), displayPrecision: z.number(), executionMode: z.enum(["MARKET", "EXCHANGE"]).optional(), tradeMode: z.enum(["TRADE_DISABLED", "TRADE_LONGONLY", "TRADE_SHORTONLY", "TRADE_CLOSEONLY", "TRADE_FULL"]), profitConvert: z.enum(["BY_MARKET", "BY_DEAL"]).optional(), type: z.enum(["CFD", "FOREX", "STOCK", "FUTURES", "CRYPTO", "INDEX", "BOND", "COMMODITY", "METAL", "ETF", "SIMULATED"]), fillType: z.array(z.enum(["FOK", "IOC", "RETURN"])).optional(), marketHours: z.array(z.object({type: z.enum(["MARKET_OPEN", "MARKET_CLOSE"]), timestamp: z.number()}))})).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 }; /** * Returns paginated instruments, sort alphabetically * * GET /v2/user/accounts/{server}/{accountId}/instruments */ export const getApiV2UserAccountsServerAccountIdInstruments = async (args: GetApiV2UserAccountsServerAccountIdInstrumentsArgs, options: GetApiV2UserAccountsServerAccountIdInstrumentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.category != null) query.append("category", String(args.category)) if (args.ids != null) query.append("ids", args.ids.join(',')) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/user/accounts/${encodeURIComponent(String(args.server))}/${encodeURIComponent(String(args.accountId))}/instruments${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(getApiV2UserAccountsServerAccountIdInstrumentsResponse, await res.json()) };