@avleon/core 0.0.46 → 0.0.48

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.
Files changed (142) hide show
  1. package/License +21 -21
  2. package/README.md +666 -666
  3. package/dist/chunk-9hOWP6kD.cjs +64 -0
  4. package/dist/chunk-DORXReHP.js +37 -0
  5. package/dist/index-BxIMWhgy.d.ts +1284 -0
  6. package/dist/index-DPn7qtzq.d.cts +1283 -0
  7. package/dist/index.cjs +3194 -0
  8. package/dist/index.cjs.map +1 -0
  9. package/dist/index.js +3022 -79
  10. package/dist/index.js.map +1 -0
  11. package/dist/lib-Bk8hUm06.cjs +7847 -0
  12. package/dist/lib-Bk8hUm06.cjs.map +1 -0
  13. package/dist/lib-CvDxBMkR.js +7843 -0
  14. package/dist/lib-CvDxBMkR.js.map +1 -0
  15. package/package.json +41 -103
  16. package/dist/application.test.d.ts +0 -1
  17. package/dist/application.test.js +0 -15
  18. package/dist/authentication.d.ts +0 -13
  19. package/dist/authentication.js +0 -16
  20. package/dist/cache.d.ts +0 -12
  21. package/dist/cache.js +0 -78
  22. package/dist/cache.test.d.ts +0 -1
  23. package/dist/cache.test.js +0 -36
  24. package/dist/collection.d.ts +0 -43
  25. package/dist/collection.js +0 -231
  26. package/dist/collection.test.d.ts +0 -1
  27. package/dist/collection.test.js +0 -59
  28. package/dist/config.d.ts +0 -18
  29. package/dist/config.js +0 -58
  30. package/dist/config.test.d.ts +0 -1
  31. package/dist/config.test.js +0 -40
  32. package/dist/constants.d.ts +0 -1
  33. package/dist/constants.js +0 -4
  34. package/dist/container.d.ts +0 -30
  35. package/dist/container.js +0 -55
  36. package/dist/controller.d.ts +0 -50
  37. package/dist/controller.js +0 -71
  38. package/dist/controller.test.d.ts +0 -1
  39. package/dist/controller.test.js +0 -97
  40. package/dist/core/application.d.ts +0 -74
  41. package/dist/core/application.js +0 -424
  42. package/dist/core/router.d.ts +0 -44
  43. package/dist/core/router.js +0 -520
  44. package/dist/core/testing.d.ts +0 -21
  45. package/dist/core/testing.js +0 -104
  46. package/dist/core/types.d.ts +0 -67
  47. package/dist/core/types.js +0 -2
  48. package/dist/decorators.d.ts +0 -15
  49. package/dist/decorators.js +0 -41
  50. package/dist/environment-variables.d.ts +0 -49
  51. package/dist/environment-variables.js +0 -130
  52. package/dist/environment-variables.test.d.ts +0 -1
  53. package/dist/environment-variables.test.js +0 -70
  54. package/dist/event-dispatcher.d.ts +0 -22
  55. package/dist/event-dispatcher.js +0 -97
  56. package/dist/event-subscriber.d.ts +0 -14
  57. package/dist/event-subscriber.js +0 -87
  58. package/dist/exceptions/http-exceptions.d.ts +0 -50
  59. package/dist/exceptions/http-exceptions.js +0 -85
  60. package/dist/exceptions/index.d.ts +0 -1
  61. package/dist/exceptions/index.js +0 -17
  62. package/dist/exceptions/system-exception.d.ts +0 -22
  63. package/dist/exceptions/system-exception.js +0 -26
  64. package/dist/file-storage.d.ts +0 -69
  65. package/dist/file-storage.js +0 -323
  66. package/dist/file-storage.test.d.ts +0 -1
  67. package/dist/file-storage.test.js +0 -117
  68. package/dist/helpers.d.ts +0 -11
  69. package/dist/helpers.js +0 -27
  70. package/dist/helpers.test.d.ts +0 -1
  71. package/dist/helpers.test.js +0 -95
  72. package/dist/index.d.ts +0 -57
  73. package/dist/interfaces/avleon-application.d.ts +0 -75
  74. package/dist/interfaces/avleon-application.js +0 -2
  75. package/dist/kenx-provider.d.ts +0 -7
  76. package/dist/kenx-provider.js +0 -44
  77. package/dist/kenx-provider.test.d.ts +0 -1
  78. package/dist/kenx-provider.test.js +0 -36
  79. package/dist/logger.d.ts +0 -12
  80. package/dist/logger.js +0 -87
  81. package/dist/logger.test.d.ts +0 -1
  82. package/dist/logger.test.js +0 -42
  83. package/dist/map-types.d.ts +0 -17
  84. package/dist/map-types.js +0 -89
  85. package/dist/middleware.d.ts +0 -34
  86. package/dist/middleware.js +0 -73
  87. package/dist/middleware.test.d.ts +0 -1
  88. package/dist/middleware.test.js +0 -121
  89. package/dist/multipart.d.ts +0 -17
  90. package/dist/multipart.js +0 -70
  91. package/dist/multipart.test.d.ts +0 -1
  92. package/dist/multipart.test.js +0 -87
  93. package/dist/openapi.d.ts +0 -410
  94. package/dist/openapi.js +0 -59
  95. package/dist/openapi.test.d.ts +0 -1
  96. package/dist/openapi.test.js +0 -111
  97. package/dist/params.d.ts +0 -17
  98. package/dist/params.js +0 -64
  99. package/dist/params.test.d.ts +0 -1
  100. package/dist/params.test.js +0 -83
  101. package/dist/queue.d.ts +0 -29
  102. package/dist/queue.js +0 -84
  103. package/dist/response.d.ts +0 -16
  104. package/dist/response.js +0 -56
  105. package/dist/results.d.ts +0 -20
  106. package/dist/results.js +0 -32
  107. package/dist/route-methods.d.ts +0 -25
  108. package/dist/route-methods.js +0 -60
  109. package/dist/route-methods.test.d.ts +0 -1
  110. package/dist/route-methods.test.js +0 -129
  111. package/dist/swagger-schema.d.ts +0 -37
  112. package/dist/swagger-schema.js +0 -454
  113. package/dist/swagger-schema.test.d.ts +0 -1
  114. package/dist/swagger-schema.test.js +0 -125
  115. package/dist/types/app-builder.interface.d.ts +0 -15
  116. package/dist/types/app-builder.interface.js +0 -8
  117. package/dist/types/application.interface.d.ts +0 -8
  118. package/dist/types/application.interface.js +0 -2
  119. package/dist/utils/common-utils.d.ts +0 -17
  120. package/dist/utils/common-utils.js +0 -108
  121. package/dist/utils/di-utils.d.ts +0 -1
  122. package/dist/utils/di-utils.js +0 -22
  123. package/dist/utils/hash.d.ts +0 -2
  124. package/dist/utils/hash.js +0 -11
  125. package/dist/utils/index.d.ts +0 -2
  126. package/dist/utils/index.js +0 -18
  127. package/dist/utils/object-utils.d.ts +0 -11
  128. package/dist/utils/object-utils.js +0 -198
  129. package/dist/utils/optional-require.d.ts +0 -8
  130. package/dist/utils/optional-require.js +0 -70
  131. package/dist/utils/validation-utils.d.ts +0 -13
  132. package/dist/utils/validation-utils.js +0 -119
  133. package/dist/validation.d.ts +0 -39
  134. package/dist/validation.js +0 -108
  135. package/dist/validation.test.d.ts +0 -1
  136. package/dist/validation.test.js +0 -61
  137. package/dist/validator-extend.d.ts +0 -7
  138. package/dist/validator-extend.js +0 -28
  139. package/dist/websocket.d.ts +0 -10
  140. package/dist/websocket.js +0 -21
  141. package/dist/websocket.test.d.ts +0 -1
  142. package/dist/websocket.test.js +0 -27
