@webtypen/webframez-core 0.3.23 → 0.3.24

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.
@@ -25,7 +25,23 @@ export declare class Datatable {
25
25
  logAggregation: boolean;
26
26
  defaultUnwind?: string | null;
27
27
  disablePerPageConfig: boolean;
28
+ selectable: boolean;
29
+ selectableFunctions: {
30
+ [key: string]: {
31
+ label: string;
32
+ icon?: string;
33
+ handle: Function;
34
+ isAvailable?: Function;
35
+ payload?: any;
36
+ };
37
+ } | null;
28
38
  getInit(req: Request): Promise<any>;
39
+ getSelectableFunctionsDef(req: Request): Promise<{
40
+ label: string;
41
+ icon: string | undefined;
42
+ apiFunction: string;
43
+ apiFunctionPayload: any;
44
+ }[] | null>;
29
45
  getAggregation(req: Request): Promise<any>;
30
46
  getSubAggregation(req: Request): Promise<any>;
31
47
  getCollection(req: Request): Promise<any>;
@@ -31,10 +31,16 @@ class Datatable {
31
31
  this.logAggregation = false;
32
32
  this.defaultUnwind = null;
33
33
  this.disablePerPageConfig = false;
34
+ this.selectable = false;
35
+ this.selectableFunctions = null;
34
36
  }
35
37
  getInit(req) {
36
38
  return __awaiter(this, void 0, void 0, function* () {
37
- const out = { table: req.body._table };
39
+ const out = {
40
+ table: req.body._table,
41
+ selectable: this.selectable ? true : false,
42
+ selectableFunctions: yield this.getSelectableFunctionsDef(req),
43
+ };
38
44
  const promises = [];
39
45
  promises.push(new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
40
46
  out.filter = yield this.getFilter(req);
@@ -72,6 +78,42 @@ class Datatable {
72
78
  return out;
73
79
  });
74
80
  }
81
+ getSelectableFunctionsDef(req) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ if (!this.selectableFunctions) {
84
+ return null;
85
+ }
86
+ const out = [];
87
+ for (let key in this.selectableFunctions) {
88
+ if (!this.selectableFunctions[key] || !this.selectableFunctions[key].handle) {
89
+ continue;
90
+ }
91
+ if (this.selectableFunctions[key].isAvailable && typeof this.selectableFunctions[key].isAvailable === "function") {
92
+ try {
93
+ // @ts-ignore
94
+ const available = yield this.selectableFunctions[key].isAvailable(req);
95
+ if (!available) {
96
+ continue;
97
+ }
98
+ }
99
+ catch (e) {
100
+ continue;
101
+ }
102
+ }
103
+ out.push({
104
+ label: this.selectableFunctions[key].label,
105
+ icon: this.selectableFunctions[key].icon,
106
+ apiFunction: key,
107
+ apiFunctionPayload: this.selectableFunctions[key].payload
108
+ ? typeof this.selectableFunctions[key].payload === "function"
109
+ ? yield this.selectableFunctions[key].payload(req)
110
+ : this.selectableFunctions[key].payload
111
+ : null,
112
+ });
113
+ }
114
+ return out && out.length > 0 ? out : null;
115
+ });
116
+ }
75
117
  getAggregation(req) {
76
118
  return __awaiter(this, void 0, void 0, function* () {
77
119
  const prefix = [];
@@ -14,6 +14,7 @@ const Controller_1 = require("../Controller/Controller");
14
14
  const DatatableRegistry_1 = require("./DatatableRegistry");
15
15
  class DatatableController extends Controller_1.Controller {
16
16
  restApi(req, res) {
17
+ var _a;
17
18
  return __awaiter(this, void 0, void 0, function* () {
18
19
  if (!req.body._table || req.body._table.trim() === "") {
19
20
  return res.status(404).send({
@@ -42,6 +43,45 @@ class DatatableController extends Controller_1.Controller {
42
43
  });
43
44
  }
44
45
  const table = new tableClass();
46
+ if (req.body.apiFunction && typeof req.body.apiFunction === "string" && req.body.apiFunction.trim() !== "") {
47
+ const func = (_a = table.selectableFunctions) === null || _a === void 0 ? void 0 : _a[req.body.apiFunction];
48
+ if (!func || !func.handle) {
49
+ return res.status(404).send({
50
+ status: "error",
51
+ message: "Unknown api-function ...",
52
+ });
53
+ }
54
+ if (func.isAvailable) {
55
+ try {
56
+ const available = yield func.isAvailable(req);
57
+ if (!available) {
58
+ return res.status(403).send({
59
+ status: "error",
60
+ message: "Api-function not available ...",
61
+ });
62
+ }
63
+ }
64
+ catch (e) {
65
+ return res.status(403).send({
66
+ status: "error",
67
+ message: e instanceof Error ? e.message : "Api-function not available ...",
68
+ });
69
+ }
70
+ }
71
+ try {
72
+ yield func.handle(req, res);
73
+ }
74
+ catch (e) {
75
+ console.error(e);
76
+ return res.status(500).send({
77
+ status: "error",
78
+ message: e instanceof Error ? e.message : "Unknown error ...",
79
+ });
80
+ }
81
+ return res.send({
82
+ status: "success",
83
+ });
84
+ }
45
85
  return res.send({
46
86
  status: "success",
47
87
  data: yield table.getData(req),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.3.23",
3
+ "version": "0.3.24",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -21,9 +21,17 @@ export class Datatable {
21
21
  logAggregation = false;
22
22
  defaultUnwind?: string | null = null;
23
23
  disablePerPageConfig = false;
24
+ selectable = false;
25
+ selectableFunctions: {
26
+ [key: string]: { label: string; icon?: string; handle: Function; isAvailable?: Function; payload?: any };
27
+ } | null = null;
24
28
 
25
29
  async getInit(req: Request) {
26
- const out: any = { table: req.body._table };
30
+ const out: any = {
31
+ table: req.body._table,
32
+ selectable: this.selectable ? true : false,
33
+ selectableFunctions: await this.getSelectableFunctionsDef(req),
34
+ };
27
35
  const promises = [];
28
36
  promises.push(
29
37
  new Promise(async (resolve) => {
@@ -71,6 +79,43 @@ export class Datatable {
71
79
  return out;
72
80
  }
73
81
 
82
+ async getSelectableFunctionsDef(req: Request) {
83
+ if (!this.selectableFunctions) {
84
+ return null;
85
+ }
86
+
87
+ const out = [];
88
+ for (let key in this.selectableFunctions) {
89
+ if (!this.selectableFunctions[key] || !this.selectableFunctions[key].handle) {
90
+ continue;
91
+ }
92
+
93
+ if (this.selectableFunctions[key].isAvailable && typeof this.selectableFunctions[key].isAvailable === "function") {
94
+ try {
95
+ // @ts-ignore
96
+ const available = await this.selectableFunctions[key].isAvailable(req);
97
+ if (!available) {
98
+ continue;
99
+ }
100
+ } catch (e) {
101
+ continue;
102
+ }
103
+ }
104
+
105
+ out.push({
106
+ label: this.selectableFunctions[key].label,
107
+ icon: this.selectableFunctions[key].icon,
108
+ apiFunction: key,
109
+ apiFunctionPayload: this.selectableFunctions[key].payload
110
+ ? typeof this.selectableFunctions[key].payload === "function"
111
+ ? await this.selectableFunctions[key].payload(req)
112
+ : this.selectableFunctions[key].payload
113
+ : null,
114
+ });
115
+ }
116
+ return out && out.length > 0 ? out : null;
117
+ }
118
+
74
119
  async getAggregation(req: Request) {
75
120
  const prefix: any = [];
76
121
  if (this.defaultUnwind && this.defaultUnwind.trim() !== "") {
@@ -36,6 +36,48 @@ export class DatatableController extends Controller {
36
36
  }
37
37
 
38
38
  const table = new tableClass();
39
+ if (req.body.apiFunction && typeof req.body.apiFunction === "string" && req.body.apiFunction.trim() !== "") {
40
+ const func = table.selectableFunctions?.[req.body.apiFunction];
41
+ if (!func || !func.handle) {
42
+ return res.status(404).send({
43
+ status: "error",
44
+ message: "Unknown api-function ...",
45
+ });
46
+ }
47
+
48
+ if (func.isAvailable) {
49
+ try {
50
+ const available = await func.isAvailable(req);
51
+ if (!available) {
52
+ return res.status(403).send({
53
+ status: "error",
54
+ message: "Api-function not available ...",
55
+ });
56
+ }
57
+ } catch (e) {
58
+ return res.status(403).send({
59
+ status: "error",
60
+ message: e instanceof Error ? e.message : "Api-function not available ...",
61
+ });
62
+ }
63
+ }
64
+
65
+ try {
66
+ await func.handle(req, res);
67
+ } catch (e) {
68
+ console.error(e);
69
+
70
+ return res.status(500).send({
71
+ status: "error",
72
+ message: e instanceof Error ? e.message : "Unknown error ...",
73
+ });
74
+ }
75
+
76
+ return res.send({
77
+ status: "success",
78
+ });
79
+ }
80
+
39
81
  return res.send({
40
82
  status: "success",
41
83
  data: await table.getData(req),