import {z} from 'zod' export type GetApiApiV1BilaTransfersArgs = {page?: number | undefined, perPage?: number | undefined, status?: ('pending' | 'successful' | 'failed') | undefined, type?: ('bank-account' | 'mobile-money') | undefined, accountId?: string | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiApiV1BilaTransfersOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transferRecipientDto = z.object({accountNumber: z.string().optional(), accountName: z.string(), bankName: z.string().optional(), phone: z.string().optional(), operator: z.string().optional()}); export const transferResponseDto = z.object({id: z.string(), reference: z.string(), type: z.enum(["bank-account", "mobile-money"]), status: z.enum(["pending", "successful", "failed"]), amount: z.number(), currency: z.string(), narration: z.string().optional(), recipient: transferRecipientDto, createdAt: z.string(), completedAt: z.string().optional()}); export const paginationMetaDto = z.object({total: z.number(), perPage: z.number(), currentPage: z.number(), pageCount: z.number()}); export const paginatedTransfersResponseDto = z.object({data: z.array(transferResponseDto), meta: paginationMetaDto}); export const getApiApiV1BilaTransfersResponse = z.object({status: z.boolean(), message: z.string(), data: paginatedTransfersResponseDto.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 transfers * * GET /api/v1/bila/transfers */ export const getApiApiV1BilaTransfers = async (args: GetApiApiV1BilaTransfersArgs, options?: GetApiApiV1BilaTransfersOptions) => { const {baseUrl = "https://api.usebila.com", fetch: fetchFn = fetch, xApiKey} = 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.perPage != null) query.append("perPage", String(args.perPage)) if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/bila/transfers${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(getApiApiV1BilaTransfersResponse, await res.json()) };