not-node 5.1.31 → 5.1.34

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "5.1.31",
3
+ "version": "5.1.34",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/src/form/form.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const FormFabric = require("./fabric");
2
-
2
+ const getIP = require("../auth").getIP;
3
3
  const { createSchemaFromFields } = require("../fields");
4
4
 
5
5
  const { objHas, isFunc } = require("../common");
@@ -14,6 +14,7 @@ const {
14
14
  } = require("../exceptions/form.js");
15
15
 
16
16
  const DEFAULT_EXTRACTORS = require("./extractors");
17
+ const DEFAULT_ID_EXTRACTORS = require("./id_extractors");
17
18
 
18
19
  /**
19
20
  * Generic form validation class
@@ -30,18 +31,36 @@ class Form {
30
31
  * @prop {string} name of form
31
32
  **/
32
33
  #FORM_NAME;
34
+ #MODEL_NAME;
33
35
  #PROTO_FIELDS;
34
36
  #VALIDATOR;
35
37
  #EXTRACTORS = {
36
38
  ...DEFAULT_EXTRACTORS,
37
39
  };
38
40
 
39
- constructor({ FIELDS, FORM_NAME, app, EXTRACTORS = {} }) {
41
+ #ID_EXTRACTORS = {
42
+ ...DEFAULT_ID_EXTRACTORS,
43
+ };
44
+
45
+ constructor({
46
+ FIELDS,
47
+ FORM_NAME,
48
+ MODEL_NAME,
49
+ app,
50
+ EXTRACTORS = {},
51
+ ID_EXTRACTORS = {},
52
+ }) {
40
53
  this.#FORM_NAME = FORM_NAME;
54
+ this.#MODEL_NAME = MODEL_NAME;
41
55
  this.#PROTO_FIELDS = FIELDS;
42
56
  this.#createValidationSchema(app);
43
57
  this.#augmentValidationSchema();
44
58
  this.#addExtractors(EXTRACTORS);
59
+ this.#addIdExtractors(ID_EXTRACTORS);
60
+ }
61
+
62
+ getModelName() {
63
+ return this.#MODEL_NAME;
45
64
  }
46
65
 
47
66
  /**
@@ -62,8 +81,34 @@ class Form {
62
81
  * @param {ExpressRequest} req expressjs request object
63
82
  * @return {Object} forma data
64
83
  **/
65
- async extract(/*req*/) {
66
- return {};
84
+ async extract(req) {
85
+ return {
86
+ ...this.extractRequestIds(req),
87
+ activeUser: req.user,
88
+ ip: getIP(req),
89
+ data: this.extractByInstructionsFromRouteActionFields(req),
90
+ };
91
+ }
92
+
93
+ #addIdExtractors(extractors = {}) {
94
+ if (extractors) {
95
+ this.#ID_EXTRACTORS = { ...this.#ID_EXTRACTORS, ...extractors };
96
+ }
97
+ }
98
+
99
+ extractRequestIds(req) {
100
+ const result = {};
101
+ Array.values(this.#ID_EXTRACTORS).forEach((extractor) => {
102
+ const extracted = extractor(this, req);
103
+ if (
104
+ extracted &&
105
+ typeof extracted.value !== "undefined" &&
106
+ extracted.name
107
+ ) {
108
+ result[extracted.name] = extracted.value;
109
+ }
110
+ });
111
+ return result;
67
112
  }
68
113
 
69
114
  /**
@@ -243,6 +288,41 @@ class Form {
243
288
  }
244
289
  return results;
245
290
  }
291
+
292
+ createInstructionFromRouteActionFields(
293
+ req,
294
+ mainInstruction = "fromBody",
295
+ exceptions = {}
296
+ ) {
297
+ const result = {};
298
+ if (
299
+ req?.notRouteData?.actionData?.fields &&
300
+ Array.isArray(req.notRouteData.actionData.fields)
301
+ ) {
302
+ const fields = req.notRouteData.actionData.fields.flat(2);
303
+ fields.forEach((fieldName) => {
304
+ if (objHas(exceptions, fieldName)) {
305
+ result[fieldName] = exceptions[fieldName];
306
+ } else {
307
+ result[fieldName] = mainInstruction;
308
+ }
309
+ });
310
+ }
311
+ return result;
312
+ }
313
+
314
+ extractByInstructionsFromRouteActionFields(
315
+ req,
316
+ mainInstruction = "fromBody",
317
+ exceptions = {}
318
+ ) {
319
+ const instructions = this.createInstructionFromRouteActionFields(
320
+ req,
321
+ mainInstruction,
322
+ exceptions
323
+ );
324
+ return this.extractByInstructions(req, instructions);
325
+ }
246
326
  }
247
327
 
248
328
  module.exports = Form;
@@ -0,0 +1,6 @@
1
+ module.exports = (form, req) => {
2
+ return {
3
+ name: "targetID",
4
+ value: req.params.ID,
5
+ };
6
+ };
@@ -0,0 +1,6 @@
1
+ module.exports = (form, req) => {
2
+ return {
3
+ name: "targetId",
4
+ value: req.params._id,
5
+ };
6
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ _id: require("./_id.js"),
3
+ ID: require("./ID.js"),
4
+ modelNameID: require("./modelNameID.js"),
5
+ };
@@ -0,0 +1,13 @@
1
+ const { firstLetterToLower } = require("../../common");
2
+
3
+ module.exports = (form, req) => {
4
+ const modelName = form.getModelName();
5
+ if (typeof modelName === "string") {
6
+ const idFieldName = `${firstLetterToLower(modelName)}ID`;
7
+ return {
8
+ name: "targetID",
9
+ value: req.params[idFieldName],
10
+ };
11
+ }
12
+ return undefined;
13
+ };
@@ -1,6 +1,7 @@
1
1
  module.exports.GenericLogic = require("./logic.js");
2
2
  module.exports.GenericRoute = require("./route.js");
3
3
  module.exports.GenericGetByIdForm = require("./form.getById.js");
4
+ module.exports.GenericGetByIDForm = require("./form.getByID.js");
4
5
  module.exports.GenericListAndCountForm = require("./form.listAndCount.js");
5
6
 
6
7
  const FORMS = {};