@avleon/core 0.0.26 → 0.0.28

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 (41) hide show
  1. package/README.md +601 -561
  2. package/package.json +38 -6
  3. package/src/application.ts +104 -125
  4. package/src/authentication.ts +16 -16
  5. package/src/cache.ts +91 -91
  6. package/src/collection.test.ts +71 -0
  7. package/src/collection.ts +344 -254
  8. package/src/config.test.ts +35 -0
  9. package/src/config.ts +85 -42
  10. package/src/constants.ts +1 -1
  11. package/src/container.ts +54 -54
  12. package/src/controller.ts +125 -127
  13. package/src/decorators.ts +27 -27
  14. package/src/environment-variables.ts +53 -46
  15. package/src/exceptions/http-exceptions.ts +86 -86
  16. package/src/exceptions/index.ts +1 -1
  17. package/src/exceptions/system-exception.ts +35 -34
  18. package/src/file-storage.ts +206 -206
  19. package/src/helpers.ts +324 -328
  20. package/src/icore.ts +66 -90
  21. package/src/index.ts +30 -30
  22. package/src/interfaces/avleon-application.ts +32 -40
  23. package/src/logger.ts +72 -72
  24. package/src/map-types.ts +159 -159
  25. package/src/middleware.ts +119 -98
  26. package/src/multipart.ts +116 -116
  27. package/src/openapi.ts +372 -372
  28. package/src/params.ts +111 -111
  29. package/src/queue.ts +126 -126
  30. package/src/response.ts +74 -74
  31. package/src/results.ts +30 -30
  32. package/src/route-methods.ts +186 -186
  33. package/src/swagger-schema.ts +213 -213
  34. package/src/testing.ts +220 -220
  35. package/src/types/app-builder.interface.ts +18 -19
  36. package/src/types/application.interface.ts +7 -9
  37. package/src/utils/hash.ts +8 -5
  38. package/src/utils/index.ts +2 -2
  39. package/src/utils/optional-require.ts +50 -50
  40. package/src/validation.ts +160 -156
  41. package/src/validator-extend.ts +25 -25
