All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.client.pagination.secondary.RestPage.ts.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
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;
};




© 2015 - 2024 Weber Informatics LLC | Privacy Policy