@avleon/core 0.0.44 → 0.0.46
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/License +21 -21
- package/README.md +667 -681
- package/dist/application.test.js +15 -0
- package/dist/controller.test.js +0 -14
- package/dist/core/application.d.ts +74 -0
- package/dist/core/application.js +424 -0
- package/dist/core/router.d.ts +44 -0
- package/dist/core/router.js +520 -0
- package/dist/core/testing.d.ts +21 -0
- package/dist/core/testing.js +104 -0
- package/dist/core/types.d.ts +67 -0
- package/dist/core/types.js +2 -0
- package/dist/event-dispatcher.d.ts +0 -1
- package/dist/event-dispatcher.js +4 -7
- package/dist/file-storage.test.js +15 -2
- package/dist/helpers.d.ts +9 -42
- package/dist/helpers.js +19 -411
- package/dist/index.d.ts +17 -15
- package/dist/index.js +18 -22
- package/dist/interfaces/avleon-application.d.ts +74 -26
- package/dist/interfaces/avleon-application.js +1 -0
- package/dist/middleware.d.ts +11 -4
- package/dist/middleware.js +9 -0
- package/dist/multipart.d.ts +2 -2
- package/dist/openapi.d.ts +70 -3
- package/dist/openapi.js +32 -0
- package/dist/params.js +1 -6
- package/dist/params.test.js +8 -8
- package/dist/queue.d.ts +27 -36
- package/dist/queue.js +67 -99
- package/dist/route-methods.js +16 -5
- package/dist/swagger-schema.d.ts +11 -17
- package/dist/swagger-schema.js +84 -82
- package/dist/swagger-schema.test.js +32 -12
- package/dist/utils/common-utils.d.ts +17 -0
- package/dist/utils/common-utils.js +108 -0
- package/dist/utils/di-utils.d.ts +1 -0
- package/dist/utils/di-utils.js +22 -0
- package/dist/utils/hash.d.ts +0 -2
- package/dist/utils/hash.js +1 -5
- package/dist/utils/object-utils.d.ts +11 -0
- package/dist/utils/object-utils.js +198 -0
- package/dist/utils/validation-utils.d.ts +13 -0
- package/dist/utils/validation-utils.js +119 -0
- package/dist/validation.js +1 -4
- package/dist/websocket.d.ts +3 -0
- package/dist/websocket.js +2 -1
- package/package.json +53 -39
- package/dist/application.d.ts +0 -47
- package/dist/application.js +0 -50
- package/dist/icore.d.ts +0 -226
- package/dist/icore.js +0 -968
- package/dist/icore.test.js +0 -14
- package/dist/queue.test.d.ts +0 -1
- package/dist/queue.test.js +0 -79
- package/dist/testing.d.ts +0 -55
- package/dist/testing.js +0 -196
- /package/dist/{icore.test.d.ts → application.test.d.ts} +0 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const uuid: `${string}-${string}-${string}-${string}-${string}`;
|
|
2
|
+
export type Constructor<T = any> = new (...args: any[]) => T;
|
|
3
|
+
export declare function isConstructor(func: any): boolean;
|
|
4
|
+
export declare function formatUrl(path: string): string;
|
|
5
|
+
export declare function parsedPath(ipath: string): string;
|
|
6
|
+
export interface MatchLocation {
|
|
7
|
+
line: number;
|
|
8
|
+
column: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const getLineNumber: (filePath: string, rpath: string | RegExp) => MatchLocation[] | null;
|
|
11
|
+
export declare function normalizePath(base?: string, subPath?: string): string;
|
|
12
|
+
export declare function extrctParamFromUrl(url: string): {
|
|
13
|
+
key: string;
|
|
14
|
+
required: boolean;
|
|
15
|
+
}[];
|
|
16
|
+
export declare function findDuplicates(arr: string[]): string[];
|
|
17
|
+
export declare function sleep(ms: number): Promise<unknown>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getLineNumber = exports.uuid = void 0;
|
|
7
|
+
exports.isConstructor = isConstructor;
|
|
8
|
+
exports.formatUrl = formatUrl;
|
|
9
|
+
exports.parsedPath = parsedPath;
|
|
10
|
+
exports.normalizePath = normalizePath;
|
|
11
|
+
exports.extrctParamFromUrl = extrctParamFromUrl;
|
|
12
|
+
exports.findDuplicates = findDuplicates;
|
|
13
|
+
exports.sleep = sleep;
|
|
14
|
+
/**
|
|
15
|
+
* @copyright 2024
|
|
16
|
+
* @author Tareq Hossain
|
|
17
|
+
* @email xtrinsic96@gmail.com
|
|
18
|
+
* @url https://github.com/xtareq
|
|
19
|
+
*/
|
|
20
|
+
const fs_1 = __importDefault(require("fs"));
|
|
21
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
22
|
+
exports.uuid = crypto_1.default.randomUUID();
|
|
23
|
+
function isConstructor(func) {
|
|
24
|
+
if (typeof func !== "function") {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if (func === Function.prototype.bind || func instanceof RegExp) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (func.prototype && typeof func.prototype === "object") {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const instance = new func();
|
|
35
|
+
return typeof instance === "object";
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function formatUrl(path) {
|
|
42
|
+
if (typeof path !== "string") {
|
|
43
|
+
throw new Error("The path must be a string");
|
|
44
|
+
}
|
|
45
|
+
path = path.trim();
|
|
46
|
+
if (!path.startsWith("/")) {
|
|
47
|
+
path = "/" + path;
|
|
48
|
+
}
|
|
49
|
+
path = path.replace(/\/\/+/g, "/");
|
|
50
|
+
if (path.endsWith("/")) {
|
|
51
|
+
path = path.slice(0, -1);
|
|
52
|
+
}
|
|
53
|
+
return path;
|
|
54
|
+
}
|
|
55
|
+
function parsedPath(ipath) {
|
|
56
|
+
return !ipath.startsWith("/") ? "/" + ipath : ipath;
|
|
57
|
+
}
|
|
58
|
+
const getLineNumber = (filePath, rpath) => {
|
|
59
|
+
var _a;
|
|
60
|
+
let numbers = [];
|
|
61
|
+
try {
|
|
62
|
+
const fileContent = fs_1.default.readFileSync(filePath, "utf8");
|
|
63
|
+
const lines = fileContent.split("\n");
|
|
64
|
+
for (let i = 0; i < lines.length; i++) {
|
|
65
|
+
const match = lines[i].match(rpath);
|
|
66
|
+
if (match) {
|
|
67
|
+
console.log(match);
|
|
68
|
+
numbers.push({
|
|
69
|
+
line: i + 1,
|
|
70
|
+
column: (_a = match.index) !== null && _a !== void 0 ? _a : 0,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return numbers;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return numbers;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
exports.getLineNumber = getLineNumber;
|
|
81
|
+
function normalizePath(base = "/", subPath = "/") {
|
|
82
|
+
return `/${base}/${subPath}`.replace(/\/+/g, "/").replace(/\/$/, "");
|
|
83
|
+
}
|
|
84
|
+
function extrctParamFromUrl(url) {
|
|
85
|
+
const splitPart = url
|
|
86
|
+
.split("/")
|
|
87
|
+
.filter((x) => x.startsWith(":") || x.startsWith("?:"));
|
|
88
|
+
return splitPart.map((f) => ({
|
|
89
|
+
key: f.replace(/(\?|:)/g, ""),
|
|
90
|
+
required: !f.startsWith("?:"),
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
function findDuplicates(arr) {
|
|
94
|
+
const seen = new Set();
|
|
95
|
+
const duplicates = new Set();
|
|
96
|
+
for (const str of arr) {
|
|
97
|
+
if (seen.has(str)) {
|
|
98
|
+
duplicates.add(str);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
seen.add(str);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return Array.from(duplicates);
|
|
105
|
+
}
|
|
106
|
+
function sleep(ms) {
|
|
107
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
108
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function inject<T>(cls: new (...args: any[]) => T): T;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.inject = inject;
|
|
7
|
+
/**
|
|
8
|
+
* @copyright 2024
|
|
9
|
+
* @author Tareq Hossain
|
|
10
|
+
* @email xtrinsic96@gmail.com
|
|
11
|
+
* @url https://github.com/xtareq
|
|
12
|
+
*/
|
|
13
|
+
const container_1 = __importDefault(require("../container"));
|
|
14
|
+
const system_exception_1 = require("../exceptions/system-exception");
|
|
15
|
+
function inject(cls) {
|
|
16
|
+
try {
|
|
17
|
+
return container_1.default.get(cls);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
throw new system_exception_1.SystemUseError("Not a project class. Maybe you wanna register it first.");
|
|
21
|
+
}
|
|
22
|
+
}
|
package/dist/utils/hash.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
export declare const hashPasswordSync: (password: string) => string;
|
|
2
|
-
export declare const matchPasswordSync: (password: string, hash: string) => boolean;
|
|
3
1
|
export declare const hashPassword: (password: string) => Promise<string>;
|
|
4
2
|
export declare const matchPassword: (password: string, hash: string) => Promise<boolean>;
|
package/dist/utils/hash.js
CHANGED
|
@@ -3,12 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.matchPassword = exports.hashPassword =
|
|
6
|
+
exports.matchPassword = exports.hashPassword = void 0;
|
|
7
7
|
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
8
|
-
const hashPasswordSync = (password) => bcryptjs_1.default.hashSync(password, 12);
|
|
9
|
-
exports.hashPasswordSync = hashPasswordSync;
|
|
10
|
-
const matchPasswordSync = (password, hash) => bcryptjs_1.default.compareSync(password, hash);
|
|
11
|
-
exports.matchPasswordSync = matchPasswordSync;
|
|
12
8
|
const hashPassword = (password) => bcryptjs_1.default.hash(password, 12);
|
|
13
9
|
exports.hashPassword = hashPassword;
|
|
14
10
|
const matchPassword = (password, hash) => bcryptjs_1.default.compare(password, hash);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Constructor } from "./common-utils";
|
|
2
|
+
export declare function pick<T extends object>(obj: T, paths: string[]): Partial<T>;
|
|
3
|
+
export declare function exclude<T extends object>(obj: T | T[], paths: string[]): Partial<T> | Partial<T>[];
|
|
4
|
+
export declare function autoCast(value: any, typeHint?: any, schema?: any): any;
|
|
5
|
+
/**
|
|
6
|
+
* Deeply normalizes query strings into nested JS objects.
|
|
7
|
+
*/
|
|
8
|
+
export declare function normalizeQueryDeep(query: Record<string, any>): Record<string, any>;
|
|
9
|
+
export declare function transformObjectByInstanceToObject(instance: Constructor, value: object): Record<string, any>;
|
|
10
|
+
export declare function jsonToJs(value: string): any;
|
|
11
|
+
export declare function jsonToInstance(value: string, instance: Constructor): any;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pick = pick;
|
|
4
|
+
exports.exclude = exclude;
|
|
5
|
+
exports.autoCast = autoCast;
|
|
6
|
+
exports.normalizeQueryDeep = normalizeQueryDeep;
|
|
7
|
+
exports.transformObjectByInstanceToObject = transformObjectByInstanceToObject;
|
|
8
|
+
exports.jsonToJs = jsonToJs;
|
|
9
|
+
exports.jsonToInstance = jsonToInstance;
|
|
10
|
+
/**
|
|
11
|
+
* @copyright 2024
|
|
12
|
+
* @author Tareq Hossain
|
|
13
|
+
* @email xtrinsic96@gmail.com
|
|
14
|
+
* @url https://github.com/xtareq
|
|
15
|
+
*/
|
|
16
|
+
const class_transformer_1 = require("class-transformer");
|
|
17
|
+
function pick(obj, paths) {
|
|
18
|
+
const result = {};
|
|
19
|
+
for (const path of paths) {
|
|
20
|
+
const keys = path.split(".");
|
|
21
|
+
let source = obj;
|
|
22
|
+
let target = result;
|
|
23
|
+
for (let i = 0; i < keys.length; i++) {
|
|
24
|
+
const key = keys[i];
|
|
25
|
+
if (!(key in source))
|
|
26
|
+
break;
|
|
27
|
+
if (i === keys.length - 1) {
|
|
28
|
+
target[key] = source[key];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
source = source[key];
|
|
32
|
+
target[key] = target[key] || {};
|
|
33
|
+
target = target[key];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function exclude(obj, paths) {
|
|
40
|
+
if (Array.isArray(obj)) {
|
|
41
|
+
return obj.map((item) => exclude(item, paths));
|
|
42
|
+
}
|
|
43
|
+
const clone = structuredClone(obj); // Or use lodash.cloneDeep
|
|
44
|
+
for (const path of paths) {
|
|
45
|
+
const keys = path.split(".");
|
|
46
|
+
let target = clone;
|
|
47
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
48
|
+
if (!(keys[i] in target))
|
|
49
|
+
break;
|
|
50
|
+
target = target[keys[i]];
|
|
51
|
+
}
|
|
52
|
+
target === null || target === void 0 ? true : delete target[keys[keys.length - 1]];
|
|
53
|
+
}
|
|
54
|
+
return clone;
|
|
55
|
+
}
|
|
56
|
+
function autoCast(value, typeHint, schema) {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
if (value === null || value === undefined)
|
|
59
|
+
return value;
|
|
60
|
+
if (Array.isArray(value)) {
|
|
61
|
+
const elementType = Array.isArray(typeHint) ? typeHint[0] : undefined;
|
|
62
|
+
return value.map((v) => autoCast(v, elementType));
|
|
63
|
+
}
|
|
64
|
+
if (typeof value === "object" && !(value instanceof Date)) {
|
|
65
|
+
const result = {};
|
|
66
|
+
for (const [key, val] of Object.entries(value)) {
|
|
67
|
+
let fieldType = undefined;
|
|
68
|
+
if ((_b = (_a = schema === null || schema === void 0 ? void 0 : schema.properties) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.type) {
|
|
69
|
+
const t = schema.properties[key].type;
|
|
70
|
+
fieldType =
|
|
71
|
+
t === "integer" || t === "number"
|
|
72
|
+
? Number
|
|
73
|
+
: t === "boolean"
|
|
74
|
+
? Boolean
|
|
75
|
+
: t === "array"
|
|
76
|
+
? Array
|
|
77
|
+
: t === "object"
|
|
78
|
+
? Object
|
|
79
|
+
: String;
|
|
80
|
+
}
|
|
81
|
+
result[key] = autoCast(val, fieldType);
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
if (typeof value !== "string")
|
|
86
|
+
return value;
|
|
87
|
+
const trimmed = value.trim();
|
|
88
|
+
if (typeHint === Boolean || trimmed.toLowerCase() === "true")
|
|
89
|
+
return true;
|
|
90
|
+
if (trimmed.toLowerCase() === "false")
|
|
91
|
+
return false;
|
|
92
|
+
if (typeHint === Number || (!isNaN(Number(trimmed)) && trimmed !== "")) {
|
|
93
|
+
const n = Number(trimmed);
|
|
94
|
+
if (!isNaN(n))
|
|
95
|
+
return n;
|
|
96
|
+
}
|
|
97
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
98
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
99
|
+
try {
|
|
100
|
+
const parsed = JSON.parse(trimmed);
|
|
101
|
+
return autoCast(parsed, typeHint, schema);
|
|
102
|
+
}
|
|
103
|
+
catch (_c) {
|
|
104
|
+
return trimmed;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (typeHint === Date ||
|
|
108
|
+
/^\d{4}-\d{2}-\d{2}([Tt]\d{2}:\d{2})?/.test(trimmed)) {
|
|
109
|
+
const d = new Date(trimmed);
|
|
110
|
+
if (!isNaN(d.getTime()))
|
|
111
|
+
return d;
|
|
112
|
+
}
|
|
113
|
+
return trimmed;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Deeply normalizes query strings into nested JS objects.
|
|
117
|
+
*/
|
|
118
|
+
function normalizeQueryDeep(query) {
|
|
119
|
+
const result = {};
|
|
120
|
+
const setDeep = (obj, path, value) => {
|
|
121
|
+
let current = obj;
|
|
122
|
+
for (let i = 0; i < path.length; i++) {
|
|
123
|
+
const key = path[i];
|
|
124
|
+
const nextKey = path[i + 1];
|
|
125
|
+
if (i === path.length - 1) {
|
|
126
|
+
if (key === "") {
|
|
127
|
+
if (!Array.isArray(current))
|
|
128
|
+
current = [];
|
|
129
|
+
current.push(value);
|
|
130
|
+
}
|
|
131
|
+
else if (Array.isArray(current[key])) {
|
|
132
|
+
current[key].push(value);
|
|
133
|
+
}
|
|
134
|
+
else if (current[key] !== undefined) {
|
|
135
|
+
current[key] = [current[key], value];
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
current[key] = value;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
if (!current[key]) {
|
|
143
|
+
current[key] = nextKey === "" || /^\d+$/.test(nextKey) ? [] : {};
|
|
144
|
+
}
|
|
145
|
+
current = current[key];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
for (const [rawKey, rawValue] of Object.entries(query)) {
|
|
150
|
+
const path = [];
|
|
151
|
+
const regex = /([^\[\]]+)|(\[\])/g;
|
|
152
|
+
let match;
|
|
153
|
+
while ((match = regex.exec(rawKey)) !== null) {
|
|
154
|
+
if (match[1])
|
|
155
|
+
path.push(match[1]);
|
|
156
|
+
else if (match[2])
|
|
157
|
+
path.push("");
|
|
158
|
+
}
|
|
159
|
+
if (path.length === 0) {
|
|
160
|
+
if (result[rawKey]) {
|
|
161
|
+
if (Array.isArray(result[rawKey]))
|
|
162
|
+
result[rawKey].push(rawValue);
|
|
163
|
+
else
|
|
164
|
+
result[rawKey] = [result[rawKey], rawValue];
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
result[rawKey] = rawValue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
setDeep(result, path, rawValue);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
function transformObjectByInstanceToObject(instance, value) {
|
|
177
|
+
return (0, class_transformer_1.instanceToPlain)((0, class_transformer_1.plainToInstance)(instance, value), {
|
|
178
|
+
excludeExtraneousValues: true,
|
|
179
|
+
exposeUnsetFields: true,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
function jsonToJs(value) {
|
|
183
|
+
try {
|
|
184
|
+
return JSON.parse(value);
|
|
185
|
+
}
|
|
186
|
+
catch (err) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function jsonToInstance(value, instance) {
|
|
191
|
+
try {
|
|
192
|
+
const parsedValue = JSON.parse(value);
|
|
193
|
+
return (0, class_transformer_1.plainToInstance)(instance, parsedValue);
|
|
194
|
+
}
|
|
195
|
+
catch (err) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Constructor } from "./common-utils";
|
|
2
|
+
export declare const isClassValidator: (target: Constructor) => boolean;
|
|
3
|
+
export declare function getDataType(expectedType: any): any;
|
|
4
|
+
export declare function isValidType(value: any, expectedType: any): boolean;
|
|
5
|
+
export declare function isValidJsonString(value: string): object | boolean;
|
|
6
|
+
export declare const isClassValidatorClass: (target: Constructor) => boolean;
|
|
7
|
+
export declare function validateObjectByInstance(target: Constructor, value?: object, options?: "object" | "array"): Promise<any>;
|
|
8
|
+
type ValidationError = {
|
|
9
|
+
count: number;
|
|
10
|
+
errors: any;
|
|
11
|
+
};
|
|
12
|
+
export declare function validateRequestBody(target: Constructor, value: object, options?: "object" | "array"): ValidationError;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isClassValidatorClass = exports.isClassValidator = void 0;
|
|
4
|
+
exports.getDataType = getDataType;
|
|
5
|
+
exports.isValidType = isValidType;
|
|
6
|
+
exports.isValidJsonString = isValidJsonString;
|
|
7
|
+
exports.validateObjectByInstance = validateObjectByInstance;
|
|
8
|
+
exports.validateRequestBody = validateRequestBody;
|
|
9
|
+
/**
|
|
10
|
+
* @copyright 2024
|
|
11
|
+
* @author Tareq Hossain
|
|
12
|
+
* @email xtrinsic96@gmail.com
|
|
13
|
+
* @url https://github.com/xtareq
|
|
14
|
+
*/
|
|
15
|
+
const class_validator_1 = require("class-validator");
|
|
16
|
+
const exceptions_1 = require("../exceptions"); // Need to check path relative to utils
|
|
17
|
+
const class_transformer_1 = require("class-transformer");
|
|
18
|
+
const isClassValidator = (target) => {
|
|
19
|
+
try {
|
|
20
|
+
const clsval = require("class-validator");
|
|
21
|
+
const result = (0, class_validator_1.getMetadataStorage)().getTargetValidationMetadatas(target, "", false, false);
|
|
22
|
+
return result.length > 0;
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
console.log(err);
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.isClassValidator = isClassValidator;
|
|
30
|
+
function getDataType(expectedType) {
|
|
31
|
+
switch (expectedType.name) {
|
|
32
|
+
case "Object":
|
|
33
|
+
if (Array.isArray(expectedType)) {
|
|
34
|
+
return "array";
|
|
35
|
+
}
|
|
36
|
+
return "object";
|
|
37
|
+
case "String":
|
|
38
|
+
return "string";
|
|
39
|
+
case "Number":
|
|
40
|
+
return "number";
|
|
41
|
+
case "Boolean":
|
|
42
|
+
return "boolean";
|
|
43
|
+
default:
|
|
44
|
+
return expectedType;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function isValidType(value, expectedType) {
|
|
48
|
+
if (value === undefined || value === null)
|
|
49
|
+
return true;
|
|
50
|
+
switch (expectedType.name) {
|
|
51
|
+
case "String":
|
|
52
|
+
return typeof value === "string";
|
|
53
|
+
case "Number":
|
|
54
|
+
return typeof value === "number" || !isNaN(Number(value));
|
|
55
|
+
case "Boolean":
|
|
56
|
+
return typeof value === "boolean";
|
|
57
|
+
default:
|
|
58
|
+
return value instanceof expectedType;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function isValidJsonString(value) {
|
|
62
|
+
try {
|
|
63
|
+
return JSON.parse(value);
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const isClassValidatorClass = (target) => {
|
|
70
|
+
try {
|
|
71
|
+
const clsval = require("class-validator");
|
|
72
|
+
const result = clsval
|
|
73
|
+
.getMetadataStorage()
|
|
74
|
+
.getTargetValidationMetadatas(target, undefined, false, false);
|
|
75
|
+
return result.length > 0;
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
exports.isClassValidatorClass = isClassValidatorClass;
|
|
82
|
+
async function validateObjectByInstance(target, value = {}, options = "array") {
|
|
83
|
+
try {
|
|
84
|
+
const { validateOrReject } = require("class-validator");
|
|
85
|
+
const { plainToInstance } = require("class-transformer");
|
|
86
|
+
await validateOrReject(plainToInstance(target, value));
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (typeof error == "object" && Array.isArray(error)) {
|
|
90
|
+
const errors = options == "object"
|
|
91
|
+
? error.reduce((acc, x) => {
|
|
92
|
+
//acc[x.property] = Object.values(x.constraints);
|
|
93
|
+
acc[x.property] = x.constraints;
|
|
94
|
+
return acc;
|
|
95
|
+
}, {})
|
|
96
|
+
: error.map((x) => ({
|
|
97
|
+
path: x.property,
|
|
98
|
+
constraints: x.constraints,
|
|
99
|
+
}));
|
|
100
|
+
return errors;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
throw new exceptions_1.InternalErrorException("Can't validate object");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function validateRequestBody(target, value, options = "array") {
|
|
108
|
+
if (!(0, exports.isClassValidatorClass)(target))
|
|
109
|
+
return { count: 0, errors: {} };
|
|
110
|
+
const error = (0, class_validator_1.validateSync)((0, class_transformer_1.plainToInstance)(target, value ? value : {}));
|
|
111
|
+
const errors = options == "object"
|
|
112
|
+
? error.reduce((acc, x) => {
|
|
113
|
+
//acc[x.property] = Object.values(x.constraints);
|
|
114
|
+
acc[x.property] = x.constraints;
|
|
115
|
+
return acc;
|
|
116
|
+
}, {})
|
|
117
|
+
: error.map((x) => ({ path: x.property, constraints: x.constraints }));
|
|
118
|
+
return { count: error.length, errors };
|
|
119
|
+
}
|
package/dist/validation.js
CHANGED
|
@@ -90,16 +90,13 @@ const isBool = (val) => {
|
|
|
90
90
|
const parseBoolean = (val) => {
|
|
91
91
|
if (typeof val === "boolean")
|
|
92
92
|
return val;
|
|
93
|
-
// if (typeof val === "number") {
|
|
94
|
-
// return val !== 0; // Common convention: 0 → false, any other number → true
|
|
95
|
-
// }
|
|
96
93
|
if (parseInt(val) == 1)
|
|
97
94
|
return true;
|
|
98
95
|
if (typeof val === "string") {
|
|
99
96
|
const normalized = val.trim().toLowerCase();
|
|
100
97
|
return normalized === "true";
|
|
101
98
|
}
|
|
102
|
-
return false;
|
|
99
|
+
return false;
|
|
103
100
|
};
|
|
104
101
|
function validateOrThrow(obj, rules, options) {
|
|
105
102
|
const valid = new Validator(rules, options);
|
package/dist/websocket.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { Server } from "socket.io";
|
|
2
|
+
import { Token } from "typedi";
|
|
3
|
+
export declare const SocketIoServer: Token<Server<import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, any>>;
|
|
1
4
|
export declare class AvleonSocketIo {
|
|
2
5
|
private io?;
|
|
3
6
|
sendToAll(): void;
|
package/dist/websocket.js
CHANGED
|
@@ -6,8 +6,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.AvleonSocketIo = void 0;
|
|
9
|
+
exports.AvleonSocketIo = exports.SocketIoServer = void 0;
|
|
10
10
|
const typedi_1 = require("typedi");
|
|
11
|
+
exports.SocketIoServer = new typedi_1.Token("SocketIoServer");
|
|
11
12
|
let AvleonSocketIo = class AvleonSocketIo {
|
|
12
13
|
sendToAll() { }
|
|
13
14
|
sendOnly() { }
|