@russ-b/nestjs-common-tools
Version:
NestJS utility tools
45 lines • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pagination = void 0;
const common_1 = require("@nestjs/common");
const out_of_range_exception_1 = require("./exceptions/out-of-range.exception");
class Pagination {
static paginationParams(page, perPage) {
return { perPage, offset: Pagination.offset(page, perPage) };
}
static offset(page, perPage) {
return (page - 1) * perPage;
}
static createResponse({ page, perPage }, [data, total]) {
const pages = Math.ceil(total / perPage);
if (page > pages && pages > 0) {
throw new out_of_range_exception_1.OutOfRangeException();
}
return {
data,
pagination: {
total,
pages,
perPage,
page,
},
};
}
static response({ page, perPage }, [data, total]) {
const totalPages = Math.ceil(total / perPage);
if (page > totalPages && totalPages > 0) {
throw new common_1.BadRequestException('Page is out of range');
}
return {
data,
pagination: {
totalItems: total,
totalPages,
perPage,
page,
},
};
}
}
exports.Pagination = Pagination;
//# sourceMappingURL=pagination.js.map