@avleon/core 0.0.12 → 0.0.14

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.
@@ -7,22 +7,28 @@ exports.generateSwaggerSchema = generateSwaggerSchema;
7
7
  * @email xtrinsic96@gmail.com
8
8
  * @url https://github.com/xtareq
9
9
  */
10
+ const class_validator_1 = require("class-validator");
10
11
  function generateSwaggerSchema(classType) {
11
- const { getMetadataStorage } = require("class-validator");
12
- const { plainToInstance } = require("class-transformer");
13
- //const { isArray } = require("lodash"); // Add lodash for array check
14
- const metadataStorage = getMetadataStorage();
15
- const validationMetadata = metadataStorage.getTargetValidationMetadatas(classType, "", true);
12
+ const metadataStorage = (0, class_validator_1.getMetadataStorage)();
13
+ const validationMetadata = metadataStorage.getTargetValidationMetadatas(classType, "", true, false);
16
14
  const schema = {
17
15
  type: "object",
18
16
  properties: {},
19
17
  required: [],
20
18
  };
21
- validationMetadata.forEach((meta) => {
22
- var _a, _b;
23
- const propertyName = meta.propertyName;
24
- // Infer the type dynamically using Reflect metadata
25
- const propertyType = Reflect.getMetadata("design:type", classType.prototype, propertyName);
19
+ const prototype = classType.prototype;
20
+ const propertyKeys = new Set([
21
+ ...Object.getOwnPropertyNames(prototype),
22
+ ...validationMetadata.map((m) => m.propertyName),
23
+ ]);
24
+ propertyKeys.forEach((propertyName) => {
25
+ var _a;
26
+ if (!propertyName || propertyName === "constructor")
27
+ return;
28
+ const openApiMeta = Reflect.getMetadata("property:openapi", prototype, propertyName);
29
+ if (openApiMeta === null || openApiMeta === void 0 ? void 0 : openApiMeta.exclude)
30
+ return;
31
+ const propertyType = Reflect.getMetadata("design:type", prototype, propertyName);
26
32
  let swaggerProperty = {};
27
33
  switch (propertyType) {
28
34
  case String:
@@ -39,85 +45,101 @@ function generateSwaggerSchema(classType) {
39
45
  swaggerProperty.format = "date-time";
40
46
  break;
41
47
  case Array:
42
- // Attempt to infer array item type
43
- const arrayItemType = Reflect.getMetadata("design:type", classType.prototype, propertyName + "[0]" // Attempt to get array item type. Very fragile.
44
- );
45
- if (arrayItemType) {
46
- swaggerProperty.type = "array";
47
- swaggerProperty.items = {
48
- type: arrayItemType.name.toLowerCase(), // basic type inference
49
- };
50
- if (arrayItemType === Object) {
51
- //try to infer the Object type within array
52
- const nestedSchema = generateSwaggerSchema(Reflect.getMetadata("design:type", classType.prototype, propertyName + "[0]"));
53
- swaggerProperty.items = nestedSchema;
54
- }
55
- }
56
- else {
57
- swaggerProperty.type = "array";
58
- swaggerProperty.items = {}; // Array of unknown type
59
- }
48
+ swaggerProperty.type = "array";
49
+ swaggerProperty.items = { type: "string" }; // fallback
60
50
  break;
61
51
  case Object:
62
- //Nested object
63
- const nestedSchema = generateSwaggerSchema(Reflect.getMetadata("design:type", classType.prototype, propertyName));
64
- swaggerProperty = nestedSchema;
52
+ swaggerProperty = generateSwaggerSchema(propertyType);
65
53
  break;
66
54
  default:
67
- swaggerProperty.type = ((_a = propertyType === null || propertyType === void 0 ? void 0 : propertyType.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || "string"; // Default to string if type cannot be inferred
55
+ swaggerProperty.type = ((_a = propertyType === null || propertyType === void 0 ? void 0 : propertyType.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || "string";
56
+ }
57
+ // Apply OpenApi metadata if present
58
+ if (openApiMeta) {
59
+ swaggerProperty = Object.assign(Object.assign({}, swaggerProperty), extractOpenApiFields(openApiMeta));
68
60
  }
69
61
  schema.properties[propertyName] = swaggerProperty;
70
- (_b = meta.constraints) === null || _b === void 0 ? void 0 : _b.forEach((constraint) => {
71
- switch (constraint.name) {
72
- case "isNotEmpty":
73
- if (!schema.required.includes(propertyName)) {
74
- schema.required.push(propertyName);
75
- }
76
- break;
77
- case "minLength":
78
- schema.properties[propertyName].minLength = constraint.constraints[0];
79
- break;
80
- case "maxLength":
81
- schema.properties[propertyName].maxLength = constraint.constraints[0];
82
- break;
83
- case "min":
84
- schema.properties[propertyName].minimum = constraint.constraints[0];
85
- break;
86
- case "max":
87
- schema.properties[propertyName].maximum = constraint.constraints[0];
88
- break;
89
- case "isEmail":
90
- schema.properties[propertyName].format = "email";
91
- break;
92
- case "isDate":
93
- schema.properties[propertyName].format = "date-time";
94
- break;
95
- case "isIn":
96
- schema.properties[propertyName].enum = constraint.constraints[0];
97
- break;
98
- case "isNumber":
99
- schema.properties[propertyName].type = "number";
100
- break;
101
- case "isInt":
102
- schema.properties[propertyName].type = "integer";
103
- break;
104
- case "isBoolean":
105
- schema.properties[propertyName].type = "boolean";
106
- break;
107
- case "isString":
108
- schema.properties[propertyName].type = "string";
109
- break;
110
- case "isOptional":
111
- if (schema.required.includes(propertyName)) {
112
- schema.required = schema.required.filter((item) => item !== propertyName);
113
- }
114
- break;
115
- // Add more cases for other validators as needed
116
- }
117
- });
62
+ });
63
+ // Handle validation rules
64
+ validationMetadata.forEach((meta) => {
65
+ const propertyName = meta.propertyName;
66
+ switch (meta.name) {
67
+ case "isNotEmpty":
68
+ if (!schema.required.includes(propertyName)) {
69
+ schema.required.push(propertyName);
70
+ }
71
+ break;
72
+ case "isDefined":
73
+ if (!schema.required.includes(propertyName)) {
74
+ schema.required.push(propertyName);
75
+ }
76
+ break;
77
+ case "isOptional":
78
+ schema.required = schema.required.filter((item) => item !== propertyName);
79
+ break;
80
+ case "minLength":
81
+ schema.properties[propertyName].minLength = meta.constraints[0];
82
+ break;
83
+ case "maxLength":
84
+ schema.properties[propertyName].maxLength = meta.constraints[0];
85
+ break;
86
+ case "min":
87
+ schema.properties[propertyName].minimum = meta.constraints[0];
88
+ break;
89
+ case "max":
90
+ schema.properties[propertyName].maximum = meta.constraints[0];
91
+ break;
92
+ case "isEmail":
93
+ schema.properties[propertyName].format = "email";
94
+ break;
95
+ case "isDate":
96
+ schema.properties[propertyName].format = "date-time";
97
+ break;
98
+ case "isIn":
99
+ schema.properties[propertyName].enum = meta.constraints[0];
100
+ break;
101
+ case "isNumber":
102
+ schema.properties[propertyName].type = "number";
103
+ break;
104
+ case "isInt":
105
+ schema.properties[propertyName].type = "integer";
106
+ break;
107
+ case "isBoolean":
108
+ schema.properties[propertyName].type = "boolean";
109
+ break;
110
+ case "isString":
111
+ schema.properties[propertyName].type = "string";
112
+ break;
113
+ }
118
114
  });
119
115
  return schema;
120
116
  }
117
+ function extractOpenApiFields(meta) {
118
+ const result = {};
119
+ const fields = [
120
+ "description",
121
+ "summary",
122
+ "deprecated",
123
+ "example",
124
+ "enum",
125
+ "format",
126
+ "default",
127
+ "minimum",
128
+ "maximum",
129
+ "minLength",
130
+ "maxLength",
131
+ "pattern",
132
+ "oneOf",
133
+ "allOf",
134
+ "anyOf",
135
+ ];
136
+ fields.forEach((field) => {
137
+ if (meta[field] !== undefined) {
138
+ result[field] = meta[field];
139
+ }
140
+ });
141
+ return result;
142
+ }
121
143
  // export function generateSwaggerSchema(classType: any) {
122
144
  // const { getMetadataStorage } = require("class-validator");
123
145
  // const { plainToInstance } = require("class-transformer");
package/package.json CHANGED
@@ -1,11 +1,7 @@
1
1
  {
2
2
  "name": "@avleon/core",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "main": "./dist/index.js",
5
- "types": [
6
- "./dist/index.d.ts",
7
- "./exceptions/index.d.ts"
8
- ],
9
5
  "scripts": {
10
6
  "build": "rimraf dist && tsc",
11
7
  "watch": "tsc-watch",
@@ -37,10 +33,14 @@
37
33
  "bcryptjs": "^3.0.2",
38
34
  "dotenv": "^16.4.7",
39
35
  "fastify": "^5.1.0",
36
+ "pino": "^9.6.0",
37
+ "pino-pretty": "^13.0.0",
40
38
  "reflect-metadata": "^0.2.2",
41
39
  "typedi": "^0.10.0"
42
40
  },
43
41
  "peerDependencies": {
42
+ "class-transformer": "^0.5.1",
43
+ "class-validator": "^0.14.1",
44
44
  "typeorm": "^0.3.20"
45
45
  },
46
46
  "directories": {