import {z} from 'zod' export type ReportStatus = 'QUEUED' | 'PROCESSING' | 'COMPLETED' | 'FAILED'; export type ReportType = 'ADDRESSES'; export type GetApiReportsArgs = {pageCursor?: string | undefined, pageSize?: number | undefined, sortBy?: ('createdAt' | 'completedAt') | undefined, order?: ('ASC' | 'DESC') | undefined, status?: Array | undefined, reportType?: Array | undefined}; export type GetApiReportsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reportStatus = z.enum(["QUEUED", "PROCESSING", "COMPLETED", "FAILED"]); export const reportType = z.literal("ADDRESSES"); export const reportOutputFormat = z.literal("CSV"); export const reportJob = z.object({id: z.string(), status: reportStatus, reportType: reportType, outputFormat: reportOutputFormat, compress: z.boolean(), requestedByUserId: z.string(), createdAt: z.number().int(), completedAt: z.number().int().optional(), failedAt: z.number().int().optional(), rowCount: z.number().int().optional(), fileSizeBytes: z.number().int().optional()}); export const reportListResponse = z.object({data: z.array(reportJob), next: z.string().nullable(), prev: z.string().nullable()}); 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 }; /** * List reports * * GET /reports */ export const getApiReports = async (args: GetApiReportsArgs, options?: GetApiReportsOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageCursor != null) query.append("pageCursor", String(args.pageCursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.order != null) query.append("order", String(args.order)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.reportType ?? []) query.append("reportType", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/reports${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(reportListResponse, await res.json()) };