import {z} from 'zod' export type GetApiApiV1TransactionsArgs = {currency?: ('EUR' | 'GBP' | 'GHS' | 'KES' | 'NGN' | 'TZS' | 'UGX' | 'USD' | 'XAF' | 'XOF') | undefined, end_date?: string | undefined, narrative?: string | undefined, page?: number | undefined, start_date?: string | undefined, trans_type?: ('ADJUSTMENT' | 'AIRTIME' | 'CHARGE' | 'DEPOSIT' | 'EXCHANGE' | 'PAYOUT' | 'SALE' | 'UNMARKED' | 'WITHDRAWAL') | undefined, wallet_id?: string | undefined}; export type GetApiApiV1TransactionsOptions = {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 paginatedTransactionSerList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(transactionSer)}); 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 /api/v1/transactions/ */ export const getApiApiV1Transactions = async (args: GetApiApiV1TransactionsArgs, options?: GetApiApiV1TransactionsOptions) => { 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 (args.currency != null) query.append("currency", String(args.currency)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.narrative != null) query.append("narrative", String(args.narrative)) if (args.page != null) query.append("page", String(args.page)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.trans_type != null) query.append("trans_type", String(args.trans_type)) if (args.wallet_id != null) query.append("wallet_id", String(args.wallet_id)) if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(paginatedTransactionSerList, await res.json()) };