UNPKG

browser-storage-adapter

Version:

Browser storage adapter to crud 1-n different stores simultaneously

3 lines (2 loc) 6.71 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});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 p(e){return"string"==typeof e?e:JSON.stringify(e)}function h(e){if("string"==typeof e)try{return JSON.parse(e)}catch(e){}return e}function f(e){return s({},e,{driver:e.driver||{},isServer:e.isServer||function(){return!1}})}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)}exports.Driver=void 0,(n=exports.Driver||(exports.Driver={})).LOCAL_STORAGE="localStorage",n.SESSION_STORAGE="sessionStorage",n.COOKIE="cookie";var u=function(){function s(e){this.options=f(e),this.state={},this.initState()}var n=s.prototype;return n.getKeyWithNamespace=function(e){var t="";return void 0!==this.options.namespace&&(t+=this.options.namespace+"_"),t+e},n.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)},n.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},n.getAll=function(){var e,t={};return(e=this.getCookies())&&(t=S({},t,e)),(e=this.getLocalStorageItems())&&(t=S({},t,e)),(e=this.getSessionStorageItems())&&(t=S({},t,e)),t},n.sync=function(e,t){var o=this.get(e);return a(o)&&c(t)&&(o=t),c(o)&&this.set(e,o),o},n.remove=function(e){this.removeState(e),this.removeSessionStorageItem(e),this.removeLocalStorageItem(e),this.removeCookie(e)},n.initState=function(){this.options.isServer()||(this.state={})},n.setState=function(e,t){this.options.isServer()||(this.state[e]=t)},n.getState=function(e){if(!this.options.isServer())return this.state[e]},n.removeState=function(e){this.setState(e,void 0)},n.getBrowserStorageItems=function(e){var t;switch(e){case exports.Driver.SESSION_STORAGE:t=sessionStorage;break;case exports.Driver.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]=h(t.getItem(i[r]))}return o},n.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,p(t))}},n.getLocalStorageItem=function(e){if("undefined"!=typeof localStorage&&this.options.driver.localStorage){var t=this.getKeyWithNamespace(e);return h(localStorage.getItem(t))}},n.getLocalStorageItems=function(){return this.getBrowserStorageItems(exports.Driver.LOCAL_STORAGE)},n.removeLocalStorageItem=function(e){if("undefined"!=typeof localStorage&&this.options.driver.localStorage){var t=this.getKeyWithNamespace(e);localStorage.removeItem(t)}},n.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,p(t))}},n.getSessionStorageItem=function(e){if("undefined"!=typeof sessionStorage&&this.options.driver.sessionStorage){var t=this.getKeyWithNamespace(e);return h(sessionStorage.getItem(t))}},n.getSessionStorageItems=function(){return this.getBrowserStorageItems(exports.Driver.SESSION_STORAGE)},n.removeSessionStorageItem=function(e){if("undefined"!=typeof sessionStorage&&this.options.driver.sessionStorage){var t=this.getKeyWithNamespace(e);sessionStorage.removeItem(t)}},n.getCookies=function(){for(var t=h(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,p=0;p<a.length;p++){var h=a[p],f=h.indexOf("=");if(!(f<0)){var S=h.substr(0,f).trim(),u=h.substr(++f,h.length).trim();'"'==u[0]&&(u=u.slice(1,-1)),null==s[S]&&(s[S]=r(u,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]]=h(decodeURIComponent(t[i[s]]));return t},n.setCookie=function(e,o){if(this.options.driver.cookie)if("function"!=typeof this.options.setCookie){var r=this.getKeyWithNamespace(e),s=S({},"boolean"==typeof this.options.driver.cookie?{}:this.options.driver.cookie),n=p(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 p=s.maxAge-0;if(isNaN(p)||!isFinite(p))throw new TypeError("option maxAge is invalid");c+="; Max-Age="+Math.floor(p)}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)},n.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}},n.removeCookie=function(e){this.setCookie(e,void 0)},s}();exports.Adapter=u,exports.buildOptions=f,exports.decodeValue=h,exports.default=u,exports.encodeValue=p,exports.isSet=c,exports.isUnset=a,module.exports=Object.assign(exports.default,exports); //# sourceMappingURL=index.cjs.map