import {z} from 'zod' export type CreateApiLedgerV1ReportSchedulesArgs = {body: {time: string, timezone: string, frequencyType: 'DAILY' | 'WEEKLY' | 'MONTHLY', frequencyDay?: number | undefined, format: 'CSV' | 'PDF' | 'JSON', channel: 'webhook' | 'email', email?: string | undefined, reportParams: {reportType: 'TRANSACTION' | 'TRADING_VOLUME' | 'PAYMENT_VOLUME' | 'DIGITAL_ASSETS_VOLUME' | 'PAYMENT' | 'WALLET_BALANCE', walletId?: string | undefined}}}; export type CreateApiLedgerV1ReportSchedulesOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiLedgerV1ReportSchedulesResponse = z.object({id: z.string().optional(), createdBy: z.string().optional(), name: z.string().optional(), accountReference: z.string().optional(), time: z.string().optional(), timezone: z.string().optional(), frequencyType: z.enum(["DAILY", "WEEKLY", "MONTHLY"]).optional(), frequencyDay: z.number().int().optional(), format: z.enum(["CSV", "PDF", "JSON"]).optional(), channel: z.enum(["WEBHOOK", "EMAIL"]).optional(), email: z.string().optional(), reportParams: z.object({reportType: z.literal("TRANSACTION").optional(), walletId: z.string().optional()}).optional(), createdAt: z.string().optional()}); 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 }; /** * Create report schedule * * POST /ledger/v1/report-schedules */ export const createApiLedgerV1ReportSchedules = async (args: CreateApiLedgerV1ReportSchedulesArgs, options?: CreateApiLedgerV1ReportSchedulesOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/v1/report-schedules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiLedgerV1ReportSchedulesResponse, await res.json()) };