import {z} from 'zod' export type GetApiReportsTransactionsReportIdArgs = {reportId: string}; export type GetApiReportsTransactionsReportIdOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoError = z.object({code: z.string(), description: z.string(), message: z.string()}); export const dtoReportFile = z.object({downloadUrl: z.string(), name: z.string()}); export const dtoReportStatus = z.enum(["REQUESTED", "IN_PROGRESS", "READY", "FAILED"]); export const dtoReportLogItem = z.object({message: z.string(), recordedAt: z.string(), status: dtoReportStatus}); export const dtoReportTransactionsPeriod = z.object({endLt: z.string(), startGte: z.string()}); export const dtoReportTransactions = z.object({createdAt: z.string(), error: dtoError.optional(), file: dtoReportFile.optional(), log: z.array(dtoReportLogItem), period: dtoReportTransactionsPeriod, reportId: z.string(), status: dtoReportStatus, updatedAt: z.string()}); 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 transactions report by ID * * GET /reports/transactions/{reportId} */ export const getApiReportsTransactionsReportId = async (args: GetApiReportsTransactionsReportIdArgs, options: GetApiReportsTransactionsReportIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/reports/transactions/${encodeURIComponent(String(args.reportId))}${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(dtoReportTransactions, await res.json()) };