@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
@@ -1,213 +1,213 @@
1
- /**
2
- * @copyright 2024
3
- * @author Tareq Hossain
4
- * @email xtrinsic96@gmail.com
5
- * @url https://github.com/xtareq
6
- */
7
- import { getMetadataStorage } from "class-validator";
8
-
9
- export function generateSwaggerSchema(classType: any): any {
10
- const metadataStorage = getMetadataStorage();
11
- const validationMetadata = metadataStorage.getTargetValidationMetadatas(
12
- classType,
13
- "",
14
- true,
15
- false
16
- );
17
-
18
- const schema: any = {
19
- type: "object",
20
- properties: {},
21
- required: [],
22
- };
23
-
24
- const prototype = classType.prototype;
25
-
26
- const propertyKeys = new Set([
27
- ...Object.getOwnPropertyNames(prototype),
28
- ...validationMetadata.map((m: any) => m.propertyName),
29
- ]);
30
-
31
- propertyKeys.forEach((propertyName) => {
32
- if (!propertyName || propertyName === "constructor") return;
33
-
34
- const openApiMeta: any = Reflect.getMetadata(
35
- "property:openapi",
36
- prototype,
37
- propertyName
38
- );
39
-
40
- if (openApiMeta?.exclude) return;
41
-
42
- const propertyType = Reflect.getMetadata(
43
- "design:type",
44
- prototype,
45
- propertyName
46
- );
47
-
48
- let swaggerProperty: any = {};
49
-
50
- switch (propertyType) {
51
- case String:
52
- swaggerProperty.type = "string";
53
- break;
54
- case Number:
55
- swaggerProperty.type = "number";
56
- break;
57
- case Boolean:
58
- swaggerProperty.type = "boolean";
59
- break;
60
- case Date:
61
- swaggerProperty.type = "string";
62
- swaggerProperty.format = "date-time";
63
- break;
64
- case Array:
65
- swaggerProperty.type = "array";
66
- swaggerProperty.items = { type: "string" }; // fallback
67
- break;
68
- case Object:
69
- swaggerProperty = generateSwaggerSchema(propertyType);
70
- break;
71
- default:
72
- swaggerProperty.type = propertyType?.name?.toLowerCase() || "string";
73
- }
74
-
75
- // Apply OpenApi metadata if present
76
- if (openApiMeta) {
77
- swaggerProperty = {
78
- ...swaggerProperty,
79
- ...extractOpenApiFields(openApiMeta),
80
- };
81
- }
82
-
83
- schema.properties[propertyName] = swaggerProperty;
84
- });
85
-
86
- // Handle validation rules
87
- validationMetadata.forEach((meta: any) => {
88
- const propertyName = meta.propertyName;
89
- switch (meta.name) {
90
- case "isNotEmpty":
91
- if (!schema.required.includes(propertyName)) {
92
- schema.required.push(propertyName);
93
- }
94
- break;
95
- case "isDefined":
96
- if (!schema.required.includes(propertyName)) {
97
- schema.required.push(propertyName);
98
- }
99
- break;
100
- case "isOptional":
101
- schema.required = schema.required.filter(
102
- (item: any) => item !== propertyName
103
- );
104
- break;
105
- case "minLength":
106
- schema.properties[propertyName].minLength = meta.constraints[0];
107
- break;
108
- case "maxLength":
109
- schema.properties[propertyName].maxLength = meta.constraints[0];
110
- break;
111
- case "min":
112
- schema.properties[propertyName].minimum = meta.constraints[0];
113
- break;
114
- case "max":
115
- schema.properties[propertyName].maximum = meta.constraints[0];
116
- break;
117
- case "isEmail":
118
- schema.properties[propertyName].format = "email";
119
- break;
120
- case "isDate":
121
- schema.properties[propertyName].format = "date-time";
122
- break;
123
- case "isIn":
124
- schema.properties[propertyName].enum = meta.constraints[0];
125
- break;
126
- case "isNumber":
127
- schema.properties[propertyName].type = "number";
128
- break;
129
- case "isInt":
130
- schema.properties[propertyName].type = "integer";
131
- break;
132
- case "isBoolean":
133
- schema.properties[propertyName].type = "boolean";
134
- break;
135
- case "isString":
136
- schema.properties[propertyName].type = "string";
137
- break;
138
- }
139
- });
140
- return schema;
141
- }
142
-
143
- function extractOpenApiFields(meta: any): any {
144
- const result: any = {};
145
- const fields = [
146
- "description",
147
- "summary",
148
- "deprecated",
149
- "example",
150
- "enum",
151
- "format",
152
- "default",
153
- "minimum",
154
- "maximum",
155
- "minLength",
156
- "maxLength",
157
- "pattern",
158
- "oneOf",
159
- "allOf",
160
- "anyOf",
161
- ];
162
-
163
- fields.forEach((field) => {
164
- if (meta[field] !== undefined) {
165
- result[field] = meta[field];
166
- }
167
- });
168
-
169
- return result;
170
- }
171
-
172
- // export function generateSwaggerSchema(classType: any) {
173
- // const { getMetadataStorage } = require("class-validator");
174
- // const { plainToInstance } = require("class-transformer");
175
-
176
- // const metadataStorage = getMetadataStorage();
177
- // const validationMetadata = metadataStorage.getTargetValidationMetadatas(
178
- // classType,
179
- // "",
180
- // true,
181
- // );
182
-
183
- // const schema: any = {
184
- // type: "object",
185
- // properties: {},
186
- // required: [],
187
- // };
188
-
189
- // validationMetadata.forEach((meta: any) => {
190
- // const propertyName = meta.propertyName;
191
-
192
- // // Infer the type dynamically using Reflect metadata
193
- // const propertyType = Reflect.getMetadata(
194
- // "design:type",
195
- // classType.prototype,
196
- // propertyName,
197
- // );
198
-
199
- // schema.properties[propertyName] = {
200
- // type: propertyType?.name.toLowerCase() || "string", // Default to string if type cannot be inferred
201
- // };
202
-
203
- // if (meta.name === "isNotEmpty") {
204
- // schema.required.push(propertyName);
205
- // }
206
-
207
- // if (meta.name === "minLength") {
208
- // schema.properties[propertyName].minLength = meta.constraints[0];
209
- // }
210
- // });
211
-
212
- // return schema;
213
- // }
1
+ /**
2
+ * @copyright 2024
3
+ * @author Tareq Hossain
4
+ * @email xtrinsic96@gmail.com
5
+ * @url https://github.com/xtareq
6
+ */
7
+ import { getMetadataStorage } from "class-validator";
8
+
9
+ export function generateSwaggerSchema(classType: any): any {
10
+ const metadataStorage = getMetadataStorage();
11
+ const validationMetadata = metadataStorage.getTargetValidationMetadatas(
12
+ classType,
13
+ "",
14
+ true,
15
+ false,
16
+ );
17
+
18
+ const schema: any = {
19
+ type: "object",
20
+ properties: {},
21
+ required: [],
22
+ };
23
+
24
+ const prototype = classType.prototype;
25
+
26
+ const propertyKeys = new Set([
27
+ ...Object.getOwnPropertyNames(prototype),
28
+ ...validationMetadata.map((m: any) => m.propertyName),
29
+ ]);
30
+
31
+ propertyKeys.forEach((propertyName) => {
32
+ if (!propertyName || propertyName === "constructor") return;
33
+
34
+ const openApiMeta: any = Reflect.getMetadata(
35
+ "property:openapi",
36
+ prototype,
37
+ propertyName,
38
+ );
39
+
40
+ if (openApiMeta?.exclude) return;
41
+
42
+ const propertyType = Reflect.getMetadata(
43
+ "design:type",
44
+ prototype,
45
+ propertyName,
46
+ );
47
+
48
+ let swaggerProperty: any = {};
49
+
50
+ switch (propertyType) {
51
+ case String:
52
+ swaggerProperty.type = "string";
53
+ break;
54
+ case Number:
55
+ swaggerProperty.type = "number";
56
+ break;
57
+ case Boolean:
58
+ swaggerProperty.type = "boolean";
59
+ break;
60
+ case Date:
61
+ swaggerProperty.type = "string";
62
+ swaggerProperty.format = "date-time";
63
+ break;
64
+ case Array:
65
+ swaggerProperty.type = "array";
66
+ swaggerProperty.items = { type: "string" }; // fallback
67
+ break;
68
+ case Object:
69
+ swaggerProperty = generateSwaggerSchema(propertyType);
70
+ break;
71
+ default:
72
+ swaggerProperty.type = propertyType?.name?.toLowerCase() || "string";
73
+ }
74
+
75
+ // Apply OpenApi metadata if present
76
+ if (openApiMeta) {
77
+ swaggerProperty = {
78
+ ...swaggerProperty,
79
+ ...extractOpenApiFields(openApiMeta),
80
+ };
81
+ }
82
+
83
+ schema.properties[propertyName] = swaggerProperty;
84
+ });
85
+
86
+ // Handle validation rules
87
+ validationMetadata.forEach((meta: any) => {
88
+ const propertyName = meta.propertyName;
89
+ switch (meta.name) {
90
+ case "isNotEmpty":
91
+ if (!schema.required.includes(propertyName)) {
92
+ schema.required.push(propertyName);
93
+ }
94
+ break;
95
+ case "isDefined":
96
+ if (!schema.required.includes(propertyName)) {
97
+ schema.required.push(propertyName);
98
+ }
99
+ break;
100
+ case "isOptional":
101
+ schema.required = schema.required.filter(
102
+ (item: any) => item !== propertyName,
103
+ );
104
+ break;
105
+ case "minLength":
106
+ schema.properties[propertyName].minLength = meta.constraints[0];
107
+ break;
108
+ case "maxLength":
109
+ schema.properties[propertyName].maxLength = meta.constraints[0];
110
+ break;
111
+ case "min":
112
+ schema.properties[propertyName].minimum = meta.constraints[0];
113
+ break;
114
+ case "max":
115
+ schema.properties[propertyName].maximum = meta.constraints[0];
116
+ break;
117
+ case "isEmail":
118
+ schema.properties[propertyName].format = "email";
119
+ break;
120
+ case "isDate":
121
+ schema.properties[propertyName].format = "date-time";
122
+ break;
123
+ case "isIn":
124
+ schema.properties[propertyName].enum = meta.constraints[0];
125
+ break;
126
+ case "isNumber":
127
+ schema.properties[propertyName].type = "number";
128
+ break;
129
+ case "isInt":
130
+ schema.properties[propertyName].type = "integer";
131
+ break;
132
+ case "isBoolean":
133
+ schema.properties[propertyName].type = "boolean";
134
+ break;
135
+ case "isString":
136
+ schema.properties[propertyName].type = "string";
137
+ break;
138
+ }
139
+ });
140
+ return schema;
141
+ }
142
+
143
+ function extractOpenApiFields(meta: any): any {
144
+ const result: any = {};
145
+ const fields = [
146
+ "description",
147
+ "summary",
148
+ "deprecated",
149
+ "example",
150
+ "enum",
151
+ "format",
152
+ "default",
153
+ "minimum",
154
+ "maximum",
155
+ "minLength",
156
+ "maxLength",
157
+ "pattern",
158
+ "oneOf",
159
+ "allOf",
160
+ "anyOf",
161
+ ];
162
+
163
+ fields.forEach((field) => {
164
+ if (meta[field] !== undefined) {
165
+ result[field] = meta[field];
166
+ }
167
+ });
168
+
169
+ return result;
170
+ }
171
+
172
+ // export function generateSwaggerSchema(classType: any) {
173
+ // const { getMetadataStorage } = require("class-validator");
174
+ // const { plainToInstance } = require("class-transformer");
175
+
176
+ // const metadataStorage = getMetadataStorage();
177
+ // const validationMetadata = metadataStorage.getTargetValidationMetadatas(
178
+ // classType,
179
+ // "",
180
+ // true,
181
+ // );
182
+
183
+ // const schema: any = {
184
+ // type: "object",
185
+ // properties: {},
186
+ // required: [],
187
+ // };
188
+
189
+ // validationMetadata.forEach((meta: any) => {
190
+ // const propertyName = meta.propertyName;
191
+
192
+ // // Infer the type dynamically using Reflect metadata
193
+ // const propertyType = Reflect.getMetadata(
194
+ // "design:type",
195
+ // classType.prototype,
196
+ // propertyName,
197
+ // );
198
+
199
+ // schema.properties[propertyName] = {
200
+ // type: propertyType?.name.toLowerCase() || "string", // Default to string if type cannot be inferred
201
+ // };
202
+
203
+ // if (meta.name === "isNotEmpty") {
204
+ // schema.required.push(propertyName);
205
+ // }
206
+
207
+ // if (meta.name === "minLength") {
208
+ // schema.properties[propertyName].minLength = meta.constraints[0];
209
+ // }
210
+ // });
211
+
212
+ // return schema;
213
+ // }