import {z} from 'zod' export type GetApiMxvOrdersArgs = {accountType?: (0 | 1 | 2) | undefined, fromDate?: string | undefined, toDate?: string | undefined, status?: string | undefined, sortBy?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiMxvOrdersOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fireAntApiModelsMXVMXVTradeTransaction = z.object({status: z.string().optional(), filledPrice: z.number().optional(), qty: z.number().int().optional(), transactionFee: z.number().optional(), sessionDate: z.string().optional()}); export const fireAntApiModelsMXVMXVOrderInfo = z.object({clientOrderCode: z.string().optional(), orderCode: z.string().optional(), contractCode: z.string().optional(), contractName: z.string().optional(), side: z.string().optional(), orderType: z.string().optional(), qty: z.number().optional(), limitPrice: z.number().optional(), stopPrice: z.number().optional(), filledPrice: z.number().optional(), filledQty: z.number().optional(), orderDatetime: z.string().optional(), status: z.string().optional(), transactionFee: z.number().optional(), investorCode: z.string().optional(), sessionDate: z.string().optional(), dataSource: z.string().optional(), transactions: z.array(fireAntApiModelsMXVMXVTradeTransaction).optional()}); export const getApiMxvOrdersResponse = z.array(fireAntApiModelsMXVMXVOrderInfo); 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 }; /** * Danh sách lệnh * * GET /mxv/orders */ export const getApiMxvOrders = async (args: GetApiMxvOrdersArgs, options?: GetApiMxvOrdersOptions) => { const {baseUrl = "https://api.fireant.vn", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.accountType != null) query.append("accountType", String(args.accountType)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.status != null) query.append("status", String(args.status)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/mxv/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(getApiMxvOrdersResponse, await res.json()) };