UNPKG

nestjs-mvc-tools

Version:

NestJS MVC Tools is a small set of tools designed to help you get started more easily with traditional web development approaches in NestJS.

100 lines (99 loc) 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestMvcFlash = void 0; class NestMvcFlash { constructor(req) { this.req = req; this.FLASH_KEY = "__flash__"; } /** * 플래시 메시지 설정 */ flash(key, value) { if (!this.req.session) return; const currentFlash = this.req.session[this.FLASH_KEY] || {}; currentFlash[key] = value; this.req.session[this.FLASH_KEY] = currentFlash; } /** * 플래시 메시지 조회 */ get(key) { if (!this.req.session) return null; const flash = this.req.session[this.FLASH_KEY]; if (!flash) return null; if (key) { return flash[key]; } else { return flash; } } /** * 플래시 메시지 삭제 */ clear(key) { if (!this.req.session) return; const flash = this.req.session[this.FLASH_KEY]; if (!flash) return; if (key) { delete flash[key]; if (Object.keys(flash).length === 0) { delete this.req.session[this.FLASH_KEY]; } } else { delete this.req.session[this.FLASH_KEY]; } } /** * 플래시 메시지 조회 후 삭제 */ getAndClear(key) { const value = this.get(key); this.clear(key); return value; } /** * 성공 메시지 설정 */ success(message) { this.flash("success", message); } /** * 에러 메시지 설정 */ error(message) { this.flash("error", message); return this; } /** * 정보 메시지 설정 */ info(message) { this.flash("info", message); } /** * 경고 메시지 설정 */ warning(message) { this.flash("warning", message); return this; } /** * 폼 입력값 플래시 (PRG 패턴용) */ flashInput(except = []) { if (this.req.method === "POST" && this.req.body) { const input = { ...this.req.body }; except.forEach((key) => delete input[key]); this.flash("input", input); } } } exports.NestMvcFlash = NestMvcFlash;