import {z} from 'zod' export type GetApiApiV1WalletsIdTransactionsArgs = {id: string}; export type GetApiApiV1WalletsIdTransactionsOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoicesSerStateEnum = z.enum(["PENDING", "PROCESSING", "FAILED", "CANCELED", "PARTIAL", "COMPLETE", "RETRY"]); export const provider906Enum = z.enum(["M-PESA", "PESALINK", "CARD-PAYMENT", "GOOGLE-PAY", "APPLE-PAY", "BITCOIN", "BANK-ACH", "COOP_B2B"]); export const invoicesSer = z.object({invoice_id: z.string(), state: invoicesSerStateEnum.optional(), provider: provider906Enum, charges: z.string().optional(), net_amount: z.string(), currency: z.string(), value: z.string(), account: z.string().max(140).nullable().optional(), api_ref: z.string().max(140).nullable().optional(), clearing_status: z.string(), mpesa_reference: z.string(), host: z.string().max(200).nullable().optional(), card_info: z.string(), retry_count: z.number().int().gte(-2147483648).lte(2147483647).optional(), failed_reason: z.string().nullable().optional(), failed_code: z.string().max(45).nullable().optional(), failed_code_link: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const transTypeEnum = z.enum(["SALE", "ADJUSTMENT", "PAYOUT", "WITHDRAWAL", "CHARGE", "AIRTIME", "DEPOSIT", "EXCHANGE", "UNMARKED"]); export const transactionSerStatusEnum = z.enum(["AVAILABLE", "CLEARING", "ON-HOLD", "CANCELLED", "CHARGEBACK-PENDING", "REFUNDED", "ADJUSTMENT"]); export const transactionSer = z.object({transaction_id: z.string(), invoice: invoicesSer, currency: z.string(), value: z.string(), running_balance: z.string(), narrative: z.string().nullable().optional(), trans_type: transTypeEnum.optional(), status: transactionSerStatusEnum.optional(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); 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 Wallet Statement * * GET /api/v1/wallets/{id}/transactions/ */ export const getApiApiV1WalletsIdTransactions = async (args: GetApiApiV1WalletsIdTransactionsArgs, options?: GetApiApiV1WalletsIdTransactionsOptions) => { const {baseUrl = "https://api.intasend.com", fetch: fetchFn = fetch, IntaSendBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/wallets/${encodeURIComponent(String(args.id))}/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(transactionSer, await res.json()) };