import {z} from 'zod' export type GetApiApiV1BalanceHistoryArgs = {dateFrom?: string | undefined, dateTo?: string | undefined}; export type GetApiApiV1BalanceHistoryOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const channelCosts = z.object({mediaType: z.enum(["email", "sms", "viber", "mobilepush", "webpush", "appinbox", "widget", "inapp", "telegrambot"]).optional(), totalSent: z.number().int().optional(), totalParts: z.number().int().optional(), bonusSpent: z.number().int().optional(), fundsWithdrawal: z.number().optional(), fundsRefund: z.number().optional(), bonusReceived: z.number().int().optional(), reservedFundsWithdrawal: z.number().optional(), reservedFundsRefund: z.number().optional()}); export const billingHistoryDay = z.object({date: z.string().optional(), channelDetails: z.array(channelCosts).optional()}); export const billingHistory = z.object({currency: z.enum(["BYR", "CAD", "GBP", "EUR", "KZT", "RUB", "UAH", "USD"]).optional(), dailyCosts: z.array(billingHistoryDay).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 }; /** * Get organization billing history * * GET /api/v1/balance/history */ export const getApiApiV1BalanceHistory = async (args: GetApiApiV1BalanceHistoryArgs, options?: GetApiApiV1BalanceHistoryOptions) => { const {baseUrl = "https://api.reteno.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=UTF-8") if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/balance/history${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(billingHistory, await res.json()) };