1 line
2.9 KiB
JSON
1 line
2.9 KiB
JSON
{"ast":null,"code":"import * as cookie from 'cookie';\nimport { hasDocumentCookie, parseCookies, readCookie } from './utils'; // We can't please Rollup and TypeScript at the same time\n// Only way to make both of them work\n\nvar objectAssign = require('object-assign');\n\nvar Cookies =\n/** @class */\nfunction () {\n function Cookies(cookies, options) {\n var _this = this;\n\n this.changeListeners = [];\n this.HAS_DOCUMENT_COOKIE = false;\n this.cookies = parseCookies(cookies, options);\n new Promise(function () {\n _this.HAS_DOCUMENT_COOKIE = hasDocumentCookie();\n }).catch(function () {});\n }\n\n Cookies.prototype._updateBrowserValues = function (parseOptions) {\n if (!this.HAS_DOCUMENT_COOKIE) {\n return;\n }\n\n this.cookies = cookie.parse(document.cookie, parseOptions);\n };\n\n Cookies.prototype._emitChange = function (params) {\n for (var i = 0; i < this.changeListeners.length; ++i) {\n this.changeListeners[i](params);\n }\n };\n\n Cookies.prototype.get = function (name, options, parseOptions) {\n if (options === void 0) {\n options = {};\n }\n\n this._updateBrowserValues(parseOptions);\n\n return readCookie(this.cookies[name], options);\n };\n\n Cookies.prototype.getAll = function (options, parseOptions) {\n if (options === void 0) {\n options = {};\n }\n\n this._updateBrowserValues(parseOptions);\n\n var result = {};\n\n for (var name_1 in this.cookies) {\n result[name_1] = readCookie(this.cookies[name_1], options);\n }\n\n return result;\n };\n\n Cookies.prototype.set = function (name, value, options) {\n var _a;\n\n if (typeof value === 'object') {\n value = JSON.stringify(value);\n }\n\n this.cookies = objectAssign({}, this.cookies, (_a = {}, _a[name] = value, _a));\n\n if (this.HAS_DOCUMENT_COOKIE) {\n document.cookie = cookie.serialize(name, value, options);\n }\n\n this._emitChange({\n name: name,\n value: value,\n options: options\n });\n };\n\n Cookies.prototype.remove = function (name, options) {\n var finalOptions = options = objectAssign({}, options, {\n expires: new Date(1970, 1, 1, 0, 0, 1),\n maxAge: 0\n });\n this.cookies = objectAssign({}, this.cookies);\n delete this.cookies[name];\n\n if (this.HAS_DOCUMENT_COOKIE) {\n document.cookie = cookie.serialize(name, '', finalOptions);\n }\n\n this._emitChange({\n name: name,\n value: undefined,\n options: options\n });\n };\n\n Cookies.prototype.addChangeListener = function (callback) {\n this.changeListeners.push(callback);\n };\n\n Cookies.prototype.removeChangeListener = function (callback) {\n var idx = this.changeListeners.indexOf(callback);\n\n if (idx >= 0) {\n this.changeListeners.splice(idx, 1);\n }\n };\n\n return Cookies;\n}();\n\nexport default Cookies;","map":null,"metadata":{},"sourceType":"module"} |