not-node 5.1.34 → 5.1.37

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.34",
3
+ "version": "5.1.37",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,8 +1,14 @@
1
1
  const getApp = require("../getApp.js"),
2
+ Form = require("../form").Form,
2
3
  HttpExceptions = require("../exceptions/http"),
3
4
  configInit = require("not-config"),
4
5
  { sayForModule } = require("not-locale"),
5
- { objHas, isFunc, executeFunctionAsAsync } = require("../common"),
6
+ {
7
+ objHas,
8
+ isFunc,
9
+ executeFunctionAsAsync,
10
+ firstLetterToUpper,
11
+ } = require("../common"),
6
12
  LogInit = require("not-log");
7
13
 
8
14
  module.exports = ({
@@ -79,6 +85,23 @@ module.exports = ({
79
85
  }
80
86
  };
81
87
 
88
+ const createDefaultForm = function ({ actionName }) {
89
+ const FIELDS = [
90
+ ["activeUser", "not-node//requiredObject"],
91
+ ["data", `${MODULE_NAME}//_data`],
92
+ ["ip", "not-node//ip"],
93
+ ];
94
+ const FORM_NAME = `${MODULE_NAME}:${firstLetterToUpper(
95
+ actionName
96
+ )}Form`;
97
+ const cls = class extends Form {
98
+ constructor({ app }) {
99
+ super({ FIELDS, FORM_NAME, app, MODULE_NAME });
100
+ }
101
+ };
102
+ return new cls({ app: getApp() });
103
+ };
104
+
82
105
  const getForm = (actionName) => {
83
106
  const form = getApp().getForm(
84
107
  [MODULE_NAME, `${MODEL_NAME}.${actionName}`].join("//")
@@ -86,7 +109,11 @@ module.exports = ({
86
109
  if (form) {
87
110
  return form;
88
111
  }
89
- return getApp().getForm([MODULE_NAME, actionName].join("//"));
112
+ const form2 = getApp().getForm([MODULE_NAME, actionName].join("//"));
113
+ if (form2) {
114
+ return form2;
115
+ }
116
+ return createDefaultForm({ actionName, MODULE_NAME });
90
117
  };
91
118
 
92
119
  const beforeDecorator = async (req, res, next) => {
File without changes
@@ -0,0 +1,6 @@
1
+ module.exports = (form, req) => {
2
+ return {
3
+ name: "activeUser",
4
+ value: req.user,
5
+ };
6
+ };
@@ -1,5 +1,8 @@
1
1
  module.exports = {
2
2
  _id: require("./_id.js"),
3
3
  ID: require("./ID.js"),
4
+ activeUser: require("./activeUser.js"),
5
+ ip: require("./ip.js"),
6
+ query: require("./query.js"),
4
7
  modelNameID: require("./modelNameID.js"),
5
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
+ };
@@ -1,11 +1,11 @@
1
1
  const { firstLetterToLower } = require("../../common");
2
2
 
3
3
  module.exports = (form, req) => {
4
- const modelName = form.getModelName();
4
+ const modelName = form.getModelName(req);
5
5
  if (typeof modelName === "string") {
6
6
  const idFieldName = `${firstLetterToLower(modelName)}ID`;
7
7
  return {
8
- name: "targetID",
8
+ name: idFieldName,
9
9
  value: req.params[idFieldName],
10
10
  };
11
11
  }
@@ -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
@@ -1,8 +1,8 @@
1
1
  const FormFabric = require("./fabric");
2
- const getIP = require("../auth").getIP;
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,7 +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
+ const DEFAULT_ID_EXTRACTORS = require("./env_extractors");
18
18
 
19
19
  /**
20
20
  * Generic form validation class
@@ -32,13 +32,14 @@ class Form {
32
32
  **/
33
33
  #FORM_NAME;
34
34
  #MODEL_NAME;
35
+ #MODULE_NAME;
35
36
  #PROTO_FIELDS;
36
37
  #VALIDATOR;
37
38
  #EXTRACTORS = {
38
39
  ...DEFAULT_EXTRACTORS,
39
40
  };
40
41
 
41
- #ID_EXTRACTORS = {
42
+ #ENV_EXTRACTORS = {
42
43
  ...DEFAULT_ID_EXTRACTORS,
43
44
  };
44
45
 
@@ -46,21 +47,32 @@ class Form {
46
47
  FIELDS,
47
48
  FORM_NAME,
48
49
  MODEL_NAME,
50
+ MODULE_NAME,
49
51
  app,
50
52
  EXTRACTORS = {},
51
- ID_EXTRACTORS = {},
53
+ ENV_EXTRACTORS = {},
52
54
  }) {
53
55
  this.#FORM_NAME = FORM_NAME;
54
56
  this.#MODEL_NAME = MODEL_NAME;
57
+ this.#MODULE_NAME = MODULE_NAME;
55
58
  this.#PROTO_FIELDS = FIELDS;
56
59
  this.#createValidationSchema(app);
57
60
  this.#augmentValidationSchema();
58
61
  this.#addExtractors(EXTRACTORS);
59
- this.#addIdExtractors(ID_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;
60
72
  }
61
73
 
62
- getModelName() {
63
- return this.#MODEL_NAME;
74
+ getModuleName() {
75
+ return this.#MODULE_NAME;
64
76
  }
65
77
 
66
78
  /**
@@ -83,22 +95,20 @@ class Form {
83
95
  **/
84
96
  async extract(req) {
85
97
  return {
86
- ...this.extractRequestIds(req),
87
- activeUser: req.user,
88
- ip: getIP(req),
98
+ ...this.extractRequestEnvs(req),
89
99
  data: this.extractByInstructionsFromRouteActionFields(req),
90
100
  };
91
101
  }
92
102
 
93
- #addIdExtractors(extractors = {}) {
103
+ #addEnvExtractors(extractors = {}) {
94
104
  if (extractors) {
95
- this.#ID_EXTRACTORS = { ...this.#ID_EXTRACTORS, ...extractors };
105
+ this.#ENV_EXTRACTORS = { ...this.#ENV_EXTRACTORS, ...extractors };
96
106
  }
97
107
  }
98
108
 
99
- extractRequestIds(req) {
109
+ extractRequestEnvs(req) {
100
110
  const result = {};
101
- Array.values(this.#ID_EXTRACTORS).forEach((extractor) => {
111
+ Array.values(this.#ENV_EXTRACTORS).forEach((extractor) => {
102
112
  const extracted = extractor(this, req);
103
113
  if (
104
114
  extracted &&
@@ -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
  };