@avleon/core 0.0.4 → 0.0.5
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/README.md +3 -0
- package/dist/authentication.d.ts +9 -0
- package/dist/authentication.js +11 -0
- package/dist/collection.d.ts +52 -0
- package/dist/collection.js +282 -0
- package/dist/config.d.ts +1 -1
- package/dist/config.js +2 -2
- package/dist/container.d.ts +4 -5
- package/dist/container.js +9 -13
- package/dist/decorators.d.ts +2 -1
- package/dist/decorators.js +12 -2
- package/dist/environment-variables.d.ts +3 -0
- package/dist/environment-variables.js +33 -0
- package/dist/exceptions/http-exceptions.d.ts +24 -1
- package/dist/exceptions/http-exceptions.js +33 -2
- package/dist/helpers.d.ts +8 -1
- package/dist/helpers.js +55 -27
- package/dist/icore.d.ts +60 -16
- package/dist/icore.js +354 -134
- package/dist/index.d.ts +10 -6
- package/dist/index.js +6 -1
- package/dist/map-types.js +1 -2
- package/dist/middleware.d.ts +10 -0
- package/dist/middleware.js +37 -0
- package/dist/params.d.ts +1 -0
- package/dist/params.js +13 -9
- package/dist/queue.d.ts +30 -0
- package/dist/queue.js +96 -0
- package/dist/render.d.ts +1 -0
- package/dist/render.js +8 -0
- package/dist/response.d.ts +3 -2
- package/dist/response.js +9 -15
- package/dist/types/app-builder.interface.d.ts +9 -0
- package/dist/types/app-builder.interface.js +2 -0
- package/dist/types/application.interface.d.ts +2 -0
- package/dist/types/application.interface.js +2 -0
- package/dist/validator-extend.js +2 -2
- package/package.json +45 -39
- package/dist/repository.d.ts +0 -0
- package/dist/repository.js +0 -1
package/dist/icore.js
CHANGED
|
@@ -32,11 +32,29 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
32
32
|
return result;
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
36
|
+
var t = {};
|
|
37
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
38
|
+
t[p] = s[p];
|
|
39
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
40
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
41
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
42
|
+
t[p[i]] = s[p[i]];
|
|
43
|
+
}
|
|
44
|
+
return t;
|
|
45
|
+
};
|
|
46
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
47
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
48
|
+
var m = o[Symbol.asyncIterator], i;
|
|
49
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
50
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
51
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
52
|
+
};
|
|
35
53
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
54
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
55
|
};
|
|
38
56
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.
|
|
57
|
+
exports.Builder = void 0;
|
|
40
58
|
const fastify_1 = __importDefault(require("fastify"));
|
|
41
59
|
const typedi_1 = __importDefault(require("typedi"));
|
|
42
60
|
const promises_1 = __importDefault(require("fs/promises")); // Use promises for asynchronous file operations
|
|
@@ -44,196 +62,398 @@ const path_1 = __importDefault(require("path"));
|
|
|
44
62
|
const container_1 = __importStar(require("./container"));
|
|
45
63
|
const helpers_1 = require("./helpers");
|
|
46
64
|
const system_exception_1 = require("./exceptions/system-exception");
|
|
47
|
-
const
|
|
65
|
+
const fs_1 = require("fs");
|
|
66
|
+
const exceptions_1 = require("./exceptions");
|
|
67
|
+
const swagger_1 = __importDefault(require("@fastify/swagger"));
|
|
68
|
+
const fastify_api_reference_1 = __importDefault(require("@scalar/fastify-api-reference"));
|
|
69
|
+
const environment_variables_1 = require("./environment-variables");
|
|
70
|
+
const isTsNode = process.env.TS_NODE_DEV ||
|
|
71
|
+
process.env.TS_NODE_PROJECT ||
|
|
72
|
+
process[Symbol.for("ts-node.register.instance")];
|
|
48
73
|
const controllerDir = path_1.default.join(process.cwd(), isTsNode ? "./src/controllers" : "./dist/cotrollers");
|
|
49
|
-
|
|
74
|
+
// InternalApplication
|
|
75
|
+
class AvleonApplication {
|
|
50
76
|
constructor() {
|
|
51
77
|
this.routeSet = new Set(); // Use Set for fast duplicate detection
|
|
52
78
|
this.alreadyRun = false;
|
|
53
79
|
this.routes = new Map();
|
|
80
|
+
this.middlewares = new Map();
|
|
81
|
+
this.rMap = new Map();
|
|
82
|
+
this.hasSwagger = false;
|
|
83
|
+
this.globalSwaggerOptions = {};
|
|
84
|
+
this.controllers = [];
|
|
85
|
+
this.metaCache = new Map();
|
|
54
86
|
this.app = (0, fastify_1.default)();
|
|
87
|
+
// this.app.setValidatorCompiler(() => () => true);
|
|
55
88
|
}
|
|
56
89
|
static getInternalApp(buildOptions) {
|
|
57
|
-
if (!
|
|
58
|
-
|
|
90
|
+
if (!AvleonApplication.instance) {
|
|
91
|
+
AvleonApplication.instance = new AvleonApplication();
|
|
92
|
+
}
|
|
93
|
+
AvleonApplication.buildOptions = buildOptions;
|
|
94
|
+
if (buildOptions.controllers) {
|
|
95
|
+
}
|
|
96
|
+
return AvleonApplication.instance;
|
|
97
|
+
}
|
|
98
|
+
isDevelopment() {
|
|
99
|
+
return environment_variables_1.env['NODE_ENV'] == "development";
|
|
100
|
+
}
|
|
101
|
+
async initSwagger(options) {
|
|
102
|
+
const { routePrefix } = options, restOptions = __rest(options, ["routePrefix"]);
|
|
103
|
+
this.app.register(swagger_1.default, {
|
|
104
|
+
openapi: Object.assign({ openapi: '3.0.0' }, restOptions)
|
|
105
|
+
});
|
|
106
|
+
const rPrefix = routePrefix ? routePrefix : "/docs";
|
|
107
|
+
//import fastifyApiReference from "@scalar/fastify-api-reference";
|
|
108
|
+
await this.app.register(fastify_api_reference_1.default, {
|
|
109
|
+
routePrefix: rPrefix,
|
|
110
|
+
configuration: {
|
|
111
|
+
metaData: {
|
|
112
|
+
title: 'Avleon Api',
|
|
113
|
+
ogTitle: 'Avleon'
|
|
114
|
+
},
|
|
115
|
+
theme: 'kepler',
|
|
116
|
+
favicon: '/static/favicon.png'
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
async useSwagger(options) {
|
|
121
|
+
this.hasSwagger = true;
|
|
122
|
+
this.globalSwaggerOptions = options;
|
|
123
|
+
}
|
|
124
|
+
handleMiddlewares(mclasses) {
|
|
125
|
+
for (const mclass of mclasses) {
|
|
126
|
+
const cls = typedi_1.default.get(mclass.constructor);
|
|
127
|
+
this.middlewares.set(mclass.name, cls);
|
|
128
|
+
this.app.addHook("preHandler", cls.invoke);
|
|
59
129
|
}
|
|
60
|
-
|
|
61
|
-
|
|
130
|
+
}
|
|
131
|
+
executeMiddlewares(target, propertyKey) {
|
|
132
|
+
const classMiddlewares = Reflect.getMetadata("controller:middleware", target.constructor) || [];
|
|
133
|
+
const methodMiddlewares = propertyKey
|
|
134
|
+
? Reflect.getMetadata("route:middleware", target, propertyKey) || []
|
|
135
|
+
: [];
|
|
136
|
+
return [...classMiddlewares, ...methodMiddlewares];
|
|
62
137
|
}
|
|
63
138
|
async buildController(controller) {
|
|
139
|
+
var _a, e_1, _b, _c;
|
|
64
140
|
const ctrl = typedi_1.default.get(controller);
|
|
65
141
|
const controllerMeta = Reflect.getMetadata(container_1.CONTROLLER_META_KEY, ctrl.constructor);
|
|
66
142
|
if (!controllerMeta)
|
|
67
143
|
return;
|
|
68
144
|
const prototype = Object.getPrototypeOf(ctrl);
|
|
69
145
|
const methods = Object.getOwnPropertyNames(prototype).filter((name) => name !== "constructor");
|
|
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
|
-
|
|
146
|
+
let classMiddlewares = [];
|
|
147
|
+
const tag = ctrl.constructor.name.replace("Controller", "");
|
|
148
|
+
const swaggerControllerMeta = Reflect.getMetadata("controller:openapi", ctrl.constructor) || {};
|
|
149
|
+
try {
|
|
150
|
+
for (var _d = true, methods_1 = __asyncValues(methods), methods_1_1; methods_1_1 = await methods_1.next(), _a = methods_1_1.done, !_a; _d = true) {
|
|
151
|
+
_c = methods_1_1.value;
|
|
152
|
+
_d = false;
|
|
153
|
+
const method = _c;
|
|
154
|
+
const methodMeta = Reflect.getMetadata(container_1.ROUTE_META_KEY, prototype, method);
|
|
155
|
+
if (!methodMeta)
|
|
156
|
+
continue;
|
|
157
|
+
const methodmetaOptions = {
|
|
158
|
+
method: methodMeta.method.toLowerCase(),
|
|
159
|
+
path: (0, helpers_1.formatUrl)(controllerMeta.path + methodMeta.path),
|
|
160
|
+
};
|
|
161
|
+
const routeKey = `${methodmetaOptions.method}:${methodmetaOptions.path}`;
|
|
162
|
+
if (!this.routeSet.has(routeKey)) {
|
|
163
|
+
this.routeSet.add(routeKey);
|
|
164
|
+
}
|
|
165
|
+
const classMiddlewares = this.executeMiddlewares(ctrl, method);
|
|
166
|
+
// handle openapi data
|
|
167
|
+
const swaggerMeta = Reflect.getMetadata("route:openapi", prototype, method) || {};
|
|
168
|
+
const allMeta = this._processMeta(prototype, method);
|
|
169
|
+
const routePath = methodmetaOptions.path == "" ? "/" : methodmetaOptions.path;
|
|
170
|
+
this.app.route({
|
|
171
|
+
url: routePath,
|
|
172
|
+
method: methodmetaOptions.method.toUpperCase(),
|
|
173
|
+
schema: Object.assign(Object.assign(Object.assign({}, swaggerControllerMeta), swaggerMeta), { tags: [tag] }),
|
|
174
|
+
handler: async (req, res) => {
|
|
175
|
+
let reqClone = req;
|
|
176
|
+
if (classMiddlewares.length > 0) {
|
|
177
|
+
for (let m of classMiddlewares) {
|
|
178
|
+
const cls = typedi_1.default.get(m.constructor);
|
|
179
|
+
reqClone = await cls.invoke(reqClone, res);
|
|
180
|
+
if (res.sent)
|
|
181
|
+
return;
|
|
97
182
|
}
|
|
98
183
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
184
|
+
const args = await this._mapArgs(reqClone, allMeta);
|
|
185
|
+
for (let bodyMeta of allMeta.body) {
|
|
186
|
+
if (bodyMeta.validatorClass) {
|
|
187
|
+
const err = await (0, helpers_1.validateObjectByInstance)(bodyMeta.dataType, args[bodyMeta.index]);
|
|
188
|
+
if (err) {
|
|
189
|
+
return await res.code(400).send({
|
|
190
|
+
code: 400,
|
|
191
|
+
error: "ValidationError",
|
|
192
|
+
errors: err,
|
|
193
|
+
message: err.message,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const result = await prototype[method].apply(ctrl, args);
|
|
199
|
+
return result;
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
205
|
+
finally {
|
|
206
|
+
try {
|
|
207
|
+
if (!_d && !_a && (_b = methods_1.return)) await _b.call(methods_1);
|
|
208
|
+
}
|
|
209
|
+
finally { if (e_1) throw e_1.error; }
|
|
117
210
|
}
|
|
118
211
|
}
|
|
119
212
|
async _mapArgs(req, meta) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
args[param.index] = value;
|
|
213
|
+
if (!req.hasOwnProperty("_argsCache")) {
|
|
214
|
+
Object.defineProperty(req, "_argsCache", {
|
|
215
|
+
value: new Map(),
|
|
216
|
+
enumerable: false,
|
|
125
217
|
});
|
|
126
218
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
});
|
|
219
|
+
const cache = req._argsCache;
|
|
220
|
+
const cacheKey = JSON.stringify(meta); // Faster key-based lookup
|
|
221
|
+
if (cache.has(cacheKey)) {
|
|
222
|
+
return cache.get(cacheKey);
|
|
132
223
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
224
|
+
const args = meta.params.map((p) => req.params[p.key] || null);
|
|
225
|
+
meta.query.forEach((q) => (args[q.index] = q.key === "all" ? req.query : req.query[q.key]));
|
|
226
|
+
meta.body.forEach((body) => (args[body.index] = req.body));
|
|
227
|
+
meta.currentUser.forEach((user) => (args[user.index] = req.user));
|
|
228
|
+
meta.headers.forEach((header) => (args[header.index] =
|
|
229
|
+
header.key === "all" ? req.headers : req.headers[header.key]));
|
|
230
|
+
cache.set(cacheKey, args);
|
|
231
|
+
return args;
|
|
232
|
+
}
|
|
233
|
+
_processMeta(prototype, method) {
|
|
234
|
+
const cacheKey = `${prototype.constructor.name}_${method}`;
|
|
235
|
+
if (this.metaCache.has(cacheKey)) {
|
|
236
|
+
return this.metaCache.get(cacheKey);
|
|
137
237
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
238
|
+
const meta = {
|
|
239
|
+
params: Reflect.getMetadata(container_1.PARAM_META_KEY, prototype, method) || [],
|
|
240
|
+
query: Reflect.getMetadata(container_1.QUERY_META_KEY, prototype, method) || [],
|
|
241
|
+
body: Reflect.getMetadata(container_1.REQUEST_BODY_META_KEY, prototype, method) || [],
|
|
242
|
+
headers: Reflect.getMetadata(container_1.REQUEST_HEADER_META_KEY, prototype, method) || [],
|
|
243
|
+
currentUser: Reflect.getMetadata(container_1.REQUEST_USER_META_KEY, prototype, method) || [],
|
|
244
|
+
// swagger: Reflect.getMetadata("route:openapi", prototype, method) || {}
|
|
245
|
+
};
|
|
246
|
+
this.metaCache.set(cacheKey, meta);
|
|
247
|
+
return meta;
|
|
248
|
+
}
|
|
249
|
+
async autoControllers() {
|
|
250
|
+
const controllers = [];
|
|
251
|
+
const files = await promises_1.default.readdir(controllerDir);
|
|
252
|
+
for (const file of files) {
|
|
253
|
+
if (isTsNode ? file.endsWith(".ts") : file.endsWith(".js")) {
|
|
254
|
+
const filePath = path_1.default.join(controllerDir, file);
|
|
255
|
+
const module = await Promise.resolve(`${filePath}`).then(s => __importStar(require(s)));
|
|
256
|
+
for (const exported of Object.values(module)) {
|
|
257
|
+
if (typeof exported === "function" && (0, container_1.isApiController)(exported)) {
|
|
258
|
+
//controllers.push(exported);
|
|
259
|
+
this.buildController(exported);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
143
263
|
}
|
|
144
|
-
return args;
|
|
145
264
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const queryMetaList = Reflect.getMetadata(container_1.QUERY_META_KEY, prototype, method) || [];
|
|
149
|
-
const bodyMetaList = Reflect.getMetadata(container_1.REQUEST_BODY_META_KEY, prototype, method) || [];
|
|
150
|
-
const headerMetaList = Reflect.getMetadata(container_1.REQUEST_HEADER_META_KEY, prototype, method) || [];
|
|
151
|
-
return { params: paramsMetaList, query: queryMetaList, body: bodyMetaList, headers: headerMetaList };
|
|
265
|
+
mapControllers(controllers) {
|
|
266
|
+
this.controllers = controllers;
|
|
152
267
|
}
|
|
153
|
-
async
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
268
|
+
async _mapControllers() {
|
|
269
|
+
if (this.controllers.length > 0) {
|
|
270
|
+
for (let controller of this.controllers) {
|
|
271
|
+
if ((0, container_1.isApiController)(controller)) {
|
|
272
|
+
this.buildController(controller);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
157
276
|
}
|
|
158
|
-
|
|
277
|
+
mapControllersAuto() {
|
|
278
|
+
const isExists = (0, fs_1.existsSync)(controllerDir);
|
|
279
|
+
if (isExists) {
|
|
280
|
+
this.autoControllers();
|
|
281
|
+
}
|
|
159
282
|
}
|
|
160
283
|
async handleRoute(args) {
|
|
161
284
|
console.log(args);
|
|
162
285
|
}
|
|
163
|
-
async
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
286
|
+
async mapFn(fn) {
|
|
287
|
+
const original = fn;
|
|
288
|
+
fn = function () {
|
|
289
|
+
console.log(arguments);
|
|
290
|
+
};
|
|
291
|
+
return fn;
|
|
292
|
+
}
|
|
293
|
+
useMiddlewares(mclasses) {
|
|
294
|
+
for (const mclass of mclasses) {
|
|
295
|
+
const cls = typedi_1.default.get(mclass);
|
|
296
|
+
this.middlewares.set(mclass.name, cls);
|
|
297
|
+
this.app.addHook("preHandler", cls.invoke);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
_handleError(error) {
|
|
301
|
+
if (error instanceof exceptions_1.BaseHttpException) {
|
|
302
|
+
return { code: error.code, error: error.name, message: (0, helpers_1.isValidJsonString)(error.message) ? JSON.parse(error.message) : error.message };
|
|
303
|
+
}
|
|
304
|
+
return { code: 500, error: 'INTERNALERROR', message: error.message ? error.message : "Something going wrong." };
|
|
305
|
+
}
|
|
306
|
+
async mapRoute(method, path = "", fn) {
|
|
307
|
+
await this.mapFn(fn); // Assuming mapFn is needed for all methods
|
|
308
|
+
this.app[method](path, async (req, res) => {
|
|
309
|
+
// Dynamic method call
|
|
310
|
+
try {
|
|
311
|
+
const result = await fn.apply(this, [req, res]);
|
|
312
|
+
if (typeof result === "object" && result !== null) {
|
|
313
|
+
res.json(result); // Use res.json for objects
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
res.send(result); // Fallback for other types
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
console.error(`Error in ${method} route handler:`, error);
|
|
321
|
+
const handledErr = this._handleError(error);
|
|
322
|
+
res.status(handledErr.code).send(handledErr);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
_routeHandler(routePath, method, fn) {
|
|
327
|
+
const routeKey = method + ":" + routePath;
|
|
328
|
+
this.rMap.set(routeKey, {
|
|
329
|
+
handler: fn,
|
|
330
|
+
middlewares: [],
|
|
331
|
+
schema: {}
|
|
167
332
|
});
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
333
|
+
this.mapFn(fn);
|
|
334
|
+
const route = {
|
|
335
|
+
useMiddleware: (middlewares) => {
|
|
336
|
+
const midds = Array.isArray(middlewares) ? middlewares : [middlewares];
|
|
337
|
+
const ms = midds.map((mclass) => {
|
|
338
|
+
const cls = typedi_1.default.get(mclass);
|
|
339
|
+
this.middlewares.set(mclass.name, cls);
|
|
340
|
+
return cls.invoke; // Ensure `invoke` runs in the correct context
|
|
341
|
+
});
|
|
342
|
+
const r = this.rMap.get(routeKey);
|
|
343
|
+
if (r) {
|
|
344
|
+
r.middlewares = ms; // Update middlewares array
|
|
345
|
+
}
|
|
346
|
+
return route; // Ensure chaining by returning the same route object
|
|
347
|
+
},
|
|
348
|
+
useSwagger: (options) => {
|
|
349
|
+
const r = this.rMap.get(routeKey);
|
|
350
|
+
if (r) {
|
|
351
|
+
r.schema = options; // Update schema
|
|
352
|
+
}
|
|
353
|
+
return route; // Ensure chaining
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
return route;
|
|
179
357
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
358
|
+
mapGet(path = "", fn) {
|
|
359
|
+
return this._routeHandler(path, "GET", fn);
|
|
360
|
+
}
|
|
361
|
+
mapPost(path = "", fn) {
|
|
362
|
+
return this._routeHandler(path, "POST", fn);
|
|
363
|
+
}
|
|
364
|
+
mapPut(path = "", fn) {
|
|
365
|
+
return this._routeHandler(path, "PUT", fn);
|
|
366
|
+
}
|
|
367
|
+
mapDelete(path = "", fn) {
|
|
368
|
+
return this._routeHandler(path, "DELETE", fn);
|
|
186
369
|
}
|
|
187
370
|
async run(port = 4000) {
|
|
188
371
|
if (this.alreadyRun)
|
|
189
372
|
throw new system_exception_1.SystemUseError("App already running");
|
|
190
373
|
this.alreadyRun = true;
|
|
191
|
-
if (
|
|
374
|
+
if (AvleonApplication.buildOptions.database) {
|
|
375
|
+
}
|
|
376
|
+
this.app.register(require('@fastify/static'), {
|
|
377
|
+
root: path_1.default.join(process.cwd(), 'public'),
|
|
378
|
+
prefix: '/static/'
|
|
379
|
+
});
|
|
380
|
+
//this.app.swagger();
|
|
381
|
+
if (this.hasSwagger) {
|
|
382
|
+
await this.initSwagger(this.globalSwaggerOptions);
|
|
192
383
|
}
|
|
193
|
-
|
|
384
|
+
await this._mapControllers();
|
|
385
|
+
// this.controllers.forEach(controller => {
|
|
386
|
+
// this.buildController(controller)
|
|
387
|
+
// })
|
|
388
|
+
this.rMap.forEach((value, key) => {
|
|
389
|
+
const [m, r] = key.split(":");
|
|
390
|
+
this.app.route({
|
|
391
|
+
method: m,
|
|
392
|
+
url: r,
|
|
393
|
+
schema: value.schema || {},
|
|
394
|
+
preHandler: value.middlewares ? value.middlewares : [],
|
|
395
|
+
handler: async (req, res) => {
|
|
396
|
+
const result = await value.handler.apply(this, [req, res]);
|
|
397
|
+
return result;
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
this.app.setErrorHandler(async (error, req, res) => {
|
|
402
|
+
const handledErr = this._handleError(error);
|
|
403
|
+
if (error instanceof exceptions_1.ValidationErrorException) {
|
|
404
|
+
return res.status(handledErr.code).send({ code: handledErr.code, error: handledErr.error, errors: handledErr.message });
|
|
405
|
+
}
|
|
406
|
+
return res.status(handledErr.code).send(handledErr);
|
|
407
|
+
});
|
|
408
|
+
await this.app.ready();
|
|
194
409
|
await this.app.listen({ port });
|
|
195
|
-
console.log(`Application running on port:
|
|
410
|
+
console.log(`Application running on port: 0.0.0.0:${port}`);
|
|
196
411
|
}
|
|
197
412
|
}
|
|
198
|
-
|
|
199
|
-
|
|
413
|
+
AvleonApplication.buildOptions = {};
|
|
414
|
+
// Applciation Builder
|
|
415
|
+
class Builder {
|
|
200
416
|
constructor() {
|
|
201
417
|
this.alreadyBuilt = false;
|
|
202
|
-
this.
|
|
418
|
+
this.database = false;
|
|
203
419
|
}
|
|
204
|
-
static
|
|
205
|
-
if (!
|
|
206
|
-
|
|
420
|
+
static createAppBuilder() {
|
|
421
|
+
if (!Builder.instance) {
|
|
422
|
+
Builder.instance = new Builder();
|
|
207
423
|
}
|
|
208
|
-
return
|
|
424
|
+
return Builder.instance;
|
|
209
425
|
}
|
|
210
426
|
async registerPlugin(plugin, options) {
|
|
211
427
|
container_1.default.set(plugin, plugin.prototype);
|
|
212
428
|
}
|
|
213
|
-
async
|
|
214
|
-
this.
|
|
429
|
+
async addDataSource(config) {
|
|
430
|
+
if (this.database) {
|
|
431
|
+
throw new system_exception_1.SystemUseError("Datasource already added.");
|
|
432
|
+
}
|
|
433
|
+
this.database = true;
|
|
434
|
+
try {
|
|
435
|
+
const typeorm = await Promise.resolve().then(() => __importStar(require("typeorm")));
|
|
436
|
+
if (!typeorm) {
|
|
437
|
+
throw new system_exception_1.SystemUseError("TypeOrm not installed");
|
|
438
|
+
}
|
|
439
|
+
const datasource = new typeorm.DataSource(config);
|
|
440
|
+
typedi_1.default.set("idatasource", datasource);
|
|
441
|
+
this.dataSource = datasource;
|
|
442
|
+
await datasource.initialize();
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
console.log(error);
|
|
446
|
+
console.error("Database Initialize Error:", error.message);
|
|
447
|
+
}
|
|
215
448
|
}
|
|
216
|
-
|
|
217
|
-
//console.log("Hello", hello())
|
|
449
|
+
build() {
|
|
218
450
|
if (this.alreadyBuilt)
|
|
219
451
|
throw new Error("Already built");
|
|
220
452
|
this.alreadyBuilt = true;
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (isTsNode ? file.endsWith(".ts") : file.endsWith(".js")) {
|
|
225
|
-
const filePath = path_1.default.join(controllerDir, file);
|
|
226
|
-
const module = await Promise.resolve(`${filePath}`).then(s => __importStar(require(s)));
|
|
227
|
-
for (const exported of Object.values(module)) {
|
|
228
|
-
if (typeof exported === "function" && (0, container_1.isApiController)(exported)) {
|
|
229
|
-
controllers.push(exported);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
const app = _InternalApplication.getInternalApp({ database: this.databse });
|
|
235
|
-
controllers.forEach(container_1.registerController);
|
|
453
|
+
const app = AvleonApplication.getInternalApp({
|
|
454
|
+
database: this.database,
|
|
455
|
+
});
|
|
236
456
|
return app;
|
|
237
457
|
}
|
|
238
458
|
}
|
|
239
|
-
exports.
|
|
459
|
+
exports.Builder = Builder;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export { inject } from
|
|
1
|
+
export * from "./icore";
|
|
2
|
+
export { inject, validateRequestBody } from "./helpers";
|
|
3
3
|
export * from "./decorators";
|
|
4
|
+
export * from "./middleware";
|
|
4
5
|
export * from "./config";
|
|
5
6
|
export * from "./openapi";
|
|
6
7
|
export * from "./map-types";
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export
|
|
8
|
+
export * from "./response";
|
|
9
|
+
export * from "./exceptions";
|
|
10
|
+
export * from "./validator-extend";
|
|
11
|
+
export * from "./environment-variables";
|
|
12
|
+
export * from "./collection";
|
|
13
|
+
export * from "./queue";
|
|
14
|
+
export { default as Container } from "./container";
|
package/dist/index.js
CHANGED
|
@@ -17,17 +17,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.Container = exports.inject = void 0;
|
|
20
|
+
exports.Container = exports.validateRequestBody = exports.inject = void 0;
|
|
21
21
|
//export * from "./iqra-core";
|
|
22
22
|
__exportStar(require("./icore"), exports);
|
|
23
23
|
var helpers_1 = require("./helpers");
|
|
24
24
|
Object.defineProperty(exports, "inject", { enumerable: true, get: function () { return helpers_1.inject; } });
|
|
25
|
+
Object.defineProperty(exports, "validateRequestBody", { enumerable: true, get: function () { return helpers_1.validateRequestBody; } });
|
|
25
26
|
__exportStar(require("./decorators"), exports);
|
|
27
|
+
__exportStar(require("./middleware"), exports);
|
|
26
28
|
__exportStar(require("./config"), exports);
|
|
27
29
|
__exportStar(require("./openapi"), exports);
|
|
28
30
|
__exportStar(require("./map-types"), exports);
|
|
29
31
|
__exportStar(require("./response"), exports);
|
|
30
32
|
__exportStar(require("./exceptions"), exports);
|
|
31
33
|
__exportStar(require("./validator-extend"), exports);
|
|
34
|
+
__exportStar(require("./environment-variables"), exports);
|
|
35
|
+
__exportStar(require("./collection"), exports);
|
|
36
|
+
__exportStar(require("./queue"), exports);
|
|
32
37
|
var container_1 = require("./container");
|
|
33
38
|
Object.defineProperty(exports, "Container", { enumerable: true, get: function () { return __importDefault(container_1).default; } });
|
package/dist/map-types.js
CHANGED
|
@@ -19,8 +19,7 @@ function PartialType(BaseClass) {
|
|
|
19
19
|
}
|
|
20
20
|
// Retrieve validation metadata (class-validator)
|
|
21
21
|
const validationMetadata = Reflect.getMetadataKeys(currentPrototype, key);
|
|
22
|
-
validationMetadata.forEach((metadataKey) => {
|
|
23
|
-
});
|
|
22
|
+
validationMetadata.forEach((metadataKey) => { });
|
|
24
23
|
});
|
|
25
24
|
currentPrototype = Object.getPrototypeOf(currentPrototype); // Move up the prototype chain
|
|
26
25
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IRequest, IResponse } from "./icore";
|
|
2
|
+
import { HttpException } from "./exceptions";
|
|
3
|
+
export declare abstract class AppMiddleware {
|
|
4
|
+
abstract invoke(req: IRequest, res?: IResponse): Promise<IRequest | HttpException>;
|
|
5
|
+
}
|
|
6
|
+
export type Constructor<T> = {
|
|
7
|
+
new (...args: any[]): T;
|
|
8
|
+
};
|
|
9
|
+
export declare function Middleware(target: Constructor<AppMiddleware>): void;
|
|
10
|
+
export declare function UseMiddleware<T extends AppMiddleware | (new (...args: any[]) => AppMiddleware)>(options: T | T[]): MethodDecorator & ClassDecorator;
|