@x-spacy/pagination 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ export declare enum PaginationLinkType {
2
+ FIRST = "FIRST",
3
+ LAST = "LAST",
4
+ PREVIOUS = "PREVIOUS",
5
+ NEXT = "NEXT",
6
+ PAGE = "PAGE"
7
+ }
@@ -0,0 +1,6 @@
1
+ export * from '@x-spacy/pagination/interceptors/PaginationInterceptor';
2
+ export * from '@x-spacy/pagination/operators/paginate';
3
+ export * from '@x-spacy/pagination/schemas/Page';
4
+ export * from '@x-spacy/pagination/schemas/PaginatedResponse';
5
+ export * from '@x-spacy/pagination/schemas/PaginationLink';
6
+ export * from '@x-spacy/pagination/schemas/PaginationMeta';
@@ -0,0 +1,8 @@
1
+ import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
2
+ import { Page } from '@x-spacy/pagination/schemas/Page';
3
+ import { PaginatedResponse } from '@x-spacy/pagination/schemas/PaginatedResponse';
4
+ import { PaginationLink } from '@x-spacy/pagination/schemas/PaginationLink';
5
+ export declare class PaginateInterceptor<T> implements NestInterceptor<Page<T>, PaginatedResponse<T>> {
6
+ intercept(context: ExecutionContext, next: CallHandler<Page<T>>): import("rxjs").Observable<PaginatedResponse<T>>;
7
+ protected buildLinks(currentPage: number, lastPage: number, buildUrl: (page: number) => string): PaginationLink[];
8
+ }
@@ -0,0 +1,2 @@
1
+ import { Page } from '@x-spacy/pagination/schemas/Page';
2
+ export declare function paginate<T>(items: Array<T>, total: number): Page<T>;
@@ -0,0 +1,5 @@
1
+ export declare class Page<T> {
2
+ readonly items: Array<T>;
3
+ readonly total: number;
4
+ constructor(items: Array<T>, total: number);
5
+ }
@@ -0,0 +1,13 @@
1
+ import { PaginationLink } from '@x-spacy/pagination/schemas/PaginationLink';
2
+ import { PaginationMeta } from '@x-spacy/pagination/schemas/PaginationMeta';
3
+ export declare class PaginatedResponse<T> {
4
+ readonly items: Array<T>;
5
+ readonly meta: PaginationMeta;
6
+ readonly path: string;
7
+ readonly firstPageURL: string;
8
+ readonly previousPageURL: string | null;
9
+ readonly nextPageURL: string | null;
10
+ readonly lastPageURL: string;
11
+ readonly links: Array<PaginationLink>;
12
+ constructor(items: Array<T>, meta: PaginationMeta, path: string, firstPageURL: string, previousPageURL: string | null, nextPageURL: string | null, lastPageURL: string, links: Array<PaginationLink>);
13
+ }
@@ -0,0 +1,8 @@
1
+ import { PaginationLinkType } from '@x-spacy/pagination/enums/PaginationLinkType';
2
+ export declare class PaginationLink {
3
+ readonly url: string | null;
4
+ readonly label: string;
5
+ readonly type: PaginationLinkType;
6
+ readonly active: boolean;
7
+ constructor(url: string | null, label: string, type: PaginationLinkType, active: boolean);
8
+ }
@@ -0,0 +1,9 @@
1
+ export declare class PaginationMeta {
2
+ readonly from: number;
3
+ readonly to: number;
4
+ readonly currentPage: number;
5
+ readonly lastPage: number;
6
+ readonly perPage: number;
7
+ readonly total: number;
8
+ constructor(from: number, to: number, currentPage: number, lastPage: number, perPage: number, total: number);
9
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaginationLinkType = void 0;
4
+ var PaginationLinkType;
5
+ (function (PaginationLinkType) {
6
+ PaginationLinkType["FIRST"] = "FIRST";
7
+ PaginationLinkType["LAST"] = "LAST";
8
+ PaginationLinkType["PREVIOUS"] = "PREVIOUS";
9
+ PaginationLinkType["NEXT"] = "NEXT";
10
+ PaginationLinkType["PAGE"] = "PAGE";
11
+ })(PaginationLinkType || (exports.PaginationLinkType = PaginationLinkType = {}));
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./interceptors/PaginationInterceptor"), exports);
18
+ __exportStar(require("./operators/paginate"), exports);
19
+ __exportStar(require("./schemas/Page"), exports);
20
+ __exportStar(require("./schemas/PaginatedResponse"), exports);
21
+ __exportStar(require("./schemas/PaginationLink"), exports);
22
+ __exportStar(require("./schemas/PaginationMeta"), exports);
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.PaginateInterceptor = void 0;
10
+ const common_1 = require("@nestjs/common");
11
+ const operators_1 = require("rxjs/operators");
12
+ const Page_1 = require("../schemas/Page");
13
+ const PaginatedResponse_1 = require("../schemas/PaginatedResponse");
14
+ const PaginationLink_1 = require("../schemas/PaginationLink");
15
+ const PaginationMeta_1 = require("../schemas/PaginationMeta");
16
+ const PaginationLinkType_1 = require("../enums/PaginationLinkType");
17
+ let PaginateInterceptor = class PaginateInterceptor {
18
+ intercept(context, next) {
19
+ const request = context.switchToHttp().getRequest();
20
+ const page = Math.max(1, parseInt(request.query.page, 10) ?? 1);
21
+ const perPage = Math.min(100, Math.max(1, parseInt(request.query.perPage, 10) ?? 10));
22
+ const path = `${request.protocol}://${request.get('host') + request.path}`;
23
+ return next.handle().pipe((0, operators_1.map)((data) => {
24
+ if (!(data instanceof Page_1.Page)) {
25
+ return data;
26
+ }
27
+ const { total, items } = data;
28
+ const lastPage = Math.max(1, Math.ceil(total / perPage));
29
+ const currentPage = Math.min(page, lastPage);
30
+ const startIndex = (currentPage - 1) * perPage;
31
+ const endIndex = Math.min(startIndex + perPage, total);
32
+ const from = total > 0 ? startIndex + 1 : 0;
33
+ const to = endIndex;
34
+ const buildUrl = (pageNumber) => `${path}?page=${pageNumber}&perPage=${perPage}`;
35
+ const links = this.buildLinks(currentPage, lastPage, buildUrl);
36
+ return new PaginatedResponse_1.PaginatedResponse(items, new PaginationMeta_1.PaginationMeta(from, to, currentPage, lastPage, perPage, total), path, buildUrl(1), currentPage > 1 ? buildUrl(currentPage - 1) : null, currentPage < lastPage ? buildUrl(currentPage + 1) : null, buildUrl(lastPage), links);
37
+ }));
38
+ }
39
+ buildLinks(currentPage, lastPage, buildUrl) {
40
+ const links = new Array();
41
+ links.push(new PaginationLink_1.PaginationLink(currentPage > 1 ? buildUrl(currentPage - 1) : null, '&laquo; Anterior', PaginationLinkType_1.PaginationLinkType.PREVIOUS, false));
42
+ for (let i = 1; i <= lastPage; i++) {
43
+ links.push(new PaginationLink_1.PaginationLink(buildUrl(i), String(i), PaginationLinkType_1.PaginationLinkType.PAGE, i === currentPage));
44
+ }
45
+ links.push(new PaginationLink_1.PaginationLink(currentPage < lastPage ? buildUrl(currentPage + 1) : null, 'Próximo &raquo;', PaginationLinkType_1.PaginationLinkType.NEXT, false));
46
+ return links;
47
+ }
48
+ };
49
+ exports.PaginateInterceptor = PaginateInterceptor;
50
+ exports.PaginateInterceptor = PaginateInterceptor = __decorate([
51
+ (0, common_1.Injectable)()
52
+ ], PaginateInterceptor);
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.paginate = paginate;
4
+ const Page_1 = require("../schemas/Page");
5
+ function paginate(items, total) {
6
+ return new Page_1.Page(items, total);
7
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Page = void 0;
4
+ class Page {
5
+ items;
6
+ total;
7
+ constructor(items, total) {
8
+ this.items = items;
9
+ this.total = total;
10
+ }
11
+ }
12
+ exports.Page = Page;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PaginatedResponse = void 0;
13
+ const class_transformer_1 = require("class-transformer");
14
+ const PaginationMeta_1 = require("./PaginationMeta");
15
+ class PaginatedResponse {
16
+ items;
17
+ meta;
18
+ path;
19
+ firstPageURL;
20
+ previousPageURL;
21
+ nextPageURL;
22
+ lastPageURL;
23
+ links;
24
+ constructor(items, meta, path, firstPageURL, previousPageURL, nextPageURL, lastPageURL, links) {
25
+ this.items = items;
26
+ this.meta = meta;
27
+ this.path = path;
28
+ this.firstPageURL = firstPageURL;
29
+ this.previousPageURL = previousPageURL;
30
+ this.nextPageURL = nextPageURL;
31
+ this.lastPageURL = lastPageURL;
32
+ this.links = links;
33
+ }
34
+ }
35
+ exports.PaginatedResponse = PaginatedResponse;
36
+ __decorate([
37
+ (0, class_transformer_1.Expose)({ name: 'items' }),
38
+ __metadata("design:type", Array)
39
+ ], PaginatedResponse.prototype, "items", void 0);
40
+ __decorate([
41
+ (0, class_transformer_1.Expose)({ name: 'meta' }),
42
+ __metadata("design:type", PaginationMeta_1.PaginationMeta)
43
+ ], PaginatedResponse.prototype, "meta", void 0);
44
+ __decorate([
45
+ (0, class_transformer_1.Expose)({ name: 'path' }),
46
+ __metadata("design:type", String)
47
+ ], PaginatedResponse.prototype, "path", void 0);
48
+ __decorate([
49
+ (0, class_transformer_1.Expose)({ name: 'first_page_url' }),
50
+ __metadata("design:type", String)
51
+ ], PaginatedResponse.prototype, "firstPageURL", void 0);
52
+ __decorate([
53
+ (0, class_transformer_1.Expose)({ name: 'previous_page_url' }),
54
+ __metadata("design:type", Object)
55
+ ], PaginatedResponse.prototype, "previousPageURL", void 0);
56
+ __decorate([
57
+ (0, class_transformer_1.Expose)({ name: 'next_page_url' }),
58
+ __metadata("design:type", Object)
59
+ ], PaginatedResponse.prototype, "nextPageURL", void 0);
60
+ __decorate([
61
+ (0, class_transformer_1.Expose)({ name: 'last_page_url' }),
62
+ __metadata("design:type", String)
63
+ ], PaginatedResponse.prototype, "lastPageURL", void 0);
64
+ __decorate([
65
+ (0, class_transformer_1.Expose)({ name: 'links' }),
66
+ __metadata("design:type", Array)
67
+ ], PaginatedResponse.prototype, "links", void 0);
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PaginationLink = void 0;
13
+ const class_transformer_1 = require("class-transformer");
14
+ const PaginationLinkType_1 = require("../enums/PaginationLinkType");
15
+ class PaginationLink {
16
+ url;
17
+ label;
18
+ type;
19
+ active;
20
+ constructor(url, label, type, active) {
21
+ this.url = url;
22
+ this.label = label;
23
+ this.type = type;
24
+ this.active = active;
25
+ }
26
+ }
27
+ exports.PaginationLink = PaginationLink;
28
+ __decorate([
29
+ (0, class_transformer_1.Expose)({ name: 'url' }),
30
+ __metadata("design:type", Object)
31
+ ], PaginationLink.prototype, "url", void 0);
32
+ __decorate([
33
+ (0, class_transformer_1.Expose)({ name: 'label' }),
34
+ __metadata("design:type", String)
35
+ ], PaginationLink.prototype, "label", void 0);
36
+ __decorate([
37
+ (0, class_transformer_1.Expose)({ name: 'type' }),
38
+ __metadata("design:type", String)
39
+ ], PaginationLink.prototype, "type", void 0);
40
+ __decorate([
41
+ (0, class_transformer_1.Expose)({ name: 'active' }),
42
+ __metadata("design:type", Boolean)
43
+ ], PaginationLink.prototype, "active", void 0);
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PaginationMeta = void 0;
13
+ const class_transformer_1 = require("class-transformer");
14
+ class PaginationMeta {
15
+ from;
16
+ to;
17
+ currentPage;
18
+ lastPage;
19
+ perPage;
20
+ total;
21
+ constructor(from, to, currentPage, lastPage, perPage, total) {
22
+ this.from = from;
23
+ this.to = to;
24
+ this.currentPage = currentPage;
25
+ this.lastPage = lastPage;
26
+ this.perPage = perPage;
27
+ this.total = total;
28
+ }
29
+ }
30
+ exports.PaginationMeta = PaginationMeta;
31
+ __decorate([
32
+ (0, class_transformer_1.Expose)({ name: 'from' }),
33
+ __metadata("design:type", Number)
34
+ ], PaginationMeta.prototype, "from", void 0);
35
+ __decorate([
36
+ (0, class_transformer_1.Expose)({ name: 'to' }),
37
+ __metadata("design:type", Number)
38
+ ], PaginationMeta.prototype, "to", void 0);
39
+ __decorate([
40
+ (0, class_transformer_1.Expose)({ name: 'current_page' }),
41
+ __metadata("design:type", Number)
42
+ ], PaginationMeta.prototype, "currentPage", void 0);
43
+ __decorate([
44
+ (0, class_transformer_1.Expose)({ name: 'last_page' }),
45
+ __metadata("design:type", Number)
46
+ ], PaginationMeta.prototype, "lastPage", void 0);
47
+ __decorate([
48
+ (0, class_transformer_1.Expose)({ name: 'per_page' }),
49
+ __metadata("design:type", Number)
50
+ ], PaginationMeta.prototype, "perPage", void 0);
51
+ __decorate([
52
+ (0, class_transformer_1.Expose)({ name: 'total' }),
53
+ __metadata("design:type", Number)
54
+ ], PaginationMeta.prototype, "total", void 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-spacy/pagination",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "A pagination library for NestJS",
5
5
  "license": "X SPACY LICENSE AGREEMENT",
6
6
  "private": false,
package/tsconfig.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "compilerOptions": {
3
3
  "target": "es2024",
4
4
  "module": "commonjs",
5
- "rootDir": ".",
5
+ "rootDir": "src",
6
6
  "outDir": "dist",
7
7
  "declarationDir": "@types",
8
8
  "paths": {