generator.client.pagination.secondary.RestPage.ts.mustache Maven / Gradle / Ivy
import type { Page } from '@/shared/pagination/domain/Page';
export type RestData = {
data: T;
};
export type RestPage = {
currentPage: number;
pagesCount: number;
pageSize: number;
hasPrevious: boolean;
hasNext: boolean;
totalElementsCount: number;
content: T[];
};
export const toPage =
(mapper: (restData: Data) => Result) =>
(response: RestData>): Page => {
const data = response.data;
return {
currentPage: data.currentPage + 1,
pagesCount: data.pagesCount,
hasPrevious: data.hasPrevious,
hasNext: data.hasNext,
totalElementsCount: data.totalElementsCount,
displayed: {
start: startElementIndex(data),
end: Math.min((data.currentPage + 1) * data.pageSize, data.totalElementsCount),
},
content: data.content.map(mapper),
};
};
const startElementIndex = (data: RestPage): number => {
if (data.totalElementsCount === 0) {
return 0;
}
return data.currentPage * data.pageSize + 1;
};