import qs from 'qs' import { type PaginatedListRow, withRowKeys } from '@/components/paginated-list/paginatedListUtils' import axiosInstance from '@/config/axios' import { extractExportFileData, getItemsKeyFromUrl, parseRemittanceList } from '@/helpers' const paramsSerializer = (params: Record) => qs.stringify(params, { arrayFormat: 'brackets' }) export async function fetchPaginatedList({ itemsKey, page, params, rowsPerPage, signal, url, }: { itemsKey?: string page: number params: Record rowsPerPage: number signal?: AbortSignal url: string }) { const response = await axiosInstance.get(url, { params, paramsSerializer, signal }) const { items, pagination } = parseRemittanceList( response.data as Record, itemsKey ?? getItemsKeyFromUrl(url), page, rowsPerPage ) return { items: withRowKeys(items as PaginatedListRow[]), pagination: { page: Number(pagination.page), pageSize: Number(pagination.pageSize), totalItems: Number(pagination.totalItemsCount), }, } } export async function fetchPaginatedListExport(url: string, params: Record) { const response = await axiosInstance.get(url, { params, paramsSerializer }) const fileData = extractExportFileData(response.data) if (!fileData) throw new Error('Export file not found in response') return fileData }