browser-storage-adapter
Version:
Browser storage adapter to crud 1-n different stores simultaneously
3 lines (2 loc) • 6.48 kB
JavaScript
var e=decodeURIComponent,t=encodeURIComponent,o=/; */,i=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;function r(e,t){try{return t(e)}catch(t){return e}}function s(){return s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var i in o)Object.prototype.hasOwnProperty.call(o,i)&&(e[i]=o[i])}return e},s.apply(this,arguments)}var n,a=function(e){return null==e},c=function(e){return!a(e)};function h(e){return"string"==typeof e?e:JSON.stringify(e)}function f(e){if("string"==typeof e)try{return JSON.parse(e)}catch(e){}return e}function S(e){return s({},e,{driver:e.driver||{},isServer:e.isServer||function(){return!1}})}function p(){return p=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var i in o)Object.prototype.hasOwnProperty.call(o,i)&&(e[i]=o[i])}return e},p.apply(this,arguments)}!function(e){e.LOCAL_STORAGE="localStorage",e.SESSION_STORAGE="sessionStorage",e.COOKIE="cookie"}(n||(n={}));var g=function(){function s(e){this.options=S(e),this.state={},this.initState()}var g=s.prototype;return g.getKeyWithNamespace=function(e){var t="";return void 0!==this.options.namespace&&(t+=this.options.namespace+"_"),t+e},g.set=function(e,t){return a(t)?this.remove(e):(this.setState(e,t),this.setCookie(e,t),this.setLocalStorageItem(e,t),this.setSessionStorageItem(e,t),t)},g.get=function(e){var t=this.getState(e);return a(t)&&(t=this.getCookie(e)),a(t)&&(t=this.getLocalStorageItem(e)),a(t)&&(t=this.getSessionStorageItem(e)),a(t)&&(t=this.getState(e)),t},g.getAll=function(){var e,t={};return(e=this.getCookies())&&(t=p({},t,e)),(e=this.getLocalStorageItems())&&(t=p({},t,e)),(e=this.getSessionStorageItems())&&(t=p({},t,e)),t},g.sync=function(e,t){var o=this.get(e);return a(o)&&c(t)&&(o=t),c(o)&&this.set(e,o),o},g.remove=function(e){this.removeState(e),this.removeSessionStorageItem(e),this.removeLocalStorageItem(e),this.removeCookie(e)},g.initState=function(){this.options.isServer()||(this.state={})},g.setState=function(e,t){this.options.isServer()||(this.state[e]=t)},g.getState=function(e){if(!this.options.isServer())return this.state[e]},g.removeState=function(e){this.setState(e,void 0)},g.getBrowserStorageItems=function(e){var t;switch(e){case n.SESSION_STORAGE:t=sessionStorage;break;case n.LOCAL_STORAGE:t=localStorage}if(void 0===t||!this.options.driver[e])return{};for(var o={},i=Object.keys(t),r=i.length;r--;){var s=i[r];if(void 0!==this.options.namespace){if(s.slice(0,this.options.namespace.length)!==this.options.namespace)continue;s=s.replace(this.options.namespace+"_","")}o[s]=f(t.getItem(i[r]))}return o},g.setLocalStorageItem=function(e,t){if(a(t))this.removeLocalStorageItem(e);else if("undefined"!=typeof localStorage&&this.options.driver.localStorage){var o=this.getKeyWithNamespace(e);localStorage.setItem(o,h(t))}},g.getLocalStorageItem=function(e){if("undefined"!=typeof localStorage&&this.options.driver.localStorage){var t=this.getKeyWithNamespace(e);return f(localStorage.getItem(t))}},g.getLocalStorageItems=function(){return this.getBrowserStorageItems(n.LOCAL_STORAGE)},g.removeLocalStorageItem=function(e){if("undefined"!=typeof localStorage&&this.options.driver.localStorage){var t=this.getKeyWithNamespace(e);localStorage.removeItem(t)}},g.setSessionStorageItem=function(e,t){if(a(t))this.removeSessionStorageItem(e);else if("undefined"!=typeof sessionStorage&&this.options.driver.sessionStorage){var o=this.getKeyWithNamespace(e);sessionStorage.setItem(o,h(t))}},g.getSessionStorageItem=function(e){if("undefined"!=typeof sessionStorage&&this.options.driver.sessionStorage){var t=this.getKeyWithNamespace(e);return f(sessionStorage.getItem(t))}},g.getSessionStorageItems=function(){return this.getBrowserStorageItems(n.SESSION_STORAGE)},g.removeSessionStorageItem=function(e){if("undefined"!=typeof sessionStorage&&this.options.driver.sessionStorage){var t=this.getKeyWithNamespace(e);sessionStorage.removeItem(t)}},g.getCookies=function(){for(var t=f(function(t,i){if("string"!=typeof t)throw new TypeError("argument str must be a string");for(var s={},n=i||{},a=t.split(o),c=n.decode||e,h=0;h<a.length;h++){var f=a[h],S=f.indexOf("=");if(!(S<0)){var p=f.substr(0,S).trim(),g=f.substr(++S,f.length).trim();'"'==g[0]&&(g=g.slice(1,-1)),null==s[p]&&(s[p]=r(g,c))}}return s}((this.options.isServer()?this.options.getServerCookies?this.options.getServerCookies():"":document.cookie)||"")||{}),i=Object.keys(t),s=0;s<i.length;s++)t[i[s]]=f(decodeURIComponent(t[i[s]]));return t},g.setCookie=function(e,o){if(this.options.driver.cookie)if("function"!=typeof this.options.setCookie){var r=this.getKeyWithNamespace(e),s=p({},"boolean"==typeof this.options.driver.cookie?{}:this.options.driver.cookie),n=h(o);a(o)&&(s.maxAge=-1);var c=function(e,o,r){var s=r||{},n=s.encode||t;if("function"!=typeof n)throw new TypeError("option encode is invalid");if(!i.test(e))throw new TypeError("argument name is invalid");var a=n(o);if(a&&!i.test(a))throw new TypeError("argument val is invalid");var c=e+"="+a;if(null!=s.maxAge){var h=s.maxAge-0;if(isNaN(h)||!isFinite(h))throw new TypeError("option maxAge is invalid");c+="; Max-Age="+Math.floor(h)}if(s.domain){if(!i.test(s.domain))throw new TypeError("option domain is invalid");c+="; Domain="+s.domain}if(s.path){if(!i.test(s.path))throw new TypeError("option path is invalid");c+="; Path="+s.path}if(s.expires){if("function"!=typeof s.expires.toUTCString)throw new TypeError("option expires is invalid");c+="; Expires="+s.expires.toUTCString()}if(s.httpOnly&&(c+="; HttpOnly"),s.secure&&(c+="; Secure"),s.sameSite)switch("string"==typeof s.sameSite?s.sameSite.toLowerCase():s.sameSite){case!0:c+="; SameSite=Strict";break;case"lax":c+="; SameSite=Lax";break;case"strict":c+="; SameSite=Strict";break;case"none":c+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}return c}(r,n,s);this.options.isServer()?void 0!==this.options.setServerCookie&&this.options.setServerCookie(c):this.options.setServerCookie&&(document.cookie=c)}else this.options.setCookie(e,o)},g.getCookie=function(e){if(this.options.driver.cookie){if("function"==typeof this.options.getCookie)return this.options.getCookie(e);var t,o=this.getKeyWithNamespace(e);return null!=(t=this.getCookies()[o])?t:void 0}},g.removeCookie=function(e){this.setCookie(e,void 0)},s}();export{g as Adapter,n as Driver,S as buildOptions,f as decodeValue,g as default,h as encodeValue,c as isSet,a as isUnset};
//# sourceMappingURL=index.mjs.map