@vegajs/storage
Version:
A flexible and type-safe storage service adapter for TypeScript and JavaScript. Supports various storage mechanisms like localStorage, query strings, and mock storage with both asynchronous and synchronous APIs.
1 lines • 1.36 kB
JavaScript
;var e,t=Object.defineProperty,r=Object.getOwnPropertyDescriptor,a=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,i={};((e,r)=>{for(var a in r)t(e,a,{get:r[a],enumerable:!0})})(i,{QueryStringAdapter:()=>c}),module.exports=(e=i,((e,i,n,c)=>{if(i&&"object"===typeof i||"function"===typeof i)for(let h of a(i))s.call(e,h)||h===n||t(e,h,{get:()=>i[h],enumerable:!(c=r(i,h))||c.enumerable});return e})(t({},"__esModule",{value:!0}),e));var n=require("@vegajs/shared");class c{constructor(){this.isSSR="undefined"===typeof window,this.searchParams=new URLSearchParams(this.isSSR?"":window.location.search)}updateSearchParams(e){if(!this.isSSR){const t=`${window.location.pathname}?${e.toString()}`;window.history.replaceState(null,"",t)}}clearItem(e){this.searchParams.delete(e),this.updateSearchParams(this.searchParams)}clear(){this.searchParams=new URLSearchParams,this.updateSearchParams(this.searchParams)}getItem(e){return Promise.resolve(this.getItemSync(e))}getItemSync(e){const t=this.searchParams.get(e);if(null===t)return null;try{return(0,n.isJsonString)(t)?JSON.parse(t):t}catch(r){return null}}setItem(e,t){return Promise.resolve(this.setItemSync(e,t))}setItemSync(e,t){const r="string"===typeof t?t:JSON.stringify(t);this.searchParams.set(e,r),this.updateSearchParams(this.searchParams)}has(e){return this.searchParams.has(e)}}