UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

105 lines 3.49 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Redirect = void 0; const symbols = __importStar(require("../../symbol")); const validate_1 = require("../../validate/validate"); const _1 = require("."); class Redirect extends _1.Response { constructor(url = '', code = 302, header = {}) { super(url, code, header); this.needWithInput = false; this.flashSessions = null; this.alt = null; this.forceBack = false; this.cacheControl('no-cache,must-revalidate'); } setUrl(url) { this.setData(url); return this; } go(url, code = 302) { this.setUrl(url).setCode(code); return this; } getUrl() { const data = this.getData(); return data; } back(alt, code = 302) { this.alt = alt; this.forceBack = true; this.setCode(code); return this; } withInput() { this.needWithInput = true; } with(name, value) { if (!name || !value) return this; if (!this.flashSessions) this.flashSessions = {}; if (typeof name === 'object') { Object.keys(name).forEach((key) => { this.flashSessions[key] = name[key]; }); } else { this.flashSessions[name] = value; } return this; } withErrors(val) { if (!val) return this; this.errors = val; return this; } async send(request) { if (this.forceBack) { const url = request.session().get(symbols.SESSION.PREVIOUS) || request.getHeader('Referrer') || this.alt || '/'; this.setUrl(url); } if (this.flashSessions) { const flashKeys = Object.keys(this.flashSessions); for (const key of flashKeys) { request.session().flash(key, this.flashSessions[key]); } } if (this.errors) { if (this.errors instanceof validate_1.Validate) { request.session().flash(symbols.SESSION.ERRORS, this.errors.message.format()); } else { request.session().flash(symbols.SESSION.ERRORS, this.errors); } } this.setHeader('Location', this.getUrl()); return super.send(request); } } exports.Redirect = Redirect; //# sourceMappingURL=redirect.js.map