UNPKG

zents

Version:

ZenTS is a Node.js & TypeScript MVC-Framework for building rich web applications, released as free and open-source software under the MIT License. It is designed for building web applications with modern tools and design patterns.

91 lines 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = void 0; const BodyParser_1 = require("./BodyParser"); const Cookie_1 = require("./Cookie"); const Request_1 = require("./Request"); const Response_1 = require("./Response"); const ResponseError_1 = require("./ResponseError"); const config_1 = require("../config/config"); class Context { constructor() { this.requestBodyValidationErrors = []; this.isBuild = false; this.isReqBodyValid = true; } async build(request, response, params, route) { var _a, _b; if (this.isBuild) { return; } this.isBuild = true; const method = request.method.toLowerCase(); let body = null; if (method === 'post' || method === 'put') { const bodyParser = new BodyParser_1.BodyParser(); body = await bodyParser.parse(request); } if (typeof route.validationSchema !== 'undefined') { const validationResult = route.validationSchema.validate(body.fields); if (validationResult.error) { this.isReqBodyValid = false; this.requestBodyValidationErrors = validationResult.error.details.map((error) => { return { message: error.message, path: error.path, }; }); } else { body.fields = validationResult.value; } } const cookie = ((_b = (_a = config_1.config.web) === null || _a === void 0 ? void 0 : _a.cookie) === null || _b === void 0 ? void 0 : _b.enable) ? new Cookie_1.Cookie(request.headers) : null; const req = new Request_1.Request(request, body, params); const res = new Response_1.Response(response, req, cookie); const error = new ResponseError_1.ResponseError(res); this.container = { body, cookie, req, res, error, }; } get req() { return this.container.req; } get request() { return this.container.req; } get res() { return this.container.res; } get response() { return this.container.res; } get body() { var _a, _b; return (_b = (_a = this.container.body) === null || _a === void 0 ? void 0 : _a.fields) !== null && _b !== void 0 ? _b : null; } get query() { return this.container.req.query; } get params() { return this.container.req.params; } get cookie() { return this.container.cookie; } get error() { return this.container.error; } get isValid() { return this.isReqBodyValid; } get validationErrors() { return this.requestBodyValidationErrors; } } exports.Context = Context; //# sourceMappingURL=Context.js.map