@x-spacy/pagination 1.0.8 → 1.0.11

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
+ /// <reference path="./enums/PaginationLinkType.d.ts" />
2
+ /// <reference path="./interceptors/PaginationInterceptor.d.ts" />
3
+ /// <reference path="./operators/paginate.d.ts" />
4
+ /// <reference path="./schemas/PaginatedResponse.d.ts" />
5
+ /// <reference path="./schemas/PaginationLink.d.ts" />
6
+ /// <reference path="./schemas/PaginationMeta.d.ts" />
@@ -0,0 +1,14 @@
1
+ import {
2
+ CallHandler,
3
+ ExecutionContext,
4
+ NestInterceptor
5
+ } from '@nestjs/common';
6
+
7
+ import { Observable } from 'rxjs';
8
+
9
+ import { Page } from '../schemas/Page';
10
+ import { PaginatedResponse } from '../schemas/PaginatedResponse';
11
+
12
+ export declare class PaginateInterceptor<T> implements NestInterceptor<Page<T>, PaginatedResponse<T>> {
13
+ intercept(context: ExecutionContext, next: CallHandler<Page<T>>): Observable<PaginatedResponse<T>>;
14
+ }
@@ -0,0 +1,3 @@
1
+ import { Page } from '../schemas/Page';
2
+
3
+ export declare function paginate<T>(items: Array<T>, total: number): Page<T>;
@@ -0,0 +1,5 @@
1
+ export class Page<T> {
2
+ public readonly items: Array<T>;
3
+
4
+ public readonly total: number;
5
+ }
@@ -0,0 +1,20 @@
1
+ import { PaginationLink } from './PaginationLink';
2
+ import { PaginationMeta } from './PaginationMeta';
3
+
4
+ export declare class PaginatedResponse<T> {
5
+ public readonly items: Array<T>;
6
+
7
+ public readonly meta: PaginationMeta;
8
+
9
+ public readonly path: string;
10
+
11
+ public readonly firstPageURL: string;
12
+
13
+ public readonly previousPageURL: string | null;
14
+
15
+ public readonly nextPageURL: string | null;
16
+
17
+ public readonly lastPageURL: string;
18
+
19
+ public readonly links: Array<PaginationLink>;
20
+ }
@@ -0,0 +1,11 @@
1
+ import { PaginationLinkType } from './PaginationLinkType';
2
+
3
+ export declare class PaginationLink {
4
+ public readonly url: string | null;
5
+
6
+ public readonly label: string;
7
+
8
+ public readonly type: PaginationLinkType;
9
+
10
+ public readonly active: boolean;
11
+ }
@@ -0,0 +1,13 @@
1
+ export declare class PaginationMeta {
2
+ public readonly from: number;
3
+
4
+ public readonly to: number;
5
+
6
+ public readonly currentPage: number;
7
+
8
+ public readonly lastPage: number;
9
+
10
+ public readonly perPage: number;
11
+
12
+ public readonly total: number;
13
+ }
@@ -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.11",
4
4
  "description": "A pagination library for NestJS",
5
5
  "license": "X SPACY LICENSE AGREEMENT",
6
6
  "private": false,
package/tsconfig.json CHANGED
@@ -2,16 +2,14 @@
2
2
  "compilerOptions": {
3
3
  "target": "es2024",
4
4
  "module": "commonjs",
5
- "rootDir": ".",
5
+ "rootDir": "src",
6
6
  "outDir": "dist",
7
- "declarationDir": "@types",
8
7
  "paths": {
9
8
  "@x-spacy/pagination/*": [
10
9
  "./src/*"
11
10
  ]
12
11
  },
13
12
  "strict": true,
14
- "declaration": true,
15
13
  "skipLibCheck": true,
16
14
  "removeComments": true,
17
15
  "esModuleInterop": true,