import {z} from 'zod' export type GetApiOrdersOrderIdOptions = {baseUrl?: string | undefined, oauthClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOrdersOrderIdResponse = z.object({id: z.string(), created_at: z.string(), updated_at: z.string(), user_id: z.string().optional(), business_id: z.string().optional(), account_id: z.string(), cash_amount: z.string(), currency: z.enum(["EUR", "GBP", "USD"]), side: z.enum(["BUY", "SELL"]), instrument_id: z.string(), instrument_id_type: z.literal("ISIN"), order_type: z.enum(["MARKET", "LIMIT", "STOP"]), quantity: z.string(), user_instrument_fit_acknowledgement: z.boolean().optional(), limit_price: z.string().optional(), stop_price: z.string().optional(), expiry_date: z.string().optional(), status: z.enum(["NEW", "PROCESSING", "FILLED", "CANCELLED"]), fee: z.string(), cancellation_reason: z.enum(["CANCELLED_BY_CLIENT", "CANCELLED_BY_UPVEST_OPERATIONS", "CANCELLED_BY_TRADING_PARTNER", "CANCELLED_BY_UPVEST_PLATFORM"]).optional(), initiation_flow: z.enum(["API", "PORTFOLIO", "CASH_DIVIDEND_REINVESTMENT", "PORTFOLIO_REBALANCING", "SELL_TO_COVER_FEES", "SELL_TO_COVER_TAXES", "ACCOUNT_LIQUIDATION", "UPVEST_OPERATIONS", "SAVINGS_PLAN", "CLIENT_OPERATIONS", "AUTO_INVESTMENT", "PORTFOLIO_SAVINGS_PLAN", "PORTFOLIO_AUTO_INVESTMENT"]), execution_flow: z.enum(["STRAIGHT_THROUGH", "BLOCK"]).optional(), executions: z.array(z.object({id: z.string(), cash_amount: z.string(), share_quantity: z.string(), price: z.string(), transaction_time: z.string(), taxes: z.array(z.object({type: z.literal("TOTAL"), amount: z.string()})), order_id: z.string(), status: z.enum(["FILLED", "SETTLED", "CANCELLED"]), side: z.enum(["BUY", "SELL"]), currency: z.enum(["EUR", "GBP", "USD"]), settlement_date: z.string().optional(), venue_id: z.string()})), client_reference: z.string().max(100).optional(), fee_configuration: z.array(z.union([z.object({type: z.enum(["TRANSACTION_FEE_BUY", "TRANSACTION_FEE_SELL"]), value_type: z.string(), charge_method: z.enum(["CHARGED_BY_CLIENT", "COLLECTED_BY_UPVEST"]), cash_amount: z.string(), currency: z.enum(["EUR", "GBP", "USD"])}), z.object({type: z.enum(["TRANSACTION_FEE_BUY", "TRANSACTION_FEE_SELL"]), transaction_fee_model_id: z.string()})])).optional(), decision_maker_id: 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 an order by ID * * GET /orders/{order_id} */ export const getApiOrdersOrderId = async (options?: GetApiOrdersOrderIdOptions) => { const {baseUrl = "https://sandbox.upvest.co", fetch: fetchFn = fetch, oauthClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauthClientCredentials !== undefined) headers.set('authorization', `Bearer ${oauthClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.order_id))}${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(getApiOrdersOrderIdResponse, await res.json()) };