import {z} from 'zod' export type GetApiV2UserAccountsServerAccountIdPricesArgs = {server: 'oanda' | 'oanda-demo' | 'otms' | 'v20' | 'v20-demo' | 'oj' | 'oj-demo' | 'opt' | 'ftmo-1', accountId: string, includeDepth?: boolean | undefined, includeHomeConversions?: boolean | undefined, includeDailyChanges?: boolean | undefined, instruments: Array}; export type GetApiV2UserAccountsServerAccountIdPricesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2UserAccountsServerAccountIdPricesResponse = z.array(z.object({instrument: z.string(), bids: z.array(z.object({price: z.number(), liquidity: z.number().optional()})), offers: z.array(z.object({price: z.number(), liquidity: z.number().optional()})), homeConversion: z.object({offer: z.number(), bid: z.number(), bidGain: z.number().optional(), bidLoss: z.number().optional(), offerGain: z.number().optional(), offerLoss: z.number().optional()}).optional(), dailyChanges: z.object({highPrice: z.number(), lowPrice: z.number(), change: z.object({pips: z.number(), percentage: z.number(), movement: z.enum(["UP", "DOWN", "NONE"])})}).optional(), timestamp: z.number().int(), chartPrice: z.number()})); 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}/prices */ export const getApiV2UserAccountsServerAccountIdPrices = async (args: GetApiV2UserAccountsServerAccountIdPricesArgs, options: GetApiV2UserAccountsServerAccountIdPricesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeDepth != null) query.append("includeDepth", String(args.includeDepth)) if (args.includeHomeConversions != null) query.append("includeHomeConversions", String(args.includeHomeConversions)) if (args.includeDailyChanges != null) query.append("includeDailyChanges", String(args.includeDailyChanges)) if (args.instruments != null) query.append("instruments", args.instruments.join(',')) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/user/accounts/${encodeURIComponent(String(args.server))}/${encodeURIComponent(String(args.accountId))}/prices${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(getApiV2UserAccountsServerAccountIdPricesResponse, await res.json()) };