import {z} from 'zod' export type PageLimitParam = number | string; export type PageOffsetParam = number | string; export type GetApiApiImportTaskListArgs = {limit?: PageLimitParam | undefined, offset?: PageOffsetParam | undefined, withCompleted?: string | undefined}; export type GetApiApiImportTaskListOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const importHistoryTaskListResponse = z.object({list: z.array(z.object({id: z.string(), payload: z.object({after: z.number(), before: z.number(), source: z.enum(["binance", "bitget", "bybit"]), instrument: z.string(), coefficient: z.number().nullable(), contractType: z.string().nullable(), sourceInstrument: z.string().nullable()}), status: z.enum(["pending", "process", "done", "error"]), error: z.string().nullable(), startAt: z.string()})), count: z.number()}); 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 /api/import-task/list */ export const getApiApiImportTaskList = async (args: GetApiApiImportTaskListArgs, options?: GetApiApiImportTaskListOptions) => { const {baseUrl = "https://market-data-api.evedex.com", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.withCompleted != null) query.append("withCompleted", String(args.withCompleted)) if (AccessToken !== undefined) headers.set('authorization', `Bearer ${AccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/import-task/list${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(importHistoryTaskListResponse, await res.json()) };