sipp
Version:
An Opinionated, High-Productivity MVC Web Framework in TypeScript
130 lines • 4.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toResponse = exports.DownloadResponse = exports.NoContentResponse = exports.HTMLResponse = exports.JSONResponse = exports.HTTPRedirect = exports.HTTPResponse = void 0;
const mime_types_1 = __importDefault(require("mime-types"));
const download_1 = require("./download");
const view_1 = require("../view");
const utils_1 = require("../../utils");
class HTTPResponse {
constructor(controllerResponse, headers, status) {
this.controllerResponse = controllerResponse;
this.headers = headers;
this.status = status;
this.mimeType = mime_types_1.default.lookup('txt');
}
handle(req, res) {
this.setStatus(req, res)
.setHeaders(req, res)
.setMimeType(req, res)
.setBody(req, res);
}
setBody(_, res) {
res.send(this.controllerResponse);
return this;
}
setStatus(req, res) {
if (this.status) {
res.status(this.status);
}
else {
if (/post/i.test(req.method)) {
res.status(201);
}
else {
res.status(200);
}
}
return this;
}
setHeaders(req, res) {
if (this.defaultHeaders) {
Object.keys(this.defaultHeaders).forEach((headerName) => {
res.setHeader(headerName, this.defaultHeaders[headerName]);
});
}
if (this.headers) {
Object.keys(this.headers).forEach((headerName) => {
res.setHeader(headerName, this.headers[headerName]);
});
}
return this;
}
setMimeType(req, res) {
this.mimeType && res.set('Content-Type', this.mimeType);
return this;
}
}
exports.HTTPResponse = HTTPResponse;
class HTTPRedirect extends HTTPResponse {
constructor() {
super(...arguments);
this.status = 302;
}
handle(req, res) {
res.redirect(this.status, this.controllerResponse);
}
}
exports.HTTPRedirect = HTTPRedirect;
class JSONResponse extends HTTPResponse {
constructor() {
super(...arguments);
this.mimeType = mime_types_1.default.lookup('json');
}
}
exports.JSONResponse = JSONResponse;
class HTMLResponse extends HTTPResponse {
constructor() {
super(...arguments);
this.mimeType = mime_types_1.default.lookup('html');
}
}
exports.HTMLResponse = HTMLResponse;
class NoContentResponse extends HTTPResponse {
constructor(content = undefined, headers = undefined) {
super(content, headers, 204);
}
}
exports.NoContentResponse = NoContentResponse;
class DownloadResponse extends HTTPResponse {
constructor(download, headers, status) {
super(download, headers, status || 200);
this.mimeType = mime_types_1.default.contentType(download.mimeType);
this.defaultHeaders = {
'Content-Disposition': `attachment; filename="${download.getFileName()}"`,
};
}
setBody(req, res) {
this.controllerResponse.handle(res);
return this;
}
}
exports.DownloadResponse = DownloadResponse;
async function toResponse(response, headers, status) {
if (utils_1.isInstanceOf(HTTPResponse, response)) {
return response;
}
switch (true) {
case response == null:
return new NoContentResponse(undefined, headers);
case response instanceof download_1.Download:
return new DownloadResponse(response, headers, status);
case response instanceof view_1.View:
case response.prototype instanceof view_1.View:
return new HTMLResponse(await response.renderToHtml(), headers, status);
case typeof response === 'string':
case response instanceof String:
return response.startsWith('<') && response.endsWith('>')
? new HTMLResponse(response, headers, status)
: new HTTPResponse(response, headers, status);
case typeof response == 'object':
case Array.isArray(response):
return new JSONResponse(response, headers, status);
default:
return new HTTPResponse(response, headers, status);
}
}
exports.toResponse = toResponse;
//# sourceMappingURL=index.js.map