@@ -1,108 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export declare function inject<T>(cls: new (...args: any[]) => T): T;
@@ -1,22 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- export declare const hashPassword: (password: string) => Promise<string>;
2
- export declare const matchPassword: (password: string, hash: string) => Promise<boolean>;
@@ -1,11 +0,0 @@
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.matchPassword = exports.hashPassword = void 0;
7
- const bcryptjs_1 = __importDefault(require("bcryptjs"));
8
- const hashPassword = (password) => bcryptjs_1.default.hash(password, 12);
9
- exports.hashPassword = hashPassword;
10
- const matchPassword = (password, hash) => bcryptjs_1.default.compare(password, hash);
11
- exports.matchPassword = matchPassword;
@@ -1,2 +0,0 @@
1
- export * from "./hash";
2
- export * from "./optional-require";
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./hash"), exports);
18
- __exportStar(require("./optional-require"), exports);
@@ -1,11 +0,0 @@
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;
@@ -1,198 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
1
- export declare function optionalRequire<T = any>(moduleName: string, options?: {
2
- failOnMissing?: boolean;
3
- customMessage?: string;
4
- }): T | undefined;
5
- export declare function optionalImport<T = any>(moduleName: string, options?: {
6
- failOnMissing?: boolean;
7
- customMessage?: string;
8
- }): Promise<T | undefined>;
@@ -1,70 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.optionalRequire = optionalRequire;
37
- exports.optionalImport = optionalImport;
38
- function optionalRequire(moduleName, options = {}) {
39
- try {
40
- return require(moduleName);
41
- }
42
- catch (err) {
43
- if (err.code === "MODULE_NOT_FOUND" && err.message.includes(moduleName)) {
44
- if (options.failOnMissing) {
45
- throw new Error(options.customMessage ||
46
- `Optional dependency "${moduleName}" is not installed.\nInstall it with:\n\n npm install ${moduleName}`);
47
- }
48
- return undefined;
49
- }
50
- throw err;
51
- }
52
- }
53
- async function optionalImport(moduleName, options = {}) {
54
- try {
55
- const mod = await Promise.resolve(`${moduleName}`).then(s => __importStar(require(s)));
56
- return mod;
57
- }
58
- catch (err) {
59
- if ((err.code === "ERR_MODULE_NOT_FOUND" ||
60
- err.code === "MODULE_NOT_FOUND") &&
61
- err.message.includes(moduleName)) {
62
- if (options.failOnMissing) {
63
- throw new Error(options.customMessage ||
64
- `Optional dependency "${moduleName}" is not installed.\nInstall it with:\n\n npm install ${moduleName}`);
65
- }
66
- return undefined;
67
- }
68
- throw err;
69
- }
70
- }
@@ -1,13 +0,0 @@
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 {};
@@ -1,119 +0,0 @@
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
- }
@@ -1,39 +0,0 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
- type BaseRule = {
8
- required?: boolean;
9
- optional?: boolean;
10
- message?: string;
11
- };
12
- type StringRule = BaseRule & {
13
- type: "string";
14
- };
15
- type NumberRule = BaseRule & {
16
- type: "number";
17
- min?: number;
18
- max?: number;
19
- exact?: number;
20
- };
21
- type BooleanRule = BaseRule & {
22
- type: "boolean";
23
- };
24
- export type ValidationRule = StringRule | NumberRule | BooleanRule;
25
- export type ValidationProps = {
26
- [key: string]: ValidationRule;
27
- };
28
- export type ValidateOptons = {
29
- location?: "header" | "queryparam" | "param" | "body" | "custom";
30
- };
31
- export declare class Validator {
32
- private rules;
33
- private options;
34
- constructor(obj: ValidationProps, options?: ValidateOptons);
35
- private init;
36
- validate(obj: any | Array<any>, options?: ValidateOptons): any[];
37
- }
38
- export declare function validateOrThrow<T extends {}>(obj: T, rules: ValidationProps, options?: ValidateOptons): any;
39
- export {};