import {z} from 'zod' export type TransactionType = 0 | 2 | 3 | 6 | 11 | 14 | 15 | 19 | 20 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60; export type GetApiV1BillingTransactionsArgs = {from_date: string, to_date: string, type?: TransactionType | undefined, details_contains?: string | undefined, payments?: boolean | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiV1BillingTransactionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatus = z.enum(["Created", "Pending", "Committed", "Reverted"]); export const transactionType = z.union([z.literal(0), z.literal(2), z.literal(3), z.literal(6), z.literal(11), z.literal(14), z.literal(15), z.literal(19), z.literal(20), z.literal(23), z.literal(24), z.literal(25), z.literal(26), z.literal(29), z.literal(30), z.literal(31), z.literal(32), z.literal(33), z.literal(34), z.literal(35), z.literal(36), z.literal(37), z.literal(38), z.literal(39), z.literal(40), z.literal(41), z.literal(42), z.literal(43), z.literal(44), z.literal(45), z.literal(46), z.literal(47), z.literal(48), z.literal(49), z.literal(50), z.literal(51), z.literal(52), z.literal(53), z.literal(54), z.literal(55), z.literal(56), z.literal(57), z.literal(58), z.literal(59), z.literal(60)]); export const financialTransaction = z.object({id: z.number().int(), date: z.string(), amount: z.number(), balance_after: z.number(), details: z.string(), status: transactionStatus, type: transactionType, show_invoice: z.boolean()}); export const pagination = z.object({current_page: z.number().int(), per_page: z.number().int(), total: z.number().int(), total_pages: z.number().int()}); export const billingTransactionListResponse = z.object({is_empty: z.boolean(), transactions: z.array(financialTransaction), pagination: pagination}); 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 transactions * * GET /v1/billing/transactions */ export const getApiV1BillingTransactions = async (args: GetApiV1BillingTransactionsArgs, options?: GetApiV1BillingTransactionsOptions) => { const {baseUrl = "https://api.wavix.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.type != null) query.append("type", String(args.type)) if (args.details_contains != null) query.append("details_contains", String(args.details_contains)) if (args.payments != null) query.append("payments", String(args.payments)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing/transactions${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(billingTransactionListResponse, await res.json()) };