not-node 5.1.33 → 5.1.36

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.33",
3
+ "version": "5.1.36",
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,48 @@ class Form {
30
31
  * @prop {string} name of form
31
32
  **/
32
33
  #FORM_NAME;
34
+ #MODEL_NAME;
35
+ #MODULE_NAME;
33
36
  #PROTO_FIELDS;
34
37
  #VALIDATOR;
35
38
  #EXTRACTORS = {
36
39
  ...DEFAULT_EXTRACTORS,
37
40
  };
38
41
 
39
- constructor({ FIELDS, FORM_NAME, app, EXTRACTORS = {} }) {
42
+ #ENV_EXTRACTORS = {
43
+ ...DEFAULT_ID_EXTRACTORS,
44
+ };
45
+
46
+ constructor({
47
+ FIELDS,
48
+ FORM_NAME,
49
+ MODEL_NAME,
50
+ MODULE_NAME,
51
+ app,
52
+ EXTRACTORS = {},
53
+ ENV_EXTRACTORS = {},
54
+ }) {
40
55
  this.#FORM_NAME = FORM_NAME;
56
+ this.#MODEL_NAME = MODEL_NAME;
57
+ this.#MODULE_NAME = MODULE_NAME;
41
58
  this.#PROTO_FIELDS = FIELDS;
42
59
  this.#createValidationSchema(app);
43
60
  this.#augmentValidationSchema();
44
61
  this.#addExtractors(EXTRACTORS);
62
+ this.#addEnvExtractors(ENV_EXTRACTORS);
63
+ }
64
+
65
+ getModelName(req) {
66
+ if (this.#MODEL_NAME) {
67
+ return this.#MODEL_NAME;
68
+ } else if (req) {
69
+ return firstLetterToUpper(req.notRouteData.modelName);
70
+ }
71
+ return undefined;
72
+ }
73
+
74
+ getModuleName() {
75
+ return this.#MODULE_NAME;
45
76
  }
46
77
 
47
78
  /**
@@ -62,8 +93,32 @@ class Form {
62
93
  * @param {ExpressRequest} req expressjs request object
63
94
  * @return {Object} forma data
64
95
  **/
65
- async extract(/*req*/) {
66
- return {};
96
+ async extract(req) {
97
+ return {
98
+ ...this.extractRequestEnvs(req),
99
+ data: this.extractByInstructionsFromRouteActionFields(req),
100
+ };
101
+ }
102
+
103
+ #addEnvExtractors(extractors = {}) {
104
+ if (extractors) {
105
+ this.#ENV_EXTRACTORS = { ...this.#ENV_EXTRACTORS, ...extractors };
106
+ }
107
+ }
108
+
109
+ extractRequestEnvs(req) {
110
+ const result = {};
111
+ Array.values(this.#ENV_EXTRACTORS).forEach((extractor) => {
112
+ const extracted = extractor(this, req);
113
+ if (
114
+ extracted &&
115
+ typeof extracted.value !== "undefined" &&
116
+ extracted.name
117
+ ) {
118
+ result[extracted.name] = extracted.value;
119
+ }
120
+ });
121
+ return result;
67
122
  }
68
123
 
69
124
  /**
@@ -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
  };