import {z} from 'zod' export type GetApiCloudAccountsAccountidOrdersArgs = {accountid: number, from?: string | undefined, to?: string | undefined, count?: number | undefined}; export type GetApiCloudAccountsAccountidOrdersOptions = {baseUrl?: string | undefined, Secured?: string | undefined, fetch?: typeof fetch | undefined}; export const order = z.object({orderid: z.number().int().optional(), buy: z.number().int().optional(), symbol: z.string().optional(), shares: z.number().optional(), price: z.number().optional(), number: z.string().optional(), status: z.number().int().optional(), statusname: z.string().optional(), timestamp: z.string().optional(), account: z.string().optional(), message: z.string().optional(), exchange: z.string().optional()}); export const getApiCloudAccountsAccountidOrdersResponse = z.array(order); 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 list by account * * GET /cloud/accounts/{accountid}/orders */ export const getApiCloudAccountsAccountidOrders = async (args: GetApiCloudAccountsAccountidOrdersArgs, options?: GetApiCloudAccountsAccountidOrdersOptions) => { const {baseUrl = "https://api.tradematic.com", fetch: fetchFn = fetch, Secured} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.count != null) query.append("count", String(args.count)) if (Secured !== undefined) headers.set("X-API-Key", Secured) const url = `${baseUrl.replace(/\/+$/, '')}/cloud/accounts/${encodeURIComponent(String(args.accountid))}/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(getApiCloudAccountsAccountidOrdersResponse, await res.json()) };