UNPKG

@flexi-ui/storage

Version:

This package is to manage the local, file, and memory storage

1 lines 8.14 kB
{"version":3,"file":"lib.umd.cjs","sources":["../src/fileStorage.ts","../src/localStorage.ts","../src/memoryStorage.ts"],"sourcesContent":["import * as fs from 'node:fs'\n\n/**\n * A simple JSON file-based key-value storage system.\n * Stores all data in a single file on the filesystem.\n */\nexport class FileStorage {\n private filePath: string\n\n /**\n * Creates an instance of FileStorage.\n * If the storage file doesn't exist, it will be created with an empty object.\n *\n * @param {string} [filePath='./storage.json'] - Path to the storage file.\n */\n constructor(filePath: string = './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 /**\n * Retrieves a value from the file storage by key.\n *\n * @template T - The expected return type.\n * @param {string} key - The key of the item to retrieve.\n * @returns {T | null} - The stored value, or `null` if the key doesn't exist.\n */\n public get<T>(key: string): T | null {\n const data = this.readFile()\n return (data[key] as T) || null\n }\n\n /**\n * Stores a key-value pair in the file storage.\n *\n * @template T - The type of the value to store.\n * @param {string} key - The key under which the value will be stored.\n * @param {T} value - The value to store.\n * @returns {void}\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 /**\n * Removes an item from the file storage by key.\n *\n * @param {string} key - The key of the item to remove.\n * @returns {void}\n */\n public remove(key: string): void {\n const data = this.readFile()\n delete data[key]\n this.writeFile(data)\n }\n\n /**\n * Clears all key-value pairs from the file storage.\n *\n * @returns {void}\n */\n public clear(): void {\n this.writeFile({})\n }\n}\n","const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7\n\n/**\n * Default cache duration in seconds (7 days).\n * Used as the default expiration time for stored items.\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 /**\n * Stores a value in local storage with an optional expiration time.\n *\n * @template T - The type of the value to be stored.\n * @param {string} key - The key under which the value will be stored.\n * @param {T} value - The value to store.\n * @param {number | null} [expire=DEFAULT_CACHE_TIME] - Expiration time in seconds. If `null`, the item does not expire.\n * @returns {void}\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 /**\n * Retrieves a value from local storage. If the value is expired or does not exist, returns the default value.\n *\n * @template T - The expected return type of the value.\n * @param {string} key - The key of the stored item.\n * @param {T} [def=null as T] - The default value to return if the item does not exist or is expired.\n * @returns {T} - The stored value or the default value.\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 (_) {\n return def\n }\n }\n return def\n }\n\n /**\n * Removes an item from local storage.\n *\n * @param {string} key - The key of the item to remove.\n * @returns {void}\n */\n public remove(key: string): void {\n this.storage.removeItem(this.getKey(key))\n }\n\n /**\n * Clears all items from the local storage.\n *\n * @returns {void}\n */\n public clear(): void {\n this.storage.clear()\n }\n}\n","/**\n * An in-memory key-value storage implementation using Map.\n * Useful for temporary storage during runtime (non-persistent).\n */\nexport class MemoryStorage {\n private store: Map<string, string>\n\n /**\n * Creates a new instance of MemoryStorage.\n * Initializes an empty in-memory store.\n */\n constructor() {\n this.store = new Map()\n }\n\n /**\n * Retrieves a value from memory storage by key.\n *\n * @template T - The expected return type.\n * @param {string} key - The key to retrieve the value for.\n * @returns {T | null} - The stored value, or `null` if the key does not exist.\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 /**\n * Stores a value in memory under the specified key.\n *\n * @template T - The type of the value to store.\n * @param {string} key - The key under which the value will be stored.\n * @param {T} value - The value to store.\n * @returns {void}\n */\n public set<T>(key: string, value: T): void {\n this.store.set(key, JSON.stringify(value))\n }\n\n /**\n * Removes a value from memory storage by key.\n *\n * @param {string} key - The key to remove.\n * @returns {void}\n */\n public remove(key: string): void {\n this.store.delete(key)\n }\n\n /**\n * Clears all key-value pairs from memory storage.\n *\n * @returns {void}\n */\n public clear(): void {\n this.store.clear()\n }\n}\n"],"names":["FileStorage","filePath","__publicField","fs.existsSync","fs.writeFileSync","fs.readFileSync","data","key","value","DEFAULT_CACHE_TIME","LocalStorage","prefixKey","storage","expire","stringData","def","item","MemoryStorage"],"mappings":"iZAMO,MAAMA,CAAY,CASvB,YAAYC,EAAmB,iBAAkB,CARzCC,EAAA,iBASN,KAAK,SAAWD,EAEXE,SAAc,KAAK,QAAQ,GAC9BC,SAAiB,KAAK,SAAU,KAAK,UAAU,CAAA,CAAE,CAAC,CAEtD,CAEQ,UAAiC,CACvC,OAAO,KAAK,MAAMC,SAAgB,KAAK,SAAU,OAAO,CAAC,CAC3D,CAEQ,UAAaC,EAA+B,CAClDF,SAAiB,KAAK,SAAU,KAAK,UAAUE,EAAM,KAAM,CAAC,CAAC,CAC/D,CASO,IAAOC,EAAuB,CAEnC,OADa,KAAK,SAAA,EACLA,CAAG,GAAW,IAC7B,CAUO,IAAOA,EAAaC,EAAgB,CACzC,MAAMF,EAAO,KAAK,SAAA,EAClBA,EAAKC,CAAG,EAAIC,EACZ,KAAK,UAAUF,CAAI,CACrB,CAQO,OAAOC,EAAmB,CAC/B,MAAMD,EAAO,KAAK,SAAA,EAClB,OAAOA,EAAKC,CAAG,EACf,KAAK,UAAUD,CAAI,CACrB,CAOO,OAAc,CACnB,KAAK,UAAU,EAA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