import {z} from 'zod' export type DeleteApiCcpOrderArgs = {acct: string, id: number}; export type DeleteApiCcpOrderOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const orderData = z.object({clientOrderId: z.string().optional(), execId: z.string().optional(), execType: z.enum(["-1 "Invalid"", "0 "New"", "1 "Partially Filled"", "2 "Filled"", "3 "Done for the Day"", "4 "Canceled"", "5 "Replaced"", "6 "Pending Cancel"", "7 "Stopped"", "8 "Rejected"", "9 "Suspended"", "A "Pending New"", "B "Calculated"", "C "Expired"", "D "Restated"", "E "Pending Replace"", "F "Trade"", "G "Trade Correct"", "H "Trade Cancel"", "I "Order Status""]).optional(), orderType: z.enum(["1 "Market"", "2 "Limit"", "3 "Stop"", "4 "Stop Limit"", "5 "Market on Close"", "6 "With or Without"", "7 "Limit or Better"", "8 "Limit With or Without"", "9 "On Basis"", "A "On Close"", "B "Limit On Close"", "C "Forex Market"", "D "Previously Quoted"", "E "Previously Indicated"", "F "Forex Limit"", "G "Forex Swap"", "H "Forex Previously Quoted"", "I "Funari"", "K "Market To Limit"", "P "Pegged""]).optional(), orderStatus: z.enum(["-1 "Invalid Order Status"", "0 "New"", "1 "Partially Filled"", "2 "Filled"", "3 "Done for the Day"", "4 "Canceled"", "5 "Replaced"", "6 "Pending Cancel"", "7 "Stopped"", "8 "Rejected"", "9 "Suspended"", "A "Pending New"", "B "Calculated"", "C "Expired"", "D "Restated"", "E "Pending Replace""]).optional(), symbol: z.string().optional(), orderQty: z.string().optional(), price: z.string().optional(), lastShares: z.string().optional(), lastPrice: z.string().optional(), cumQty: z.string().optional(), leavesQty: z.string().optional(), avgPrice: z.string().optional(), side: z.enum(["1 "Buy"", "2 "Sell"", "3 "Buy Minus"", "4 "Sell Plus"", "5 "Sell Short"", "6 "Sell Short Exempt""]).optional(), orderId: z.string().optional(), account: z.string().optional(), secType: z.enum(["CS "Stock"", "CASH "Forex"", "CFD "Contract for Difference"", "FUT "Future"", "OPT "Option""]).optional(), txTime: z.string().optional(), rcptTime: z.string().optional(), tif: z.enum(["0 "Day"", "1 "GTC"", "2 "At the Opening""]).optional(), conid: z.string().optional(), currency: z.string().optional(), exchange: z.string().optional(), listingExchange: z.string().optional(), text: z.number().optional(), warnings: z.object({PRICECAP: z.string().optional(), TIME: z.string().optional()}).optional(), commCurr: z.string().optional(), comms: z.string().optional(), realizedPnl: 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 }; /** * Delete Order * * DELETE /ccp/order */ export const deleteApiCcpOrder = async (args: DeleteApiCcpOrderArgs, options?: DeleteApiCcpOrderOptions) => { const {baseUrl = "http://localhost:5000/v1/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.acct != null) query.append("acct", String(args.acct)) if (args.id != null) query.append("id", String(args.id)) const url = `${baseUrl.replace(/\/+$/, '')}/ccp/order${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(orderData, await res.json()) };