UNPKG

springboot-express

Version:

express-spring-boot

83 lines 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Spring = void 0; const Application_1 = require("./Application"); const Define_1 = require("lib-utils-ts/src/Define"); const logger20js_ts_1 = require("logger20js-ts"); var Spring; (function (Spring) { let AUTH_LEVEL; (function (AUTH_LEVEL) { AUTH_LEVEL[AUTH_LEVEL["ALL"] = 0] = "ALL"; AUTH_LEVEL[AUTH_LEVEL["ADMIN"] = 1] = "ADMIN"; AUTH_LEVEL[AUTH_LEVEL["OPERATOR"] = 2] = "OPERATOR"; AUTH_LEVEL[AUTH_LEVEL["SELLER"] = 4] = "SELLER"; AUTH_LEVEL[AUTH_LEVEL["CUSTOMER"] = 8] = "CUSTOMER"; })(AUTH_LEVEL = Spring.AUTH_LEVEL || (Spring.AUTH_LEVEL = {})); function getHandler(target) { let app = Application_1.Application.getInstance(), handler = null; if (!target.getClass().getName().equals("Function")) { if (!(handler = app.getHandler(target.getClass().getName()))) handler = app.addHandler(target.getClass().newInstance()); return handler; } return null; } Spring.getHandler = getHandler; function userControlHandler(handler, level, target = null) { return (req, res) => { let spring = req["springboot"], user = Define_1.Define.of(spring.getType()); if (user.isNull() || (user.orElse(-1) > level && !level.equals(Spring.AUTH_LEVEL.ALL))) { res.status(401).json({ status: "Unauthorized" }); } else { Object.requireNotNull(handler, "Some bad - Endpoint Handler is null !"); handler.call(target, req, res); } }; } Spring.userControlHandler = userControlHandler; function GetMapping(route, level = Spring.AUTH_LEVEL.ALL) { return function (target, propertyKey, descriptor) { Application_1.Application.getInstance().getApp().get(route, Spring.userControlHandler(target[propertyKey], level, Spring.getHandler(target))); }; } Spring.GetMapping = GetMapping; function PostMapping(route, level = Spring.AUTH_LEVEL.ALL) { return function (target, propertyKey, descriptor) { Application_1.Application.getInstance().getApp().post(route, Spring.userControlHandler(target[propertyKey], level, Spring.getHandler(target))); }; } Spring.PostMapping = PostMapping; function PutMapping(route, level = Spring.AUTH_LEVEL.ALL) { return function (target, propertyKey, descriptor) { Application_1.Application.getInstance().getApp().put(route, Spring.userControlHandler(target[propertyKey], level, Spring.getHandler(target))); }; } Spring.PutMapping = PutMapping; function DeleteMapping(route, level = Spring.AUTH_LEVEL.ALL) { return function (target, propertyKey, descriptor) { Application_1.Application.getInstance().getApp().delete(route, Spring.userControlHandler(target[propertyKey], level, Spring.getHandler(target))); }; } Spring.DeleteMapping = DeleteMapping; function HeadMapping(route, level = Spring.AUTH_LEVEL.ALL) { return function (target, propertyKey, descriptor) { Application_1.Application.getInstance().getApp().head(route, Spring.userControlHandler(target[propertyKey], level, Spring.getHandler(target))); }; } Spring.HeadMapping = HeadMapping; function Configuration(path) { return function (constructor) { Application_1.Application.getInstance().loadConfiguration(path); }; } Spring.Configuration = Configuration; function Instance(constructor) { Application_1.Application.getInstance().setPreValidation(constructor.class().getName()); if (!Application_1.Application.getInstance().getHandler(constructor.class().getName())) logger20js_ts_1.Logger.factory("Spring").warn(`@Sprint.Instance : No instantiated '${constructor.class().getName()}' class was found ! Are there any endpoint defined ?`); } Spring.Instance = Instance; })(Spring = exports.Spring || (exports.Spring = {})); //# sourceMappingURL=Spring.js.map