not-node 5.1.32 → 5.1.35

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.32",
3
+ "version": "5.1.35",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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,6 @@
1
+ module.exports = (form, req) => {
2
+ return {
3
+ name: "activeUser",
4
+ value: req.user,
5
+ };
6
+ };
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ _id: require("./_id.js"),
3
+ ID: require("./ID.js"),
4
+ activeUser: require("./activeUser.js"),
5
+ ip: require("./ip.js"),
6
+ query: require("./query.js"),
7
+ modelNameID: require("./modelNameID.js"),
8
+ };
@@ -0,0 +1,8 @@
1
+ const getIP = require("../../auth").getIP;
2
+
3
+ module.exports = (form, req) => {
4
+ return {
5
+ name: "ip",
6
+ value: getIP(req),
7
+ };
8
+ };
@@ -0,0 +1,13 @@
1
+ const { firstLetterToLower } = require("../../common");
2
+
3
+ module.exports = (form, req) => {
4
+ const modelName = form.getModelName(req);
5
+ if (typeof modelName === "string") {
6
+ const idFieldName = `${firstLetterToLower(modelName)}ID`;
7
+ return {
8
+ name: idFieldName,
9
+ value: req.params[idFieldName],
10
+ };
11
+ }
12
+ return undefined;
13
+ };
@@ -0,0 +1,24 @@
1
+ const notFilter = require("not-filter");
2
+ const getApp = require("../../getApp");
3
+
4
+ module.exports = (form, req) => {
5
+ const MODULE_NAME = form.getModuleName();
6
+ const MODEL_NAME = form.getModelName(req);
7
+ if (MODEL_NAME && MODULE_NAME) {
8
+ const thisSchema = getApp().getModelSchema(
9
+ `${MODULE_NAME}//${MODEL_NAME}`
10
+ );
11
+ if (thisSchema) {
12
+ const { skip, size } = notFilter.pager.process(req), //skip,size
13
+ sorter = notFilter.sorter.process(req, thisSchema),
14
+ search = notFilter.search.process(req, thisSchema);
15
+ let filter = notFilter.filter.process(req, thisSchema);
16
+
17
+ return {
18
+ name: "query",
19
+ value: { skip, size, sorter, search, filter },
20
+ };
21
+ }
22
+ }
23
+ return undefined;
24
+ };
package/src/form/form.js CHANGED
@@ -2,7 +2,7 @@ const FormFabric = require("./fabric");
2
2
 
3
3
  const { createSchemaFromFields } = require("../fields");
4
4
 
5
- const { objHas, isFunc } = require("../common");
5
+ const { objHas, isFunc, firstLetterToUpper } = require("../common");
6
6
 
7
7
  const ValidationBuilder = require("not-validation").Builder;
8
8
  const ValidationSession = require("not-validation").Session;
@@ -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("./env_extractors");
17
18
 
18
19
  /**
19
20
  * Generic form validation class
@@ -30,18 +31,41 @@ 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
+ #ENV_EXTRACTORS = {
42
+ ...DEFAULT_ID_EXTRACTORS,
43
+ };
44
+
45
+ constructor({
46
+ FIELDS,
47
+ FORM_NAME,
48
+ MODEL_NAME,
49
+ app,
50
+ EXTRACTORS = {},
51
+ ENV_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.#addEnvExtractors(ENV_EXTRACTORS);
60
+ }
61
+
62
+ getModelName(req) {
63
+ if (this.#MODEL_NAME) {
64
+ return this.#MODEL_NAME;
65
+ } else if (req) {
66
+ return firstLetterToUpper(req.notRouteData.modelName);
67
+ }
68
+ return undefined;
45
69
  }
46
70
 
47
71
  /**
@@ -62,8 +86,32 @@ class Form {
62
86
  * @param {ExpressRequest} req expressjs request object
63
87
  * @return {Object} forma data
64
88
  **/
65
- async extract(/*req*/) {
66
- return {};
89
+ async extract(req) {
90
+ return {
91
+ ...this.extractRequestEnvs(req),
92
+ data: this.extractByInstructionsFromRouteActionFields(req),
93
+ };
94
+ }
95
+
96
+ #addEnvExtractors(extractors = {}) {
97
+ if (extractors) {
98
+ this.#ENV_EXTRACTORS = { ...this.#ENV_EXTRACTORS, ...extractors };
99
+ }
100
+ }
101
+
102
+ extractRequestEnvs(req) {
103
+ const result = {};
104
+ Array.values(this.#ENV_EXTRACTORS).forEach((extractor) => {
105
+ const extracted = extractor(this, req);
106
+ if (
107
+ extracted &&
108
+ typeof extracted.value !== "undefined" &&
109
+ extracted.name
110
+ ) {
111
+ result[extracted.name] = extracted.value;
112
+ }
113
+ });
114
+ return result;
67
115
  }
68
116
 
69
117
  /**
@@ -243,6 +291,41 @@ class Form {
243
291
  }
244
292
  return results;
245
293
  }
294
+
295
+ createInstructionFromRouteActionFields(
296
+ req,
297
+ mainInstruction = "fromBody",
298
+ exceptions = {}
299
+ ) {
300
+ const result = {};
301
+ if (
302
+ req?.notRouteData?.actionData?.fields &&
303
+ Array.isArray(req.notRouteData.actionData.fields)
304
+ ) {
305
+ const fields = req.notRouteData.actionData.fields.flat(2);
306
+ fields.forEach((fieldName) => {
307
+ if (objHas(exceptions, fieldName)) {
308
+ result[fieldName] = exceptions[fieldName];
309
+ } else {
310
+ result[fieldName] = mainInstruction;
311
+ }
312
+ });
313
+ }
314
+ return result;
315
+ }
316
+
317
+ extractByInstructionsFromRouteActionFields(
318
+ req,
319
+ mainInstruction = "fromBody",
320
+ exceptions = {}
321
+ ) {
322
+ const instructions = this.createInstructionFromRouteActionFields(
323
+ req,
324
+ mainInstruction,
325
+ exceptions
326
+ );
327
+ return this.extractByInstructions(req, instructions);
328
+ }
246
329
  }
247
330
 
248
331
  module.exports = Form;
@@ -77,6 +77,7 @@ class notRoute {
77
77
  setRequestRouteData(req, actionName, rule) {
78
78
  req.notRouteData = {
79
79
  actionName,
80
+ modelName: this.routeName,
80
81
  rule: copyObj(rule),
81
82
  actionData: copyObj(this.actionData),
82
83
  };