import {z} from 'zod' export type GetApiTransactionsArgs = {_limit: number, _offset: number, createdAt?: string | undefined, productType?: ('TOPUP' | 'ESIM' | 'VOUCHER' | 'BILL_PAY' | 'WALLET_RECHARGE' | 'REFUND' | 'BULK_CHECKOUT') | undefined, status?: ('ACCEPTED' | 'PENDING' | 'AUTHORIZED' | 'IN_PROGRESS' | 'DONE' | 'FAILED') | undefined, type?: ('CREDIT' | 'DEBIT') | undefined}; export type GetApiTransactionsOptions = {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 dtoTransactionStatus = z.enum(["ACCEPTED", "PENDING", "AUTHORIZED", "IN_PROGRESS", "DONE", "FAILED"]); export const dtoTransactionLogItem = z.object({dateTime: z.string(), status: dtoTransactionStatus, statusMessage: z.string()}); export const dtoProductType = z.enum(["TOPUP", "ESIM", "VOUCHER", "BILL_PAY", "WALLET_RECHARGE", "REFUND", "BULK_CHECKOUT"]); export const dtoTransactionType = z.enum(["CREDIT", "DEBIT"]); export const dtoTransaction = z.object({amount: z.number().int(), createdAt: z.string(), currency: z.string(), currencyDivisor: z.number().int(), error: dtoError.optional(), log: z.array(dtoTransactionLogItem), productType: dtoProductType, status: dtoTransactionStatus, transactionId: z.string(), type: dtoTransactionType, updatedAt: z.string()}); export const dtoTransactionsResponse = z.object({limit: z.number().int(), list: z.array(dtoTransaction), offset: z.number().int(), total: z.number().int()}); 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 list of transactions * * GET /transactions */ export const getApiTransactions = async (args: GetApiTransactionsArgs, options: GetApiTransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args._limit != null) query.append("_limit", String(args._limit)) if (args._offset != null) query.append("_offset", String(args._offset)) if (args.createdAt != null) query.append("createdAt", String(args.createdAt)) if (args.productType != null) query.append("productType", String(args.productType)) if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/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(dtoTransactionsResponse, await res.json()) };