badmfck-api-server 3.0.8 → 3.1.0
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.
@@ -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 =
|
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,34 @@ 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] || "";
|
36
|
+
let childs = undefined;
|
37
|
+
let optional = structure['$__optional_' + i] || "";
|
38
|
+
if (optional === undefined)
|
39
|
+
optional = structure[i] === null || structure[i] === undefined;
|
40
|
+
if (type === "object" && Array.isArray(structure[i]))
|
41
|
+
type = "array";
|
42
|
+
if (typeof structure[i] === "object")
|
43
|
+
childs = this.documentStructure(structure[i]);
|
44
|
+
if (structure[i] === "@")
|
45
|
+
type = "email";
|
46
|
+
params[i] = {
|
47
|
+
type: type,
|
48
|
+
description: description,
|
49
|
+
optional: optional,
|
50
|
+
childs: childs
|
51
|
+
};
|
52
|
+
}
|
53
|
+
return params;
|
54
|
+
}
|
27
55
|
static async validateStructure(structure, object, level = 0) {
|
28
56
|
if (!structure)
|
29
57
|
return;
|
@@ -47,12 +75,23 @@ class Validator {
|
|
47
75
|
let errors = [];
|
48
76
|
let foundKeys = [];
|
49
77
|
for (let i in structure) {
|
78
|
+
if (i.startsWith("$__")) {
|
79
|
+
foundKeys.push(i);
|
80
|
+
continue;
|
81
|
+
}
|
50
82
|
if (structure[i] === null) {
|
51
83
|
foundKeys.push(i);
|
52
84
|
continue;
|
53
85
|
}
|
54
|
-
|
55
|
-
|
86
|
+
let optional = structure['$__optional_' + i] || false;
|
87
|
+
if (!(i in object)) {
|
88
|
+
if (!optional)
|
89
|
+
errors.push("no field '" + i + "'");
|
90
|
+
else {
|
91
|
+
foundKeys.push(i);
|
92
|
+
continue;
|
93
|
+
}
|
94
|
+
}
|
56
95
|
if (typeof structure[i] === "number" && typeof object[i] === "string") {
|
57
96
|
let num = parseInt(object[i]);
|
58
97
|
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
|
}
|