@jmondi/browser-storage
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -1,8 +0,8 @@ | ||
import { CookieAdapter } from "./cookie_adapter"; | ||
import { CookieAttributes } from "js-cookie"; | ||
export declare abstract class AbstractStorage { | ||
abstract readonly adapter: Storage | CookieAdapter; | ||
abstract readonly adapter: Storage; | ||
readonly storagePrefix: string; | ||
constructor(storagePrefix?: string); | ||
get<T>(key: string): T | null; | ||
set(key: string, value?: any): boolean; | ||
set(key: string, value?: unknown): boolean; | ||
remove(key: string): void; | ||
@@ -17,4 +17,6 @@ clear(): void; | ||
} | ||
export declare class CookieStorage extends AbstractStorage { | ||
readonly adapter: CookieAdapter; | ||
export declare class CookieStorage { | ||
get<T>(key: string): T | null; | ||
remove(key: string, options?: CookieAttributes): void; | ||
set(key: string, value: string, options?: CookieAttributes): void; | ||
} |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CookieStorage = exports.SessionStorage = exports.LocalStorage = exports.AbstractStorage = void 0; | ||
const cookie_adapter_1 = require("./cookie_adapter"); | ||
const js_cookie_1 = __importDefault(require("js-cookie")); | ||
class AbstractStorage { | ||
@@ -11,26 +14,10 @@ storagePrefix; | ||
get(key) { | ||
const item = this.adapter.getItem(this.storagePrefix + key); | ||
if (!item || item === "null") { | ||
return null; | ||
} | ||
try { | ||
return JSON.parse(item); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
return null; | ||
return fromStore(this.adapter.getItem(this.storagePrefix + key)); | ||
} | ||
set(key, value) { | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
else { | ||
value = JSON.stringify(value); | ||
} | ||
try { | ||
this.adapter.setItem(this.storagePrefix + key, value); | ||
this.adapter.setItem(this.storagePrefix + key, toStore(value)); | ||
return true; | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
@@ -55,6 +42,30 @@ return false; | ||
exports.SessionStorage = SessionStorage; | ||
class CookieStorage extends AbstractStorage { | ||
adapter = new cookie_adapter_1.CookieAdapter(); | ||
class CookieStorage { | ||
get(key) { | ||
return fromStore(js_cookie_1.default.get(key)); | ||
} | ||
remove(key, options) { | ||
js_cookie_1.default.remove(key, options); | ||
} | ||
set(key, value, options) { | ||
js_cookie_1.default.set(key, toStore(value), options); | ||
} | ||
} | ||
exports.CookieStorage = CookieStorage; | ||
function toStore(value) { | ||
if (value === undefined) | ||
value = null; | ||
return JSON.stringify(value); | ||
} | ||
function fromStore(item) { | ||
if (typeof item !== "string" || item === "null") { | ||
return null; | ||
} | ||
try { | ||
return JSON.parse(item); | ||
} | ||
catch (e) { | ||
} | ||
return null; | ||
} | ||
//# sourceMappingURL=browser_storage.js.map |
{ | ||
"name": "@jmondi/browser-storage", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"author": "Jason Raimondi <jason@raimondi.us>", | ||
@@ -24,3 +24,3 @@ "main": "dist/browser_storage.js", | ||
}, | ||
"readme": "# @jmondi/browser-storage\n\nSupports null and serializable objects.\n\n## Install\n\n```bash\nnpm install @jmondi/browser-storage\n```\n\n## Usage \n\n### Local Storage\n\nLocal storage is persistent after close.\n\n```typescript\nimport { LocalStorage } from \"@jmondi/browser-storage\";\n\nconst localStorage = new LocalStorage();\n\nlocalStorage.set(\"user1\", null);\nlocalStorage.set(\"user2\", { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" });\n\nconsole.log(localStorage.get(\"user1\"));\n// null\nconsole.log(localStorage.get(\"user2\"));\n// { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" }\n```\n\n### Session Storage\n\nSession storage is reset when the browser is closed.\n\n```typescript\nimport { SessionStorage } from \"@jmondi/browser-storage\";\n\nconst sessionStorage = new SessionStorage();\n\nsessionStorage.set(\"user1\", null);\nsessionStorage.set(\"user2\", { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" });\n\nconsole.log(sessionStorage.get(\"user1\"));\n// null\nconsole.log(sessionStorage.get(\"user2\"));\n// { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" }\n```" | ||
"readme": "# @jmondi/browser-storage\n\nSupports null and serializable objects.\n\n## Install\n\n```bash\nnpm install @jmondi/browser-storage\n```\n\n## Usage \n\n### Local Storage\n\nLocal storage is persistent after close.\n\n```typescript\nimport { LocalStorage } from \"@jmondi/browser-storage\";\n\nconst localStorage = new LocalStorage();\n\nlocalStorage.set(\"user1\", null);\nlocalStorage.set(\"user2\", { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" });\n\nconsole.log(localStorage.get(\"user1\"));\n// null\nconsole.log(localStorage.get(\"user2\"));\n// { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" }\n```\n\n### Session Storage\n\nSession storage is reset when the browser is closed.\n\n```typescript\nimport { SessionStorage } from \"@jmondi/browser-storage\";\n\nconst sessionStorage = new SessionStorage();\n\nsessionStorage.set(\"user1\", null);\nsessionStorage.set(\"user2\", { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" });\n\nconsole.log(sessionStorage.get(\"user1\"));\n// null\nconsole.log(sessionStorage.get(\"user2\"));\n// { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" }\n```\n\n### Cookie Storage\n\nSession storage is reset when the browser is closed.\n\n```typescript\nimport type { CookieAttributes } from \"js-cookie\";\nimport { CookieStorage } from \"@jmondi/browser-storage\";\n\nconst cookieStorage = new CookieStorage();\n\nconst cookieOptions: CookieAttributes = {};\n\ncookieStorage.set(\"user1\", null, cookieOptions);\ncookieStorage.set(\"user2\", { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" });\n\nconsole.log(cookieStorage.get(\"user1\"));\n// null\nconsole.log(cookieStorage.get(\"user2\"));\n// { email: \"hermoine@hogwarts.com\", name: \"Hermoine\" }\n```" | ||
} |
@@ -47,2 +47,23 @@ # @jmondi/browser-storage | ||
// { email: "hermoine@hogwarts.com", name: "Hermoine" } | ||
``` | ||
### Cookie Storage | ||
Session storage is reset when the browser is closed. | ||
```typescript | ||
import type { CookieAttributes } from "js-cookie"; | ||
import { CookieStorage } from "@jmondi/browser-storage"; | ||
const cookieStorage = new CookieStorage(); | ||
const cookieOptions: CookieAttributes = {}; | ||
cookieStorage.set("user1", null, cookieOptions); | ||
cookieStorage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" }); | ||
console.log(cookieStorage.get("user1")); | ||
// null | ||
console.log(cookieStorage.get("user2")); | ||
// { email: "hermoine@hogwarts.com", name: "Hermoine" } | ||
``` |
@@ -1,5 +0,5 @@ | ||
import { CookieAdapter } from "./cookie_adapter"; | ||
import cookies, { CookieAttributes } from "js-cookie"; | ||
export abstract class AbstractStorage { | ||
abstract readonly adapter: Storage | CookieAdapter; | ||
abstract readonly adapter: Storage; | ||
@@ -13,28 +13,10 @@ readonly storagePrefix: string; | ||
get<T>(key: string): T | null { | ||
const item = this.adapter.getItem(this.storagePrefix + key); | ||
if (!item || item === "null") { | ||
return null; | ||
} | ||
try { | ||
return JSON.parse(item); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
return null; | ||
return fromStore<T>(this.adapter.getItem(this.storagePrefix + key)); | ||
} | ||
set(key: string, value?: any): boolean { | ||
if (value === undefined) { | ||
value = null; | ||
} else { | ||
value = JSON.stringify(value); | ||
} | ||
set(key: string, value?: unknown): boolean { | ||
try { | ||
this.adapter.setItem(this.storagePrefix + key, value); | ||
this.adapter.setItem(this.storagePrefix + key, toStore(value)); | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
@@ -61,5 +43,32 @@ return false; | ||
export class CookieStorage extends AbstractStorage { | ||
readonly adapter = new CookieAdapter(); | ||
export class CookieStorage { | ||
get<T>(key: string): T | null { | ||
return fromStore(cookies.get(key)); | ||
} | ||
remove(key: string, options?: CookieAttributes): void { | ||
cookies.remove(key, options); | ||
} | ||
set(key: string, value: string, options?: CookieAttributes): void { | ||
cookies.set(key, toStore(value), options); | ||
} | ||
} | ||
function toStore(value: unknown): string { | ||
if (value === undefined) value = null; | ||
return JSON.stringify(value); | ||
} | ||
function fromStore<T>(item: unknown): T | null { | ||
if (typeof item !== "string" || item === "null") { | ||
return null; | ||
} | ||
try { | ||
return JSON.parse(item); | ||
} catch (e) { | ||
} | ||
return null; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38903
68
8
218