import {z} from 'zod' export type GetApiAlgoOrdersAlgoIdArgs = {algo_id: string}; export type GetApiAlgoOrdersAlgoIdOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const responsesAlgoType = z.enum(["", "TWAP"]); export const responsesOrderSide = z.enum(["BUY", "SELL"]); export const responsesOrderStatus = z.enum(["NEW", "UNTRIGGERED", "OPEN", "CLOSED"]); export const responsesAlgoOrderResp = z.object({account: z.string().optional(), algo_params: z.record(z.string(), z.string()).optional(), algo_type: responsesAlgoType.optional(), avg_fill_price: z.string().optional(), cancel_reason: z.string().optional(), created_at: z.number().int().optional(), end_at: z.number().int().optional(), id: z.string().optional(), last_updated_at: z.number().int().optional(), market: z.string().optional(), remaining_size: z.string().optional(), side: responsesOrderSide.optional(), size: z.string().optional(), status: responsesOrderStatus.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 algo order by id * * GET /algo/orders/{algo_id} */ export const getApiAlgoOrdersAlgoId = async (args: GetApiAlgoOrdersAlgoIdArgs, options?: GetApiAlgoOrdersAlgoIdOptions) => { const {baseUrl = "https://api.prod.paradex.trade/v1", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerToken !== undefined) headers.set("Authorization", BearerToken) const url = `${baseUrl.replace(/\/+$/, '')}/algo/orders/${encodeURIComponent(String(args.algo_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(responsesAlgoOrderResp, await res.json()) };