badmfck-api-server 3.0.8 → 3.0.9

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.
@@ -96,7 +96,7 @@ async function Initializer(services) {
96
96
  exports.Initializer = Initializer;
97
97
  class APIService extends BaseService_1.BaseService {
98
98
  static nextLogID = 0;
99
- version = "3.0.7";
99
+ version = "3.0.9";
100
100
  options;
101
101
  monitor = null;
102
102
  monitorIndexFile;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DocumentGenerator = exports.REQ_DOC = void 0;
4
4
  const badmfck_signal_1 = require("badmfck-signal");
5
+ const Validator_1 = require("./helper/Validator");
5
6
  exports.REQ_DOC = new badmfck_signal_1.Req(undefined, "REQ_DOC");
6
7
  class DocumentGenerator {
7
8
  endpoints = [];
@@ -20,19 +21,18 @@ class DocumentGenerator {
20
21
  description: i.description,
21
22
  endpoints: []
22
23
  };
23
- this.documentation.push(branch);
24
24
  for (let j of i.endpoints || []) {
25
25
  if (j.ignoreInDocumentation || j.ignoreHttpLogging)
26
26
  continue;
27
27
  const endpoint = {};
28
- endpoint.name = i.endpoint + "/" + j.endpoint;
28
+ endpoint.name = j.endpoint;
29
29
  endpoint.methods = {};
30
30
  if (typeof j.handler === "function") {
31
31
  const method = j.validationModel ? "POST" : "GET";
32
32
  endpoint.methods[method] = {
33
33
  title: j.title || "",
34
34
  description: j.description,
35
- parameters: j.validationModel ?? {},
35
+ parameters: Validator_1.Validator.documentStructure(j.validationModel),
36
36
  authorized: !j.ignoreInterceptor
37
37
  };
38
38
  }
@@ -44,13 +44,14 @@ class DocumentGenerator {
44
44
  endpoint.methods[method] = {
45
45
  title: handler.title || j.title || "",
46
46
  description: handler.description || j.description,
47
- parameters: handler.validationModel || {},
47
+ parameters: Validator_1.Validator.documentStructure(handler.validationModel),
48
48
  authorized: !handler.ignoreInterceptor
49
49
  };
50
50
  }
51
51
  }
52
52
  branch.endpoints.push(endpoint);
53
53
  }
54
+ this.documentation.push(branch);
54
55
  }
55
56
  };
56
57
  }
@@ -21,6 +21,7 @@ export declare enum ValidationReport {
21
21
  }
22
22
  export declare class Validator {
23
23
  static validateObject(fields: string[], object: any): boolean;
24
+ static documentStructure(structure: any): Record<string, any> | undefined;
24
25
  static validateStructure(structure: any, object: any, level?: number): Promise<string[] | undefined>;
25
26
  static filterStructure(structure: any, object: any): void;
26
27
  static convertToType<T>(value: any, type: "string" | "number" | "boolean" | "date"): T | null;
@@ -24,6 +24,32 @@ class Validator {
24
24
  }
25
25
  return true;
26
26
  }
27
+ static documentStructure(structure) {
28
+ const params = {};
29
+ if (!structure || typeof structure !== "object")
30
+ return;
31
+ for (let i in structure) {
32
+ if (i.startsWith("$__"))
33
+ continue;
34
+ let type = typeof structure[i];
35
+ let description = structure[i]['$__' + i] || "";
36
+ let childs = undefined;
37
+ let optional = structure[i]['$__optional_' + i] || "";
38
+ if (optional === undefined)
39
+ optional = structure[i] === null || structure[i] === undefined;
40
+ if (typeof structure[i] === "object")
41
+ childs = this.documentStructure(structure[i]);
42
+ if (structure[i] === "@")
43
+ type = "email";
44
+ params[i] = {
45
+ type: type,
46
+ description: description,
47
+ optional: optional,
48
+ childs: childs
49
+ };
50
+ }
51
+ return params;
52
+ }
27
53
  static async validateStructure(structure, object, level = 0) {
28
54
  if (!structure)
29
55
  return;
@@ -47,12 +73,23 @@ class Validator {
47
73
  let errors = [];
48
74
  let foundKeys = [];
49
75
  for (let i in structure) {
76
+ if (i.startsWith("$__")) {
77
+ foundKeys.push(i);
78
+ continue;
79
+ }
50
80
  if (structure[i] === null) {
51
81
  foundKeys.push(i);
52
82
  continue;
53
83
  }
54
- if (!(i in object))
55
- errors.push("no field '" + i + "'");
84
+ let optional = structure[i]['$__optional_' + i] || false;
85
+ if (!(i in object)) {
86
+ if (!optional)
87
+ errors.push("no field '" + i + "'");
88
+ else {
89
+ foundKeys.push(i);
90
+ continue;
91
+ }
92
+ }
56
93
  if (typeof structure[i] === "number" && typeof object[i] === "string") {
57
94
  let num = parseInt(object[i]);
58
95
  if (object[i].includes(","))
@@ -7,6 +7,7 @@ class Liveness extends BaseEndpoint_1.BaseEndpoint {
7
7
  started;
8
8
  constructor(stated) {
9
9
  super("liveness");
10
+ this.ignoreInDocumentation = true;
10
11
  this.started = stated;
11
12
  this.registerEndpoints([{ endpoint: "", handler: this.checkLiveness, ignoreInterceptor: true }]);
12
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "3.0.8",
3
+ "version": "3.0.9",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",