@ejfdelgado/ejflab-back 1.24.2 → 1.25.0
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.
- package/package.json +1 -1
- package/srv/common/General.mjs +26 -0
package/package.json
CHANGED
package/srv/common/General.mjs
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
import md5 from 'md5';
|
2
2
|
|
3
3
|
export class General {
|
4
|
+
static PAGE_SIZE_DEFAULT = "20";
|
5
|
+
static PAGE_DIRECTION = "DESC";
|
6
|
+
static DIRECTION_CHOICES = ["asc", "desc"];
|
4
7
|
static readMaxOffset(req, MAX_READ_SIZE) {
|
5
8
|
const offsetR = parseInt(General.readParam(req, "offset"));
|
6
9
|
const maxR = parseInt(General.readParam(req, "max"));
|
@@ -66,4 +69,27 @@ export class General {
|
|
66
69
|
hash: md5(epochText),
|
67
70
|
};
|
68
71
|
}
|
72
|
+
|
73
|
+
static getPaginationArguments(req, orderColumnDef = null) {
|
74
|
+
const limit = parseInt(General.readParam(req, "limit", General.PAGE_SIZE_DEFAULT));
|
75
|
+
const orderColumn = General.readParam(req, "orderColumn", orderColumnDef);
|
76
|
+
const direction = General.readParam(req, "direction", General.PAGE_DIRECTION).toLowerCase();
|
77
|
+
const page = parseInt(General.readParam(req, "page", "0"));
|
78
|
+
if (!(typeof limit == "number")) {
|
79
|
+
throw new MalaPeticionException("limit is expected to be a number");
|
80
|
+
}
|
81
|
+
if (!(typeof page == "number")) {
|
82
|
+
throw new MalaPeticionException("page is expected to be a number");
|
83
|
+
}
|
84
|
+
if (General.DIRECTION_CHOICES.indexOf(direction) < 0) {
|
85
|
+
throw new MalaPeticionException(`direction is expected to be one of ${General.DIRECTION_CHOICES.join(', ')}`);
|
86
|
+
}
|
87
|
+
return {
|
88
|
+
limit,
|
89
|
+
orderColumn,
|
90
|
+
direction,
|
91
|
+
page,
|
92
|
+
offset: page * limit
|
93
|
+
};
|
94
|
+
}
|
69
95
|
}
|