@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.
- package/dist/classToOpenapi.d.ts +0 -0
- package/dist/classToOpenapi.js +1 -0
- package/dist/exceptions/http-exceptions.d.ts +0 -9
- package/dist/exceptions/http-exceptions.js +17 -9
- package/dist/file-storage.d.ts +22 -5
- package/dist/file-storage.js +203 -55
- package/dist/helpers.d.ts +2 -2
- package/dist/helpers.js +23 -17
- package/dist/icore.d.ts +28 -15
- package/dist/icore.js +87 -52
- package/dist/index.d.ts +3 -0
- package/dist/index.js +32 -7
- package/dist/logger.d.ts +12 -0
- package/dist/logger.js +87 -0
- package/dist/multipart.d.ts +3 -3
- package/dist/multipart.js +7 -2
- package/dist/params.js +4 -0
- package/dist/response.d.ts +13 -3
- package/dist/response.js +51 -9
- package/dist/swagger-schema.d.ts +0 -6
- package/dist/swagger-schema.js +102 -80
- package/package.json +5 -5
package/dist/swagger-schema.js
CHANGED
|
@@ -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
|
|
12
|
-
const
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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";
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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.
|
|
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": {
|