@avleon/core 0.0.20 → 0.0.25

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 (75) hide show
  1. package/README.md +559 -2
  2. package/dist/application.d.ts +47 -0
  3. package/dist/application.js +50 -0
  4. package/dist/cache.d.ts +12 -0
  5. package/dist/cache.js +78 -0
  6. package/dist/collection.js +3 -2
  7. package/dist/container.d.ts +1 -0
  8. package/dist/container.js +2 -1
  9. package/dist/environment-variables.js +1 -1
  10. package/dist/file-storage.js +0 -128
  11. package/dist/helpers.d.ts +2 -0
  12. package/dist/helpers.js +41 -4
  13. package/dist/icore.d.ts +93 -42
  14. package/dist/icore.js +319 -251
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.js +4 -2
  17. package/dist/middleware.js +0 -8
  18. package/dist/multipart.js +4 -1
  19. package/dist/openapi.d.ts +37 -37
  20. package/dist/params.js +2 -2
  21. package/dist/response.js +6 -2
  22. package/dist/route-methods.js +1 -1
  23. package/dist/swagger-schema.js +4 -1
  24. package/dist/testing.js +6 -3
  25. package/dist/utils/index.d.ts +2 -0
  26. package/dist/utils/index.js +18 -0
  27. package/dist/utils/optional-require.d.ts +8 -0
  28. package/dist/utils/optional-require.js +70 -0
  29. package/dist/validation.d.ts +4 -1
  30. package/dist/validation.js +10 -5
  31. package/package.json +26 -9
  32. package/src/application.ts +125 -0
  33. package/src/authentication.ts +16 -0
  34. package/src/cache.ts +91 -0
  35. package/src/collection.ts +254 -0
  36. package/src/config.ts +42 -0
  37. package/src/constants.ts +1 -0
  38. package/src/container.ts +54 -0
  39. package/src/controller.ts +127 -0
  40. package/src/decorators.ts +27 -0
  41. package/src/environment-variables.ts +46 -0
  42. package/src/exceptions/http-exceptions.ts +86 -0
  43. package/src/exceptions/index.ts +1 -0
  44. package/src/exceptions/system-exception.ts +34 -0
  45. package/src/file-storage.ts +206 -0
  46. package/src/helpers.ts +328 -0
  47. package/src/icore.ts +1140 -0
  48. package/src/index.ts +30 -0
  49. package/src/logger.ts +72 -0
  50. package/src/map-types.ts +159 -0
  51. package/src/middleware.ts +98 -0
  52. package/src/multipart.ts +116 -0
  53. package/src/openapi.ts +372 -0
  54. package/src/params.ts +111 -0
  55. package/src/queue.ts +126 -0
  56. package/src/response.ts +117 -0
  57. package/src/results.ts +30 -0
  58. package/src/route-methods.ts +186 -0
  59. package/src/swagger-schema.ts +213 -0
  60. package/src/testing.ts +220 -0
  61. package/src/types/app-builder.interface.ts +19 -0
  62. package/src/types/application.interface.ts +9 -0
  63. package/src/utils/hash.ts +5 -0
  64. package/src/utils/index.ts +2 -0
  65. package/src/utils/optional-require.ts +50 -0
  66. package/src/validation.ts +156 -0
  67. package/src/validator-extend.ts +25 -0
  68. package/dist/classToOpenapi.d.ts +0 -0
  69. package/dist/classToOpenapi.js +0 -1
  70. package/dist/render.d.ts +0 -1
  71. package/dist/render.js +0 -8
  72. package/jest.config.ts +0 -9
  73. package/tsconfig.json +0 -25
  74. /package/dist/{security.d.ts → utils/hash.d.ts} +0 -0
  75. /package/dist/{security.js → utils/hash.js} +0 -0
@@ -0,0 +1,50 @@
1
+
2
+ export function optionalRequire<T = any>(
3
+ moduleName: string,
4
+ options: {
5
+ failOnMissing?: boolean;
6
+ customMessage?: string;
7
+ } = {},
8
+ ): T | undefined {
9
+ try {
10
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
11
+ return require(moduleName);
12
+ } catch (err: any) {
13
+ if (err.code === "MODULE_NOT_FOUND" && err.message.includes(moduleName)) {
14
+ if (options.failOnMissing) {
15
+ throw new Error(
16
+ options.customMessage ||
17
+ `Optional dependency "${moduleName}" is not installed.\nInstall it with:\n\n npm install ${moduleName}`
18
+ );
19
+ }
20
+ return undefined;
21
+ }
22
+ throw err;
23
+ }
24
+ }
25
+
26
+ export async function optionalImport<T = any>(
27
+ moduleName: string,
28
+ options: {
29
+ failOnMissing?: boolean;
30
+ customMessage?: string;
31
+ } = {},
32
+ ): Promise<T | undefined> {
33
+ try {
34
+ const mod = await import(moduleName);
35
+ return mod as T;
36
+ } catch (err: any) {
37
+ if ((err.code === "ERR_MODULE_NOT_FOUND" || err.code === "MODULE_NOT_FOUND") &&
38
+ err.message.includes(moduleName)) {
39
+ if (options.failOnMissing) {
40
+ throw new Error(
41
+ options.customMessage ||
42
+ `Optional dependency "${moduleName}" is not installed.\nInstall it with:\n\n npm install ${moduleName}`
43
+ );
44
+ }
45
+ return undefined;
46
+ }
47
+ throw err;
48
+ }
49
+ }
50
+
@@ -0,0 +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 {}>(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
+ }
@@ -0,0 +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
+ }
File without changes
@@ -1 +0,0 @@
1
- "use strict";
package/dist/render.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function render(viewPath: string, data: any): void;
package/dist/render.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.render = render;
4
- function render(viewPath, data) {
5
- // complie view
6
- // razor
7
- // export
8
- }
package/jest.config.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { Config } from 'jest'
2
-
3
- const config: Config = {
4
- preset: "ts-jest",
5
- testEnvironment: "node",
6
- }
7
-
8
-
9
- export default config;
package/tsconfig.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2017",
4
- "module": "CommonJS",
5
- "strict": true,
6
- "emitDecoratorMetadata": true,
7
- "experimentalDecorators": true,
8
- "outDir": "./dist",
9
- "declaration": true,
10
- "esModuleInterop": true,
11
- "skipLibCheck": true,
12
- "moduleResolution": "node",
13
- "allowSyntheticDefaultImports": true,
14
- "forceConsistentCasingInFileNames": true
15
- },
16
- "include": [
17
- "src/**/*"
18
- ],
19
- "exclude": [
20
- "node_modules",
21
- "dist",
22
- "scripts",
23
- "jest.config.ts"
24
- ]
25
- }
File without changes
File without changes