UNPKG

@churchapps/apihelper

Version:

Library of helper functions not specific to any one ChurchApps project or framework.

70 lines 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomBaseController = void 0; const inversify_express_utils_1 = require("inversify-express-utils"); const LoggingHelper_1 = require("../helpers/LoggingHelper"); const auth_1 = require("../auth"); class CustomBaseController extends inversify_express_utils_1.BaseHttpController { constructor() { super(); this.logger = LoggingHelper_1.LoggingHelper.getCurrent(); } error(errors) { return this.json({ errors }, 500); } denyAccess(errors) { return this.json({ errors }, 401); } authUser() { if (this.httpContext.user === null) return new auth_1.AuthenticatedUser(new auth_1.Principal({})); else return new auth_1.AuthenticatedUser(this.httpContext.user); } include(req, item) { let result = false; if (req.query.include !== undefined) { const value = req.query.include; const items = value.split(","); if (items.indexOf(item) > -1) result = true; } return result; } async actionWrapper(_req, _res, fetchFunction) { try { const result = await fetchFunction(this.authUser()); await this.logger.flush(); return result; } catch (e) { try { this.logger.error(e); await this.logger.flush(); } catch (e) { console.log(e); } return this.internalServerError(e); } } async actionWrapperAnon(_req, _res, fetchFunction) { try { const result = await fetchFunction(); await this.logger.flush(); return result; } catch (e) { try { this.logger.error(e); await this.logger.flush(); } catch (e) { console.log(e); } return this.internalServerError(e); } } } exports.CustomBaseController = CustomBaseController; //# sourceMappingURL=CustomBaseController.js.map