package/src/helpers.ts CHANGED
@@ -1,328 +1,324 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
- import { instanceToPlain, plainToInstance } from "class-transformer";
8
- import { InternalErrorException } from "./exceptions";
9
- import fs from "fs";
10
- import container from "./container";
11
- import { SystemUseError } from "./exceptions/system-exception";
12
- import crypto, { UUID } from "crypto";
13
- import { getMetadataStorage, validate, validateSync } from "class-validator";
14
-
15
- export const uuid = crypto.randomUUID();
16
-
17
- export function inject<T>(cls: new (...args: any[]) => T): T {
18
- try {
19
- return container.get(cls);
20
- } catch (error) {
21
- throw new SystemUseError(
22
- `Not a project class. Maybe you wanna register it first.`
23
- );
24
- }
25
- }
26
-
27
- export type Constructor<T = any> = new (...args: any[]) => T;
28
-
29
- export function isConstructor(func: any): boolean {
30
-
31
- if (typeof func !== "function") {
32
- return false;
33
- }
34
-
35
- if (func === Function.prototype.bind || func instanceof RegExp) {
36
- return false;
37
- }
38
-
39
- if (func.prototype && typeof func.prototype === "object") {
40
- return true;
41
- }
42
-
43
- try {
44
- const instance = new (func as any)();
45
- return typeof instance === "object";
46
- } catch (e) {
47
- return false;
48
- }
49
- }
50
-
51
- export function formatUrl(path: string): string {
52
- if (typeof path !== "string") {
53
- throw new Error("The path must be a string");
54
- }
55
- path = path.trim();
56
-
57
- if (!path.startsWith("/")) {
58
- path = "/" + path;
59
- }
60
- path = path.replace(/\/\/+/g, "/");
61
- if (path.endsWith("/")) {
62
- path = path.slice(0, -1);
63
- }
64
-
65
- return path;
66
- }
67
-
68
- export function parsedPath(ipath: string): string {
69
- return !ipath.startsWith("/") ? "/" + ipath : ipath;
70
- }
71
- export const isClassValidator = (target: Constructor) => {
72
- try {
73
- const clsval = require("class-validator");
74
- const result = getMetadataStorage().getTargetValidationMetadatas(
75
- target,
76
- "",
77
- false,
78
- false
79
- );
80
- return result.length > 0;
81
- } catch (err: any) {
82
- console.log(err);
83
- return false;
84
- }
85
- };
86
-
87
- export interface MatchLocation {
88
- line: number;
89
- column: number;
90
- }
91
- export const getLineNumber = (
92
- filePath: string,
93
- rpath: string | RegExp
94
- ): MatchLocation[] | null => {
95
- let numbers = [];
96
- try {
97
- const fileContent = fs.readFileSync(filePath, "utf8");
98
- const lines = fileContent.split("\n");
99
- for (let i = 0; i < lines.length; i++) {
100
- const match = lines[i].match(rpath);
101
-
102
- if (match) {
103
- console.log(match);
104
- numbers.push({
105
- line: i + 1,
106
- column: match.index ?? 0,
107
- });
108
- }
109
- }
110
-
111
- return numbers;
112
- } catch (error) {
113
- return numbers;
114
- }
115
- };
116
-
117
- export function normalizePath(base: string = "/", subPath: string = "/") {
118
- return `/${base}/${subPath}`.replace(/\/+/g, "/").replace(/\/$/, "");
119
- }
120
-
121
- export function extrctParamFromUrl(url: string) {
122
- const splitPart = url
123
- .split("/")
124
- .filter((x) => x.startsWith(":") || x.startsWith("?:"));
125
- return splitPart.map((f) => ({
126
- key: f.replace(/(\?|:)/g, ""),
127
- required: !f.startsWith("?:"),
128
- }));
129
- }
130
-
131
- export function findDuplicates(arr: string[]): string[] {
132
- const seen = new Set();
133
- const duplicates = new Set();
134
-
135
- for (const str of arr) {
136
- if (seen.has(str)) {
137
- duplicates.add(str);
138
- } else {
139
- seen.add(str);
140
- }
141
- }
142
-
143
- return Array.from(duplicates) as string[];
144
- }
145
-
146
- export function getDataType(expectedType: any) {
147
- switch (expectedType.name) {
148
- case "Object":
149
- if (Array.isArray(expectedType)) {
150
- return "array";
151
- }
152
- return "object";
153
- case "String":
154
- return "string";
155
- case "Number":
156
- return "number";
157
- case "Boolean":
158
- return "boolean";
159
- default:
160
- return expectedType;
161
- }
162
- }
163
- export function isValidType(value: any, expectedType: any): boolean {
164
- if (value === undefined || value === null) return true;
165
-
166
- switch (expectedType.name) {
167
- case "String":
168
- return typeof value === "string";
169
- case "Number":
170
- return typeof value === "number" || !isNaN(Number(value));
171
- case "Boolean":
172
- return typeof value === "boolean";
173
- default:
174
- return value instanceof expectedType;
175
- }
176
- }
177
-
178
- export function isValidJsonString(value: string): object | boolean {
179
- try {
180
- return JSON.parse(value);
181
- } catch (err: any) {
182
- return false;
183
- }
184
- }
185
-
186
- export function jsonToJs(value: string) {
187
- try {
188
- return JSON.parse(value);
189
- } catch (err: any) {
190
- return false;
191
- }
192
- }
193
-
194
- export function jsonToInstance(value: string, instance: Constructor) {
195
- try {
196
- const parsedValue = JSON.parse(value);
197
- return plainToInstance(instance, parsedValue);
198
- } catch (err: any) {
199
- return false;
200
- }
201
- }
202
-
203
- export function transformObjectByInstanceToObject(
204
- instance: Constructor,
205
- value: object
206
- ) {
207
- return instanceToPlain(plainToInstance(instance, value), {
208
- excludeExtraneousValues: true,
209
- exposeUnsetFields: true,
210
- });
211
- }
212
-
213
- export const isClassValidatorClass = (target: Constructor) => {
214
- try {
215
- const clsval = require("class-validator");
216
- const result = clsval
217
- .getMetadataStorage()
218
- .getTargetValidationMetadatas(target, undefined, false, false);
219
- return result.length > 0;
220
- } catch (err: any) {
221
- return false;
222
- }
223
- };
224
-
225
- export async function validateObjectByInstance(
226
- target: Constructor,
227
- value: object = {},
228
- options: "object" | "array" = "array"
229
- ) {
230
- try {
231
- const { validateOrReject } = require("class-validator");
232
- const { plainToInstance } = require("class-transformer");
233
- await validateOrReject(plainToInstance(target, value));
234
- } catch (error: any) {
235
- if (typeof error == "object" && Array.isArray(error)) {
236
- const errors =
237
- options == "object"
238
- ? error.reduce((acc: any, x: any) => {
239
- //acc[x.property] = Object.values(x.constraints);
240
- acc[x.property] = x.constraints;
241
- return acc;
242
- }, {})
243
- : error.map((x) => ({
244
- path: x.property,
245
- constraints: x.constraints,
246
- }));
247
- return errors;
248
- } else {
249
- throw new InternalErrorException("Can't validate object");
250
- }
251
- }
252
- }
253
-
254
- type ValidationError = {
255
- count: number;
256
- errors: any;
257
- };
258
-
259
- export function validateRequestBody(
260
- target: Constructor,
261
- value: object,
262
- options: "object" | "array" = "array"
263
- ): ValidationError {
264
- if (!isClassValidatorClass(target)) return { count: 0, errors: {} };
265
- const error = validateSync(plainToInstance(target, value ? value : {}));
266
- const errors =
267
- options == "object"
268
- ? error.reduce((acc: any, x: any) => {
269
- //acc[x.property] = Object.values(x.constraints);
270
- acc[x.property] = x.constraints;
271
- return acc;
272
- }, {})
273
- : error.map((x) => ({ path: x.property, constraints: x.constraints }));
274
- return { count: error.length, errors } as ValidationError;
275
- }
276
-
277
- export function pick<T extends object>(obj: T, paths: string[]): Partial<T> {
278
- const result: any = {};
279
-
280
- for (const path of paths) {
281
- const keys = path.split(".");
282
- let source: any = obj;
283
- let target: any = result;
284
-
285
- for (let i = 0; i < keys.length; i++) {
286
- const key = keys[i];
287
-
288
- if (!(key in source)) break;
289
-
290
- if (i === keys.length - 1) {
291
- target[key] = source[key];
292
- } else {
293
- source = source[key];
294
- target[key] = target[key] || {};
295
- target = target[key];
296
- }
297
- }
298
- }
299
-
300
- return result;
301
- }
302
-
303
-
304
- export function exclude<T extends object>(
305
- obj: T | T[],
306
- paths: string[],
307
- ): Partial<T> | Partial<T>[] {
308
- if (Array.isArray(obj)) {
309
- return obj.map((item) => exclude(item, paths) as Partial<T>);
310
- }
311
-
312
- const clone = structuredClone(obj); // Or use lodash.cloneDeep
313
- for (const path of paths) {
314
- const keys = path.split(".");
315
- let target: any = clone;
316
-
317
- for (let i = 0; i < keys.length - 1; i++) {
318
- if (!(keys[i] in target)) break;
319
- target = target[keys[i]];
320
- }
321
-
322
- delete target?.[keys[keys.length - 1]];
323
- }
324
-
325
- return clone;
326
- }
327
-
328
-
1
+ /**
2
+ * @copyright 2024
3
+ * @author Tareq Hossain
4
+ * @email xtrinsic96@gmail.com
5
+ * @url https://github.com/xtareq
6
+ */
7
+ import { instanceToPlain, plainToInstance } from "class-transformer";
8
+ import { InternalErrorException } from "./exceptions";
9
+ import fs from "fs";
10
+ import container from "./container";
11
+ import { SystemUseError } from "./exceptions/system-exception";
12
+ import crypto, { UUID } from "crypto";
13
+ import { getMetadataStorage, validate, validateSync } from "class-validator";
14
+
15
+ export const uuid = crypto.randomUUID();
16
+
17
+ export function inject<T>(cls: new (...args: any[]) => T): T {
18
+ try {
19
+ return container.get(cls);
20
+ } catch (error) {
21
+ throw new SystemUseError(
22
+ "Not a project class. Maybe you wanna register it first.",
23
+ );
24
+ }
25
+ }
26
+
27
+ export type Constructor<T = any> = new (...args: any[]) => T;
28
+
29
+ export function isConstructor(func: any): boolean {
30
+ if (typeof func !== "function") {
31
+ return false;
32
+ }
33
+
34
+ if (func === Function.prototype.bind || func instanceof RegExp) {
35
+ return false;
36
+ }
37
+
38
+ if (func.prototype && typeof func.prototype === "object") {
39
+ return true;
40
+ }
41
+
42
+ try {
43
+ const instance = new (func as any)();
44
+ return typeof instance === "object";
45
+ } catch (e) {
46
+ return false;
47
+ }
48
+ }
49
+
50
+ export function formatUrl(path: string): string {
51
+ if (typeof path !== "string") {
52
+ throw new Error("The path must be a string");
53
+ }
54
+ path = path.trim();
55
+
56
+ if (!path.startsWith("/")) {
57
+ path = "/" + path;
58
+ }
59
+ path = path.replace(/\/\/+/g, "/");
60
+ if (path.endsWith("/")) {
61
+ path = path.slice(0, -1);
62
+ }
63
+
64
+ return path;
65
+ }
66
+
67
+ export function parsedPath(ipath: string): string {
68
+ return !ipath.startsWith("/") ? "/" + ipath : ipath;
69
+ }
70
+ export const isClassValidator = (target: Constructor) => {
71
+ try {
72
+ const clsval = require("class-validator");
73
+ const result = getMetadataStorage().getTargetValidationMetadatas(
74
+ target,
75
+ "",
76
+ false,
77
+ false,
78
+ );
79
+ return result.length > 0;
80
+ } catch (err: any) {
81
+ console.log(err);
82
+ return false;
83
+ }
84
+ };
85
+
86
+ export interface MatchLocation {
87
+ line: number;
88
+ column: number;
89
+ }
90
+ export const getLineNumber = (
91
+ filePath: string,
92
+ rpath: string | RegExp,
93
+ ): MatchLocation[] | null => {
94
+ let numbers = [];
95
+ try {
96
+ const fileContent = fs.readFileSync(filePath, "utf8");
97
+ const lines = fileContent.split("\n");
98
+ for (let i = 0; i < lines.length; i++) {
99
+ const match = lines[i].match(rpath);
100
+
101
+ if (match) {
102
+ console.log(match);
103
+ numbers.push({
104
+ line: i + 1,
105
+ column: match.index ?? 0,
106
+ });
107
+ }
108
+ }
109
+
110
+ return numbers;
111
+ } catch (error) {
112
+ return numbers;
113
+ }
114
+ };
115
+
116
+ export function normalizePath(base: string = "/", subPath: string = "/") {
117
+ return `/${base}/${subPath}`.replace(/\/+/g, "/").replace(/\/$/, "");
118
+ }
119
+
120
+ export function extrctParamFromUrl(url: string) {
121
+ const splitPart = url
122
+ .split("/")
123
+ .filter((x) => x.startsWith(":") || x.startsWith("?:"));
124
+ return splitPart.map((f) => ({
125
+ key: f.replace(/(\?|:)/g, ""),
126
+ required: !f.startsWith("?:"),
127
+ }));
128
+ }
129
+
130
+ export function findDuplicates(arr: string[]): string[] {
131
+ const seen = new Set();
132
+ const duplicates = new Set();
133
+
134
+ for (const str of arr) {
135
+ if (seen.has(str)) {
136
+ duplicates.add(str);
137
+ } else {
138
+ seen.add(str);
139
+ }
140
+ }
141
+
142
+ return Array.from(duplicates) as string[];
143
+ }
144
+
145
+ export function getDataType(expectedType: any) {
146
+ switch (expectedType.name) {
147
+ case "Object":
148
+ if (Array.isArray(expectedType)) {
149
+ return "array";
150
+ }
151
+ return "object";
152
+ case "String":
153
+ return "string";
154
+ case "Number":
155
+ return "number";
156
+ case "Boolean":
157
+ return "boolean";
158
+ default:
159
+ return expectedType;
160
+ }
161
+ }
162
+ export function isValidType(value: any, expectedType: any): boolean {
163
+ if (value === undefined || value === null) return true;
164
+
165
+ switch (expectedType.name) {
166
+ case "String":
167
+ return typeof value === "string";
168
+ case "Number":
169
+ return typeof value === "number" || !isNaN(Number(value));
170
+ case "Boolean":
171
+ return typeof value === "boolean";
172
+ default:
173
+ return value instanceof expectedType;
174
+ }
175
+ }
176
+
177
+ export function isValidJsonString(value: string): object | boolean {
178
+ try {
179
+ return JSON.parse(value);
180
+ } catch (err: any) {
181
+ return false;
182
+ }
183
+ }
184
+
185
+ export function jsonToJs(value: string) {
186
+ try {
187
+ return JSON.parse(value);
188
+ } catch (err: any) {
189
+ return false;
190
+ }
191
+ }
192
+
193
+ export function jsonToInstance(value: string, instance: Constructor) {
194
+ try {
195
+ const parsedValue = JSON.parse(value);
196
+ return plainToInstance(instance, parsedValue);
197
+ } catch (err: any) {
198
+ return false;
199
+ }
200
+ }
201
+
202
+ export function transformObjectByInstanceToObject(
203
+ instance: Constructor,
204
+ value: object,
205
+ ) {
206
+ return instanceToPlain(plainToInstance(instance, value), {
207
+ excludeExtraneousValues: true,
208
+ exposeUnsetFields: true,
209
+ });
210
+ }
211
+
212
+ export const isClassValidatorClass = (target: Constructor) => {
213
+ try {
214
+ const clsval = require("class-validator");
215
+ const result = clsval
216
+ .getMetadataStorage()
217
+ .getTargetValidationMetadatas(target, undefined, false, false);
218
+ return result.length > 0;
219
+ } catch (err: any) {
220
+ return false;
221
+ }
222
+ };
223
+
224
+ export async function validateObjectByInstance(
225
+ target: Constructor,
226
+ value: object = {},
227
+ options: "object" | "array" = "array",
228
+ ) {
229
+ try {
230
+ const { validateOrReject } = require("class-validator");
231
+ const { plainToInstance } = require("class-transformer");
232
+ await validateOrReject(plainToInstance(target, value));
233
+ } catch (error: any) {
234
+ if (typeof error == "object" && Array.isArray(error)) {
235
+ const errors =
236
+ options == "object"
237
+ ? error.reduce((acc: any, x: any) => {
238
+ //acc[x.property] = Object.values(x.constraints);
239
+ acc[x.property] = x.constraints;
240
+ return acc;
241
+ }, {})
242
+ : error.map((x) => ({
243
+ path: x.property,
244
+ constraints: x.constraints,
245
+ }));
246
+ return errors;
247
+ } else {
248
+ throw new InternalErrorException("Can't validate object");
249
+ }
250
+ }
251
+ }
252
+
253
+ type ValidationError = {
254
+ count: number;
255
+ errors: any;
256
+ };
257
+
258
+ export function validateRequestBody(
259
+ target: Constructor,
260
+ value: object,
261
+ options: "object" | "array" = "array",
262
+ ): ValidationError {
263
+ if (!isClassValidatorClass(target)) return { count: 0, errors: {} };
264
+ const error = validateSync(plainToInstance(target, value ? value : {}));
265
+ const errors =
266
+ options == "object"
267
+ ? error.reduce((acc: any, x: any) => {
268
+ //acc[x.property] = Object.values(x.constraints);
269
+ acc[x.property] = x.constraints;
270
+ return acc;
271
+ }, {})
272
+ : error.map((x) => ({ path: x.property, constraints: x.constraints }));
273
+ return { count: error.length, errors } as ValidationError;
274
+ }
275
+
276
+ export function pick<T extends object>(obj: T, paths: string[]): Partial<T> {
277
+ const result: any = {};
278
+
279
+ for (const path of paths) {
280
+ const keys = path.split(".");
281
+ let source: any = obj;
282
+ let target: any = result;
283
+
284
+ for (let i = 0; i < keys.length; i++) {
285
+ const key = keys[i];
286
+
287
+ if (!(key in source)) break;
288
+
289
+ if (i === keys.length - 1) {
290
+ target[key] = source[key];
291
+ } else {
292
+ source = source[key];
293
+ target[key] = target[key] || {};
294
+ target = target[key];
295
+ }
296
+ }
297
+ }
298
+
299
+ return result;
300
+ }
301
+
302
+ export function exclude<T extends object>(
303
+ obj: T | T[],
304
+ paths: string[],
305
+ ): Partial<T> | Partial<T>[] {
306
+ if (Array.isArray(obj)) {
307
+ return obj.map((item) => exclude(item, paths) as Partial<T>);
308
+ }
309
+
310
+ const clone = structuredClone(obj); // Or use lodash.cloneDeep
311
+ for (const path of paths) {
312
+ const keys = path.split(".");
313
+ let target: any = clone;
314
+
315
+ for (let i = 0; i < keys.length - 1; i++) {
316
+ if (!(keys[i] in target)) break;
317
+ target = target[keys[i]];
318
+ }
319
+
320
+ delete target?.[keys[keys.length - 1]];
321
+ }
322
+
323
+ return clone;
324
+ }