UNPKG

kamboja-express

Version:

KambojaJS engine implementation using ExpressJs

61 lines (60 loc) 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ResponseAdapter = (function () { function ResponseAdapter(nativeResponse, nativeNextFunction) { this.nativeResponse = nativeResponse; this.nativeNextFunction = nativeNextFunction; } ResponseAdapter.prototype.setup = function (result) { var _this = this; result.status = result.status || 200; result.type = result.type || "text/plain"; if (result.header) this.nativeResponse.set(result.header); this.nativeResponse.status(result.status); if (result.cookies) { result.cookies.forEach(function (x) { _this.nativeResponse.cookie(x.key, x.value, x.options); }); } }; ResponseAdapter.prototype.json = function (result) { this.setup(result); this.nativeResponse.status(result.status).json(result.body); }; ResponseAdapter.prototype.redirect = function (result, path) { this.setup(result); this.nativeResponse.redirect(path); }; ResponseAdapter.prototype.download = function (result, path) { this.setup(result); this.nativeResponse.download(path); }; ResponseAdapter.prototype.file = function (result, path) { this.setup(result); this.nativeResponse.sendFile(path); }; ResponseAdapter.prototype.render = function (result, viewName, model) { this.setup(result); this.nativeResponse.render(viewName, model); }; ResponseAdapter.prototype.send = function (result) { if (result.type == "application/json") return this.json(result); this.setup(result); this.nativeResponse.type(result.type); switch (typeof result.body) { case "number": case "boolean": this.nativeResponse.send(result.body.toString()); break; case "undefined": this.nativeResponse.end(); break; default: this.nativeResponse.send(result.body); } }; return ResponseAdapter; }()); exports.ResponseAdapter = ResponseAdapter;