import {z} from 'zod' export type OrderStatus = 'PLACED' | 'FILLED' | 'FAILED' | 'CANCELED' | 'PENDING_NEW' | 'NEW' | 'PARTIALLY_FILLED' | 'REJECTED' | 'PENDING_CANCEL' | 'CANCEL_FAILED' | 'EXPIRED'; export type SortDirection = 'ASC' | 'DESC'; export type GetApiV1OrdersArgs = {clientReference?: string | undefined, orderStatus?: Array | undefined, placedAtStart?: string | undefined, placedAtEnd?: string | undefined, completedAtStart?: string | undefined, completedAtEnd?: string | undefined, accountId?: string | undefined, pair?: string | undefined, side?: string | undefined, after?: string | undefined, limit?: unknown | undefined, sortField?: ('orderStatus' | 'placedAt' | 'completedAt') | undefined, sortDirection?: SortDirection | undefined}; export type GetApiV1OrdersOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const orderStatus = z.enum(["PLACED", "FILLED", "FAILED", "CANCELED", "PENDING_NEW", "NEW", "PARTIALLY_FILLED", "REJECTED", "PENDING_CANCEL", "CANCEL_FAILED", "EXPIRED"]); export const orderType = z.enum(["MARKET", "LIMIT", "PREVIOUSLY_QUOTED"]); export const timeInForceNullable = z.enum(["GTC", "GTD", "DAY"]); export const order = z.object({orderId: z.string().optional(), accountId: z.string().optional(), orderStatus: orderStatus.optional(), side: z.string().optional(), pair: z.string().optional(), strategy: z.literal("RFQ").optional(), limitPrice: z.string().nullable().optional(), orderPrice: z.string().optional(), leavesQty: z.string().optional(), orderQuantity: z.string().optional(), fillQuantity: z.string().optional(), cumulativeQty: z.string().optional(), fillPrice: z.string().optional(), avgPrice: z.string().nullable().optional(), orderType: orderType.optional(), timeInForce: timeInForceNullable.optional(), expireTime: z.string().nullable().optional(), clientOrderId: z.string().nullable().optional(), fillPercentage: z.string().optional(), clientFeeRate: z.string().nullable().optional(), clientFeeAmount: z.string().nullable().optional(), providerFeeRate: z.string().optional(), placedAt: z.string().optional(), completedAt: z.string().nullable().optional(), clientReference: z.string().nullable().optional(), rejectedAt: z.string().nullable().optional(), orderRejectReason: z.string().nullable().optional(), currency: z.string().optional()}); export const getApiV1OrdersResponse = z.object({after: z.string().nullable().optional(), totalCount: z.string().optional(), items: z.array(order).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 Orders * * GET /v1/orders */ export const getApiV1Orders = async (args: GetApiV1OrdersArgs, options?: GetApiV1OrdersOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.clientReference != null) query.append("clientReference", String(args.clientReference)) if (args.orderStatus != null) query.append("orderStatus", args.orderStatus.join(',')) if (args.placedAtStart != null) query.append("placedAtStart", String(args.placedAtStart)) if (args.placedAtEnd != null) query.append("placedAtEnd", String(args.placedAtEnd)) if (args.completedAtStart != null) query.append("completedAtStart", String(args.completedAtStart)) if (args.completedAtEnd != null) query.append("completedAtEnd", String(args.completedAtEnd)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.pair != null) query.append("pair", String(args.pair)) if (args.side != null) query.append("side", String(args.side)) if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders${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(getApiV1OrdersResponse, await res.json()) };