UNPKG

@tdb/util

Version:
51 lines (50 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var libs_1 = require("../../libs"); var constants_1 = require("../../constants"); var prop_1 = require("./prop"); var SERVER_COOKIE_KEY = '__TDB_TEMP__'; var ServerCookie = (function () { function ServerCookie() { this.cookies = []; } ServerCookie.prototype.add = function (prop, value) { if (value) { var key = prop.key; var options = libs_1.R.isEmpty(prop.options) ? undefined : prop.options; this.cookies = this.cookies.concat([{ key: key, value: value, options: options }]); } return this; }; Object.defineProperty(ServerCookie.prototype, "header", { get: function () { var obj = {}; this.cookies.forEach(function (cookie) { return (obj[cookie.key] = cookie); }); var json = JSON.stringify(obj); return SERVER_COOKIE_KEY + "=" + encodeURIComponent(json); }, enumerable: true, configurable: true }); ServerCookie.prototype.write = function (ctx) { if (!constants_1.IS_BROWSER && this.cookies.length > 0) { var header = this.header; ctx.res.setHeader('Set-Cookie', header); } }; ServerCookie.prototype.clear = function () { this.cookies = []; }; return ServerCookie; }()); exports.ServerCookie = ServerCookie; if (constants_1.IS_BROWSER) { var serverCookies = prop_1.prop(SERVER_COOKIE_KEY, { path: '/' }); var cookies_1 = serverCookies(); if (cookies_1) { Object.keys(cookies_1) .map(function (key) { return cookies_1[key]; }) .forEach(function (cookie) { return prop_1.prop(cookie.key, cookie.options)(cookie.value); }); } serverCookies(null); }