@avleon/core 0.0.6 → 0.0.7
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/icore.js +5 -13
- package/dist/openapi.d.ts +2 -0
- package/dist/swagger-schema.js +127 -8
- package/package.json +5 -3
package/dist/icore.js
CHANGED
|
@@ -65,7 +65,6 @@ const system_exception_1 = require("./exceptions/system-exception");
|
|
|
65
65
|
const fs_1 = require("fs");
|
|
66
66
|
const exceptions_1 = require("./exceptions");
|
|
67
67
|
const swagger_1 = __importDefault(require("@fastify/swagger"));
|
|
68
|
-
const fastify_api_reference_1 = __importDefault(require("@scalar/fastify-api-reference"));
|
|
69
68
|
const environment_variables_1 = require("./environment-variables");
|
|
70
69
|
const isTsNode = process.env.TS_NODE_DEV ||
|
|
71
70
|
process.env.TS_NODE_PROJECT ||
|
|
@@ -100,13 +99,16 @@ class AvleonApplication {
|
|
|
100
99
|
return environment_variables_1.env["NODE_ENV"] == "development";
|
|
101
100
|
}
|
|
102
101
|
async initSwagger(options) {
|
|
103
|
-
const { routePrefix } = options, restOptions = __rest(options, ["routePrefix"]);
|
|
102
|
+
const { routePrefix, logo, theme } = options, restOptions = __rest(options, ["routePrefix", "logo", "theme"]);
|
|
104
103
|
this.app.register(swagger_1.default, {
|
|
105
104
|
openapi: Object.assign({ openapi: "3.0.0" }, restOptions),
|
|
106
105
|
});
|
|
107
106
|
const rPrefix = routePrefix ? routePrefix : "/docs";
|
|
108
107
|
//import fastifyApiReference from "@scalar/fastify-api-reference";
|
|
109
|
-
await
|
|
108
|
+
//const fastifyApiReference = await require("@scalar/fastify-api-reference");
|
|
109
|
+
await this.app.register(require("@fastify/swagger-ui"), {
|
|
110
|
+
logo: logo ? logo : null,
|
|
111
|
+
theme: theme ? theme : {},
|
|
110
112
|
routePrefix: rPrefix,
|
|
111
113
|
configuration: {
|
|
112
114
|
metaData: {
|
|
@@ -174,16 +176,6 @@ class AvleonApplication {
|
|
|
174
176
|
// handle openapi data
|
|
175
177
|
const swaggerMeta = Reflect.getMetadata("route:openapi", prototype, method) || {};
|
|
176
178
|
const authClsMethodMeata = Reflect.getMetadata(container_1.AUTHORIZATION_META_KEY, ctrl.constructor, method) || { authorize: false, options: undefined };
|
|
177
|
-
console.log(tag, ":", method, authClsMethodMeata);
|
|
178
|
-
// if (authClsMethodMeata.authorize && this.authorizeMiddleware) {
|
|
179
|
-
// this.app.addHook('preHandler', async (req, res) => {
|
|
180
|
-
// if (res.sent) return;
|
|
181
|
-
// console.log(this.authorizeMiddleware)
|
|
182
|
-
// const cls = container.get(this.authorizeMiddleware) as any;
|
|
183
|
-
// console.log(cls)
|
|
184
|
-
// cls.authorize(req);
|
|
185
|
-
// })
|
|
186
|
-
// }
|
|
187
179
|
const allMeta = this._processMeta(prototype, method);
|
|
188
180
|
const routePath = methodmetaOptions.path == "" ? "/" : methodmetaOptions.path;
|
|
189
181
|
this.app.route({
|
package/dist/openapi.d.ts
CHANGED
package/dist/swagger-schema.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.generateSwaggerSchema = generateSwaggerSchema;
|
|
|
4
4
|
function generateSwaggerSchema(classType) {
|
|
5
5
|
const { getMetadataStorage } = require("class-validator");
|
|
6
6
|
const { plainToInstance } = require("class-transformer");
|
|
7
|
+
//const { isArray } = require("lodash"); // Add lodash for array check
|
|
7
8
|
const metadataStorage = getMetadataStorage();
|
|
8
9
|
const validationMetadata = metadataStorage.getTargetValidationMetadatas(classType, "", true);
|
|
9
10
|
const schema = {
|
|
@@ -12,18 +13,136 @@ function generateSwaggerSchema(classType) {
|
|
|
12
13
|
required: [],
|
|
13
14
|
};
|
|
14
15
|
validationMetadata.forEach((meta) => {
|
|
16
|
+
var _a, _b;
|
|
15
17
|
const propertyName = meta.propertyName;
|
|
16
18
|
// Infer the type dynamically using Reflect metadata
|
|
17
19
|
const propertyType = Reflect.getMetadata("design:type", classType.prototype, propertyName);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
let swaggerProperty = {};
|
|
21
|
+
switch (propertyType) {
|
|
22
|
+
case String:
|
|
23
|
+
swaggerProperty.type = "string";
|
|
24
|
+
break;
|
|
25
|
+
case Number:
|
|
26
|
+
swaggerProperty.type = "number";
|
|
27
|
+
break;
|
|
28
|
+
case Boolean:
|
|
29
|
+
swaggerProperty.type = "boolean";
|
|
30
|
+
break;
|
|
31
|
+
case Date:
|
|
32
|
+
swaggerProperty.type = "string";
|
|
33
|
+
swaggerProperty.format = "date-time";
|
|
34
|
+
break;
|
|
35
|
+
case Array:
|
|
36
|
+
// Attempt to infer array item type
|
|
37
|
+
const arrayItemType = Reflect.getMetadata("design:type", classType.prototype, propertyName + "[0]" // Attempt to get array item type. Very fragile.
|
|
38
|
+
);
|
|
39
|
+
if (arrayItemType) {
|
|
40
|
+
swaggerProperty.type = "array";
|
|
41
|
+
swaggerProperty.items = {
|
|
42
|
+
type: arrayItemType.name.toLowerCase(), // basic type inference
|
|
43
|
+
};
|
|
44
|
+
if (arrayItemType === Object) {
|
|
45
|
+
//try to infer the Object type within array
|
|
46
|
+
const nestedSchema = generateSwaggerSchema(Reflect.getMetadata("design:type", classType.prototype, propertyName + "[0]"));
|
|
47
|
+
swaggerProperty.items = nestedSchema;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
swaggerProperty.type = "array";
|
|
52
|
+
swaggerProperty.items = {}; // Array of unknown type
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
case Object:
|
|
56
|
+
//Nested object
|
|
57
|
+
const nestedSchema = generateSwaggerSchema(Reflect.getMetadata("design:type", classType.prototype, propertyName));
|
|
58
|
+
swaggerProperty = nestedSchema;
|
|
59
|
+
break;
|
|
60
|
+
default:
|
|
61
|
+
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
|
|
26
62
|
}
|
|
63
|
+
schema.properties[propertyName] = swaggerProperty;
|
|
64
|
+
(_b = meta.constraints) === null || _b === void 0 ? void 0 : _b.forEach((constraint) => {
|
|
65
|
+
switch (constraint.name) {
|
|
66
|
+
case "isNotEmpty":
|
|
67
|
+
if (!schema.required.includes(propertyName)) {
|
|
68
|
+
schema.required.push(propertyName);
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case "minLength":
|
|
72
|
+
schema.properties[propertyName].minLength = constraint.constraints[0];
|
|
73
|
+
break;
|
|
74
|
+
case "maxLength":
|
|
75
|
+
schema.properties[propertyName].maxLength = constraint.constraints[0];
|
|
76
|
+
break;
|
|
77
|
+
case "min":
|
|
78
|
+
schema.properties[propertyName].minimum = constraint.constraints[0];
|
|
79
|
+
break;
|
|
80
|
+
case "max":
|
|
81
|
+
schema.properties[propertyName].maximum = constraint.constraints[0];
|
|
82
|
+
break;
|
|
83
|
+
case "isEmail":
|
|
84
|
+
schema.properties[propertyName].format = "email";
|
|
85
|
+
break;
|
|
86
|
+
case "isDate":
|
|
87
|
+
schema.properties[propertyName].format = "date-time";
|
|
88
|
+
break;
|
|
89
|
+
case "isIn":
|
|
90
|
+
schema.properties[propertyName].enum = constraint.constraints[0];
|
|
91
|
+
break;
|
|
92
|
+
case "isNumber":
|
|
93
|
+
schema.properties[propertyName].type = "number";
|
|
94
|
+
break;
|
|
95
|
+
case "isInt":
|
|
96
|
+
schema.properties[propertyName].type = "integer";
|
|
97
|
+
break;
|
|
98
|
+
case "isBoolean":
|
|
99
|
+
schema.properties[propertyName].type = "boolean";
|
|
100
|
+
break;
|
|
101
|
+
case "isString":
|
|
102
|
+
schema.properties[propertyName].type = "string";
|
|
103
|
+
break;
|
|
104
|
+
case "isOptional":
|
|
105
|
+
if (schema.required.includes(propertyName)) {
|
|
106
|
+
schema.required = schema.required.filter((item) => item !== propertyName);
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
// Add more cases for other validators as needed
|
|
110
|
+
}
|
|
111
|
+
});
|
|
27
112
|
});
|
|
28
113
|
return schema;
|
|
29
114
|
}
|
|
115
|
+
// export function generateSwaggerSchema(classType: any) {
|
|
116
|
+
// const { getMetadataStorage } = require("class-validator");
|
|
117
|
+
// const { plainToInstance } = require("class-transformer");
|
|
118
|
+
// const metadataStorage = getMetadataStorage();
|
|
119
|
+
// const validationMetadata = metadataStorage.getTargetValidationMetadatas(
|
|
120
|
+
// classType,
|
|
121
|
+
// "",
|
|
122
|
+
// true,
|
|
123
|
+
// );
|
|
124
|
+
// const schema: any = {
|
|
125
|
+
// type: "object",
|
|
126
|
+
// properties: {},
|
|
127
|
+
// required: [],
|
|
128
|
+
// };
|
|
129
|
+
// validationMetadata.forEach((meta: any) => {
|
|
130
|
+
// const propertyName = meta.propertyName;
|
|
131
|
+
// // Infer the type dynamically using Reflect metadata
|
|
132
|
+
// const propertyType = Reflect.getMetadata(
|
|
133
|
+
// "design:type",
|
|
134
|
+
// classType.prototype,
|
|
135
|
+
// propertyName,
|
|
136
|
+
// );
|
|
137
|
+
// schema.properties[propertyName] = {
|
|
138
|
+
// type: propertyType?.name.toLowerCase() || "string", // Default to string if type cannot be inferred
|
|
139
|
+
// };
|
|
140
|
+
// if (meta.name === "isNotEmpty") {
|
|
141
|
+
// schema.required.push(propertyName);
|
|
142
|
+
// }
|
|
143
|
+
// if (meta.name === "minLength") {
|
|
144
|
+
// schema.properties[propertyName].minLength = meta.constraints[0];
|
|
145
|
+
// }
|
|
146
|
+
// });
|
|
147
|
+
// return schema;
|
|
148
|
+
// }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avleon/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"@fastify/static": "^8.1.1",
|
|
26
26
|
"@fastify/swagger": "^9.4.0",
|
|
27
27
|
"@fastify/swagger-ui": "^5.1.0",
|
|
28
|
-
"@scalar/fastify-api-reference": "^1.25.122",
|
|
29
28
|
"class-transformer": "^0.5.1",
|
|
30
29
|
"class-validator": "^0.14.1",
|
|
31
30
|
"dotenv": "^16.4.7",
|
|
@@ -34,12 +33,15 @@
|
|
|
34
33
|
"typedi": "^0.10.0",
|
|
35
34
|
"typeorm": "^0.3.20"
|
|
36
35
|
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@scalar/fastify-api-reference":"^1.25.130"
|
|
38
|
+
},
|
|
37
39
|
"directories": {
|
|
38
40
|
"test": "tests"
|
|
39
41
|
},
|
|
40
42
|
"description": "avleon core",
|
|
41
43
|
"repository": {
|
|
42
44
|
"type": "git",
|
|
43
|
-
"url": "git+https://github.com/
|
|
45
|
+
"url": "git+https://github.com/avleonjs/avleon-core"
|
|
44
46
|
}
|
|
45
47
|
}
|