import {z} from 'zod' export type GetApiTransactionArgs = {page?: number | undefined, per_page?: string | undefined, from?: string | undefined, to?: string | undefined, status?: ('pending' | 'failed' | 'completed' | 'reversed' | 'cancelled' | 'successful' | 'declined' | 'refunded' | 'chargeback' | 'chargeback_reversed') | undefined, currency?: ('USD' | 'NGN' | 'GBP' | 'EUR' | 'CNY' | 'KES') | undefined, account_id?: string | undefined, card_id?: string | undefined, linked_transaction_id?: string | undefined, kind?: string | undefined, include?: string | undefined}; export type GetApiTransactionOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTransactionResponse = z.object({data: z.array(z.object({id: z.string().optional(), account_id: z.string().optional(), kind: z.string().optional(), deposit_id: z.string().optional(), payout_id: z.unknown(), type: z.string().optional(), description: z.string().optional(), linked_transaction_id: z.unknown(), currency: z.string().optional(), amount: z.number().int().optional(), balance_before: z.number().int().optional(), balance_after: z.number().int().optional(), status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()})).optional(), message: z.string().optional(), meta: z.object({per_page: z.number().int().optional(), page: z.number().int().optional(), count_total: z.number().int().optional(), page_total: z.number().int().optional(), prev_page: z.boolean().optional(), next_page: z.boolean().optional()}).optional(), status: 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 }; /** * List Transactions * * GET /transaction */ export const getApiTransaction = async (args: GetApiTransactionArgs, options?: GetApiTransactionOptions) => { const {baseUrl = "https://api.useoval.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.status != null) query.append("status", String(args.status)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.card_id != null) query.append("card_id", String(args.card_id)) if (args.linked_transaction_id != null) query.append("linked_transaction_id", String(args.linked_transaction_id)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.include != null) query.append("include", String(args.include)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/transaction${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(getApiTransactionResponse, await res.json()) };