@resk/core
Version:
An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla
1 lines • 2.37 kB
JavaScript
;var platform=require('../platform'),json=require('../utils/json'),l=require('../utils/isNonNullString'),S=require('../utils/isPromise');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var l__default=/*#__PURE__*/_interopDefault(l);var S__default=/*#__PURE__*/_interopDefault(S);const o=class o{static get storage(){var n;const t=Reflect.getMetadata(o.sessionStorageMetaData,o);if(g(t)&&(this._storage=t),this._storage)return this._storage;if(platform.Platform.isClientSide()&&typeof window!="undefined"&&window.localStorage&&((n=window.localStorage)!=null&&n.getItem))this._storage={get:r=>window.localStorage.getItem(r),set:(r,i)=>window.localStorage.setItem(r,i),remove:r=>window.localStorage.removeItem(r),removeAll:()=>window.localStorage.clear()};else {let r={};this._storage={get:i=>r[i],set:(i,u)=>r[i]=u,remove:i=>delete r[i],removeAll:()=>r={}};}return this._storage}static set storage(t){g(t)&&Reflect.defineMetadata(o.sessionStorageMetaData,t,o);}static get keyNamespace(){return l__default.default(this._keyNamespace)?this._keyNamespace:""}static set keyNamespace(t){l__default.default(t)&&(this._keyNamespace=t.trim().replace(/\s+/g,"-"));}static sanitizeKey(t){if(!t||!l__default.default(t))return "";t=t.trim().replace(/\s+/g,"-");const n=this.keyNamespace;return n?`${n}-${t}`:t}};o.sessionStorageMetaData=Symbol("sessionStorage"),o._keyNamespace=void 0;let s=o;function a(e){return s.sanitizeKey(e)}const f=(e,t)=>(e=e&&json.JsonHelper.stringify(e,t),e==null&&(e=""),e),m=e=>{if(S__default.default(e))return new Promise((t,n)=>{e.then(r=>{t(json.JsonHelper.parse(r));}).catch(r=>{n(r);});});if(e!=null)return json.JsonHelper.parse(e)},p=e=>{if(e=a(e),s.storage&&e&&typeof e=="string"){const t=s.storage.get(e);return m(t)}},y=e=>{if(e=a(e),s.storage&&e&&typeof e=="string")return s.storage.remove(e)},h=()=>{if(s.storage)return s.storage.removeAll()},g=e=>{if(!e)return false;try{return ["get","set","remove","removeAll"].every(t=>typeof e[t]=="function")}catch(t){return false}},C={get:p,set:(e,t,n=true)=>(e=a(e),s.storage.set(e,f(t,n))),remove:y,handleGetValue:m,sanitizeKey:a,handleSetValue:f,isValidStorage:g,Manager:s,removeAll:h};function _(){return function(e){try{const t=new e;if(!g(t))return;s.storage=t;}catch(t){console.error(t," registering session storage");}}}exports.AttachSessionStorage=_;exports.Session=C;