UNPKG

@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.3 kB
"use strict";var e,t=Object.defineProperty,r=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,a=Object.prototype.hasOwnProperty,o={};((e,r)=>{for(var s in r)t(e,s,{get:r[s],enumerable:!0})})(o,{LocalStorageAdapter:()=>l}),module.exports=(e=o,((e,o,i,n)=>{if(o&&"object"===typeof o||"function"===typeof o)for(let l of s(o))a.call(e,l)||l===i||t(e,l,{get:()=>o[l],enumerable:!(n=r(o,l))||n.enumerable});return e})(t({},"__esModule",{value:!0}),e));var i=require("@vegajs/shared");const n=new Error("localStorage is not supported");class l{constructor(){this.storage="undefined"!==typeof window?window?.localStorage:void 0}assertStorageAvailable(){if(!this.storage)throw n}clearItem(e){this.assertStorageAvailable(),this.storage.removeItem(e)}clear(){this.assertStorageAvailable(),this.storage.clear()}getItem(e){return new Promise((t=>t(this.getItemSync(e))))}getItemSync(e){this.assertStorageAvailable();const t=this.storage.getItem(e);if(!t)return null;try{return(0,i.isJsonString)(t)?JSON.parse(t):t}catch(r){return null}}setItem(e,t){return new Promise((r=>r(this.setItemSync(e,t))))}setItemSync(e,t){this.assertStorageAvailable();let r=t;"string"!==typeof t&&(r=JSON.stringify(r)),this.storage.setItem(e,r)}has(e){return this.assertStorageAvailable(),Boolean(this.storage.getItem(e))}}