import {z} from 'zod' export type GetApiApiV1BilaCollectionsArgs = {page?: number | undefined, perPage?: number | undefined, status?: ('pending' | 'successful' | 'failed' | 'otp-required' | 'pay-offline') | undefined, accountId?: string | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiApiV1BilaCollectionsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const bilaCollectionCustomerDto = z.object({phone: z.string(), name: z.string(), operator: z.string()}); export const bilaCollectionResponseDto = z.object({id: z.string(), reference: z.string(), status: z.enum(["pending", "successful", "failed", "otp-required", "pay-offline"]), amount: z.number(), currency: z.string(), feeBearer: z.enum(["merchant", "customer"]).optional(), narration: z.string().optional(), customer: bilaCollectionCustomerDto, 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 paginatedCollectionsResponseDto = z.object({data: z.array(bilaCollectionResponseDto), meta: paginationMetaDto}); export const getApiApiV1BilaCollectionsResponse = z.object({status: z.boolean(), message: z.string(), data: paginatedCollectionsResponseDto.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 collections * * GET /api/v1/bila/collections */ export const getApiApiV1BilaCollections = async (args: GetApiApiV1BilaCollectionsArgs, options?: GetApiApiV1BilaCollectionsOptions) => { 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.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/collections${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(getApiApiV1BilaCollectionsResponse, await res.json()) };