@flexi-ui/storage
Version:
1 lines • 4.77 kB
Source Map (JSON)
{"version":3,"file":"lib.umd.cjs","sources":["../src/fileStorage.ts","../src/localStorage.ts","../src/memoryStorage.ts"],"sourcesContent":["import * as fs from 'node:fs'\n\nexport class FileStorage {\n private filePath: string\n\n constructor(filePath = './theme-storage.json') {\n this.filePath = filePath\n\n if (!fs.existsSync(this.filePath)) {\n fs.writeFileSync(this.filePath, JSON.stringify({}))\n }\n }\n\n private readFile<T>(): Record<string, T> {\n return JSON.parse(fs.readFileSync(this.filePath, 'utf-8'))\n }\n\n private writeFile<T>(data: Record<string, T>): void {\n fs.writeFileSync(this.filePath, JSON.stringify(data, null, 2))\n }\n\n public get<T>(key: string): T | null {\n const data = this.readFile()\n return (data[key] as T) || null\n }\n\n public set<T>(key: string, value: T): void {\n const data = this.readFile()\n data[key] = value\n this.writeFile(data)\n }\n\n public remove(key: string): void {\n const data = this.readFile()\n delete data[key]\n this.writeFile(data)\n }\n\n public clear(): void {\n this.writeFile({})\n }\n}\n","const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7\n\nexport class LocalStorage {\n private storage: globalThis.Storage\n private prefixKey?: string\n\n constructor(prefixKey = '', storage = localStorage) {\n this.storage = storage\n this.prefixKey = prefixKey\n }\n\n private getKey(key: string) {\n return `${this.prefixKey}${key}`.toUpperCase()\n }\n\n public set<T>(key: string, value: T, expire: number | null = DEFAULT_CACHE_TIME): void {\n const stringData = JSON.stringify({\n value,\n expire: expire !== null ? new Date().getTime() + expire * 1000 : null,\n })\n this.storage.setItem(this.getKey(key), stringData)\n }\n\n public get<T>(key: string, def: T = null as T): T {\n const item = this.storage.getItem(this.getKey(key))\n if (item) {\n try {\n const data = JSON.parse(item)\n const { value, expire } = data\n\n if (expire !== null && expire < Date.now()) {\n this.remove(key)\n return def\n }\n\n return value as T\n } catch (e) {\n return def\n }\n }\n return def\n }\n\n public remove(key: string): void {\n this.storage.removeItem(this.getKey(key))\n }\n\n public clear(): void {\n this.storage.clear()\n }\n}\n","export class MemoryStorage {\n private store: Map<string, string>\n\n constructor() {\n this.store = new Map()\n }\n\n public get<T>(key: string): T | null {\n const value = this.store.get(key)\n return value ? (JSON.parse(value) as T) : null\n }\n\n public set<T>(key: string, value: T): void {\n this.store.set(key, JSON.stringify(value))\n }\n\n public remove(key: string): void {\n this.store.delete(key)\n }\n public clear(): void {\n this.store.clear()\n }\n}\n"],"names":["FileStorage","filePath","fs.existsSync","fs.writeFileSync","fs.readFileSync","data","key","value","DEFAULT_CACHE_TIME","LocalStorage","prefixKey","storage","expire","stringData","def","item","MemoryStorage"],"mappings":"4OAEO,MAAMA,CAAY,CAGvB,YAAYC,EAAW,uBAAwB,CAC7C,KAAK,SAAWA,EAEXC,SAAc,KAAK,QAAQ,GAC9BC,SAAiB,KAAK,SAAU,KAAK,UAAU,CAAE,CAAA,CAAC,CACpD,CAGM,UAAiC,CACvC,OAAO,KAAK,MAAMC,SAAgB,KAAK,SAAU,OAAO,CAAC,CAAA,CAGnD,UAAaC,EAA+B,CAC/CF,SAAc,KAAK,SAAU,KAAK,UAAUE,EAAM,KAAM,CAAC,CAAC,CAAA,CAGxD,IAAOC,EAAuB,CAE3B,OADK,KAAK,SAAS,EACdA,CAAG,GAAW,IAAA,CAGtB,IAAOA,EAAaC,EAAgB,CACnC,MAAAF,EAAO,KAAK,SAAS,EAC3BA,EAAKC,CAAG,EAAIC,EACZ,KAAK,UAAUF,CAAI,CAAA,CAGd,OAAOC,EAAmB,CACzB,MAAAD,EAAO,KAAK,SAAS,EAC3B,OAAOA,EAAKC,CAAG,EACf,KAAK,UAAUD,CAAI,CAAA,CAGd,OAAc,CACd,KAAA,UAAU,EAAE,CAAA,CAErB,CCzCA,MAAMG,EAAqB,GAAK,GAAK,GAAK,EAEnC,MAAMC,CAAa,CAIxB,YAAYC,EAAY,GAAIC,EAAU,aAAc,CAClD,KAAK,QAAUA,EACf,KAAK,UAAYD,CAAA,CAGX,OAAOJ,EAAa,CAC1B,MAAO,GAAG,K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