tsbase
Version:
Base class libraries for TypeScript
89 lines • 3.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CookieStorage = void 0;
var Strings_1 = require("../../System/Strings");
var module_1 = require("../../Patterns/CommandQuery/module");
var JsonSerializer_1 = require("../../Utility/Serialization/JsonSerializer");
var Cookies_1 = require("../../Utility/Web/Cookies");
var CookieOptionKeys;
(function (CookieOptionKeys) {
CookieOptionKeys["Domain"] = "domain";
CookieOptionKeys["Path"] = "path";
CookieOptionKeys["Expires"] = "expires";
CookieOptionKeys["Secure"] = "secure";
CookieOptionKeys["SameSite"] = "samesite";
CookieOptionKeys["Priority"] = "priority";
})(CookieOptionKeys || (CookieOptionKeys = {}));
/**
* Provides a generic interface for interacting with Cookies
*/
var CookieStorage = /** @class */ (function () {
function CookieStorage(serializer, mainDocument) {
if (serializer === void 0) { serializer = new JsonSerializer_1.JsonSerializer(); }
if (mainDocument === void 0) { mainDocument = document; }
this.serializer = serializer;
this.mainDocument = mainDocument;
}
CookieStorage.prototype.Get = function (type, key) {
var _this = this;
return new module_1.Query(function () {
var value = _this.GetValue(key).Value;
if (value) {
return _this.serializer.Deserialize(type, JSON.parse(value));
}
else {
throw new Error("Unable to retrieve \"".concat(key, "\""));
}
}).Execute();
};
CookieStorage.prototype.Set = function (key, value, options) {
var _this = this;
return new module_1.Command(function () {
_this.SetValue(key, JSON.stringify(value), options);
}).Execute();
};
CookieStorage.prototype.GetValue = function (key) {
var _this = this;
return new module_1.Query(function () {
var cookieMap = Cookies_1.Cookies.GetCookieMap(_this.mainDocument);
var value = cookieMap.get(key);
if (value) {
return value;
}
else {
throw new Error("Unable to retrieve \"".concat(key, "\""));
}
}).Execute();
};
CookieStorage.prototype.SetValue = function (key, value, options) {
var _this = this;
if (options === void 0) { options = {
path: '/',
secure: true,
samesite: 'strict'
}; }
return new module_1.Command(function () {
var optionKeys = Object.values(CookieOptionKeys);
var expiresOptionString = options.expires ?
"expires=".concat(options.expires.toUTCString(), ";") : Strings_1.Strings.Empty;
var optionsString = expiresOptionString + optionKeys.map(function (k) { return k !== CookieOptionKeys.Expires && !!options[k]
? "".concat(k, "=").concat(options[k], ";") : Strings_1.Strings.Empty; }).join('');
var newCookie = "".concat(key, "=").concat(value, ";").concat(optionsString);
_this.mainDocument.cookie = newCookie;
}).Execute();
};
CookieStorage.prototype.Remove = function (key, path, domain) {
var _this = this;
if (path === void 0) { path = '/'; }
return new module_1.Command(function () {
_this.SetValue(key, Strings_1.Strings.Empty, {
expires: new Date(0),
domain: domain,
path: path
});
}).Execute();
};
return CookieStorage;
}());
exports.CookieStorage = CookieStorage;
//# sourceMappingURL=CookieStorage.js.map