import {z} from 'zod' export type UpdateApiV1AccountsAccountIdOrdersOrderIdArgs = {account_id: string, 'Content-Type': 'application/x-www-form-urlencoded', order_id: number, body: {type?: ('market' | 'limit' | 'stop' | 'stop_limit') | undefined, duration?: ('day' | 'gtc' | 'pre' | 'post') | undefined, price?: number | undefined, stop?: number | undefined, tag?: string | undefined}}; export type UpdateApiV1AccountsAccountIdOrdersOrderIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const detailedOrder = 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(), last_fill_price: z.number().optional(), last_fill_quantity: z.number().optional(), remaining_quantity: z.number().optional()}); export const updateApiV1AccountsAccountIdOrdersOrderIdResponse = z.object({order: detailedOrder.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 }; /** * Change Order * * PUT /v1/accounts/{account_id}/orders/{order_id} */ export const updateApiV1AccountsAccountIdOrdersOrderId = async (args: UpdateApiV1AccountsAccountIdOrdersOrderIdArgs, options?: UpdateApiV1AccountsAccountIdOrdersOrderIdOptions) => { const {baseUrl = "https://api.tradier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/orders/${encodeURIComponent(String(args.order_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiV1AccountsAccountIdOrdersOrderIdResponse, await res.json()) };