@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/validation.ts CHANGED
@@ -1,156 +1,160 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
-
8
- import { BadRequestException } from "./exceptions";
9
-
10
- class PValidationRule<T> {
11
- name: string;
12
- type: T;
13
- message?: string;
14
-
15
- constructor(name: string, type: T, message?: string) {
16
- this.name = name;
17
- this.type = type;
18
- this.message = message;
19
- }
20
- }
21
-
22
- type BaseRule = {
23
- required?: boolean;
24
- optional?: boolean;
25
- message?: string;
26
- };
27
-
28
- type StringRule = BaseRule & {
29
- type: "string";
30
- };
31
-
32
- type NumberRule = BaseRule & {
33
- type: "number";
34
- min?: number;
35
- max?: number;
36
- exact?: number;
37
- };
38
-
39
- type BooleanRule = BaseRule & {
40
- type: "boolean";
41
- };
42
-
43
- export type ValidationRule = StringRule | NumberRule | BooleanRule;
44
-
45
- export type ValidationProps = {
46
- [key: string]: ValidationRule;
47
- };
48
-
49
- export type ValidateOptons = {
50
- location?:'header' | 'queryparam' | 'param' | 'body' | 'custom'
51
- }
52
-
53
- class Validator {
54
- private rules: PValidationRule<any>[] = [];
55
- private options:ValidateOptons = {}
56
-
57
- constructor(obj: ValidationProps, options?:ValidateOptons) {
58
- this.init(obj);
59
- if (options) {
60
- this.options = options;
61
- }
62
- }
63
-
64
- private init(obj: ValidationProps) {
65
- Object.keys(obj).forEach((key) => {
66
- const rule = obj[key];
67
- this.rules.push(
68
- new PValidationRule<typeof rule.type>(key, rule.type, rule.message),
69
- );
70
- });
71
- }
72
-
73
- validate(obj: any | Array<any>, options?:ValidateOptons) {
74
- const erors: any[] = [];
75
-
76
- this.rules.forEach((k) => {
77
- const r = Object.keys(obj).find((key) => key == k.name);
78
- let messages: any = [];
79
- if (!r || obj[r] == undefined || obj[r] == "") {
80
- messages.push({
81
- constraint: "required",
82
- message: k.name + " is required",
83
- });
84
- }
85
-
86
- if (k.type == "string" && typeof obj[k.name] != "string") {
87
- messages.push({
88
- constraint: "type",
89
- message: `${k.name} must be type ${k.type}`,
90
- });
91
- }
92
- if (k.type == "number" && !parseInt(obj[k.name])) {
93
- messages.push({
94
- constraint: "type",
95
- message: `${k.name} must be type ${k.type}`,
96
- });
97
- }
98
-
99
- if (k.type == "number") {
100
- obj[k.name] = parseInt(obj[k.name]);
101
- }
102
- if (k.type == "boolean" && !isBool(obj[k.name])) {
103
- messages.push({
104
- constraint: "type",
105
- message: `${k.name} must be type ${k.type}`,
106
- });
107
- }
108
- if (k.type == "boolean") {
109
- obj[k.name] = parseBoolean(obj[k.name]);
110
- }
111
- if (messages.length > 0) {
112
- erors.push({
113
- path: k.name,
114
- ...(this.options.location ? { location: this.options.location } :{}),
115
- constraints: messages,
116
- });
117
- }
118
- });
119
-
120
- return [erors, obj];
121
- }
122
- }
123
-
124
- const isBool = (val: any) => {
125
- if (typeof val == "boolean") return true;
126
- if (parseInt(val) == 0 || parseInt(val) == 1) return true;
127
- if (val == "true" || val == "false") return true;
128
-
129
- return false;
130
- };
131
-
132
- const parseBoolean = (val: any): boolean => {
133
- if (typeof val === "boolean") return val;
134
-
135
- // if (typeof val === "number") {
136
- // return val !== 0; // Common convention: 0 → false, any other number → true
137
- // }
138
-
139
- if (parseInt(val) == 1) return true;
140
- if (typeof val === "string") {
141
- const normalized = val.trim().toLowerCase();
142
- return normalized === "true";
143
- }
144
- return false; // Default for unsupported types (null, undefined, objects, etc.)
145
- };
146
-
147
- export function validateOrThrow<T extends {}>(obj: T, rules: ValidationProps, options?:ValidateOptons) {
148
- const valid = new Validator(rules, options);
149
- const errors = valid.validate(obj);
150
-
151
- if (errors[0].length > 0) {
152
- throw new BadRequestException(errors[0]);
153
- }
154
-
155
- return errors[1];
156
- }
1
+ /**
2
+ * @copyright 2024
3
+ * @author Tareq Hossain
4
+ * @email xtrinsic96@gmail.com
5
+ * @url https://github.com/xtareq
6
+ */
7
+
8
+ import { BadRequestException } from "./exceptions";
9
+
10
+ class PValidationRule<T> {
11
+ name: string;
12
+ type: T;
13
+ message?: string;
14
+
15
+ constructor(name: string, type: T, message?: string) {
16
+ this.name = name;
17
+ this.type = type;
18
+ this.message = message;
19
+ }
20
+ }
21
+
22
+ type BaseRule = {
23
+ required?: boolean;
24
+ optional?: boolean;
25
+ message?: string;
26
+ };
27
+
28
+ type StringRule = BaseRule & {
29
+ type: "string";
30
+ };
31
+
32
+ type NumberRule = BaseRule & {
33
+ type: "number";
34
+ min?: number;
35
+ max?: number;
36
+ exact?: number;
37
+ };
38
+
39
+ type BooleanRule = BaseRule & {
40
+ type: "boolean";
41
+ };
42
+
43
+ export type ValidationRule = StringRule | NumberRule | BooleanRule;
44
+
45
+ export type ValidationProps = {
46
+ [key: string]: ValidationRule;
47
+ };
48
+
49
+ export type ValidateOptons = {
50
+ location?: "header" | "queryparam" | "param" | "body" | "custom";
51
+ };
52
+
53
+ class Validator {
54
+ private rules: PValidationRule<any>[] = [];
55
+ private options: ValidateOptons = {};
56
+
57
+ constructor(obj: ValidationProps, options?: ValidateOptons) {
58
+ this.init(obj);
59
+ if (options) {
60
+ this.options = options;
61
+ }
62
+ }
63
+
64
+ private init(obj: ValidationProps) {
65
+ Object.keys(obj).forEach((key) => {
66
+ const rule = obj[key];
67
+ this.rules.push(
68
+ new PValidationRule<typeof rule.type>(key, rule.type, rule.message),
69
+ );
70
+ });
71
+ }
72
+
73
+ validate(obj: any | Array<any>, options?: ValidateOptons) {
74
+ const erors: any[] = [];
75
+
76
+ this.rules.forEach((k) => {
77
+ const r = Object.keys(obj).find((key) => key == k.name);
78
+ let messages: any = [];
79
+ if (!r || obj[r] == undefined || obj[r] == "") {
80
+ messages.push({
81
+ constraint: "required",
82
+ message: k.name + " is required",
83
+ });
84
+ }
85
+
86
+ if (k.type == "string" && typeof obj[k.name] != "string") {
87
+ messages.push({
88
+ constraint: "type",
89
+ message: `${k.name} must be type ${k.type}`,
90
+ });
91
+ }
92
+ if (k.type == "number" && !parseInt(obj[k.name])) {
93
+ messages.push({
94
+ constraint: "type",
95
+ message: `${k.name} must be type ${k.type}`,
96
+ });
97
+ }
98
+
99
+ if (k.type == "number") {
100
+ obj[k.name] = parseInt(obj[k.name]);
101
+ }
102
+ if (k.type == "boolean" && !isBool(obj[k.name])) {
103
+ messages.push({
104
+ constraint: "type",
105
+ message: `${k.name} must be type ${k.type}`,
106
+ });
107
+ }
108
+ if (k.type == "boolean") {
109
+ obj[k.name] = parseBoolean(obj[k.name]);
110
+ }
111
+ if (messages.length > 0) {
112
+ erors.push({
113
+ path: k.name,
114
+ ...(this.options.location ? { location: this.options.location } : {}),
115
+ constraints: messages,
116
+ });
117
+ }
118
+ });
119
+
120
+ return [erors, obj];
121
+ }
122
+ }
123
+
124
+ const isBool = (val: any) => {
125
+ if (typeof val == "boolean") return true;
126
+ if (parseInt(val) == 0 || parseInt(val) == 1) return true;
127
+ if (val == "true" || val == "false") return true;
128
+
129
+ return false;
130
+ };
131
+
132
+ const parseBoolean = (val: any): boolean => {
133
+ if (typeof val === "boolean") return val;
134
+
135
+ // if (typeof val === "number") {
136
+ // return val !== 0; // Common convention: 0 → false, any other number → true
137
+ // }
138
+
139
+ if (parseInt(val) == 1) return true;
140
+ if (typeof val === "string") {
141
+ const normalized = val.trim().toLowerCase();
142
+ return normalized === "true";
143
+ }
144
+ return false; // Default for unsupported types (null, undefined, objects, etc.)
145
+ };
146
+
147
+ export function validateOrThrow<T extends {}>(
148
+ obj: T,
149
+ rules: ValidationProps,
150
+ options?: ValidateOptons,
151
+ ) {
152
+ const valid = new Validator(rules, options);
153
+ const errors = valid.validate(obj);
154
+
155
+ if (errors[0].length > 0) {
156
+ throw new BadRequestException(errors[0]);
157
+ }
158
+
159
+ return errors[1];
160
+ }
@@ -1,25 +1,25 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
- export function IsArrayNotEmpty(validationOptions?: any) {
8
- const { registerDecorator, ValidationArguments } = require("class-validator");
9
- return function (object: Object, propertyName: string) {
10
- registerDecorator({
11
- name: "isArrayWithAtLeastOneElement",
12
- target: object.constructor,
13
- propertyName: propertyName,
14
- options: validationOptions,
15
- validator: {
16
- validate(value: any, args: any) {
17
- return Array.isArray(value) && value.length > 0;
18
- },
19
- defaultMessage(args: any) {
20
- return `${args.property} must contain at least one item.`;
21
- },
22
- },
23
- });
24
- };
25
- }
1
+ /**
2
+ * @copyright 2024
3
+ * @author Tareq Hossain
4
+ * @email xtrinsic96@gmail.com
5
+ * @url https://github.com/xtareq
6
+ */
7
+ export function IsArrayNotEmpty(validationOptions?: any) {
8
+ const { registerDecorator, ValidationArguments } = require("class-validator");
9
+ return function (object: Object, propertyName: string) {
10
+ registerDecorator({
11
+ name: "isArrayWithAtLeastOneElement",
12
+ target: object.constructor,
13
+ propertyName: propertyName,
14
+ options: validationOptions,
15
+ validator: {
16
+ validate(value: any, args: any) {
17
+ return Array.isArray(value) && value.length > 0;
18
+ },
19
+ defaultMessage(args: any) {
20
+ return `${args.property} must contain at least one item.`;
21
+ },
22
+ },
23
+ });
24
+ };
25
+ }