@jmondi/browser-storage
Advanced tools
Comparing version
@@ -18,2 +18,4 @@ import { CookieAttributes } from "js-cookie"; | ||
export declare class CookieStorage { | ||
readonly storagePrefix: string; | ||
constructor(storagePrefix?: string); | ||
get<T>(key: string): T | null; | ||
@@ -20,0 +22,0 @@ remove(key: string, options?: CookieAttributes): void; |
@@ -10,3 +10,3 @@ "use strict"; | ||
storagePrefix; | ||
constructor(storagePrefix = "@jmondi:") { | ||
constructor(storagePrefix = "") { | ||
this.storagePrefix = storagePrefix; | ||
@@ -43,10 +43,14 @@ } | ||
class CookieStorage { | ||
storagePrefix; | ||
constructor(storagePrefix = "") { | ||
this.storagePrefix = storagePrefix; | ||
} | ||
get(key) { | ||
return fromStore(js_cookie_1.default.get(key)); | ||
return fromStore(js_cookie_1.default.get(this.storagePrefix + key)); | ||
} | ||
remove(key, options) { | ||
js_cookie_1.default.remove(key, options); | ||
js_cookie_1.default.remove(this.storagePrefix + key, options); | ||
} | ||
set(key, value, options) { | ||
js_cookie_1.default.set(key, toStore(value), options); | ||
js_cookie_1.default.set(this.storagePrefix + key, toStore(value), options); | ||
} | ||
@@ -53,0 +57,0 @@ } |
{ | ||
"name": "@jmondi/browser-storage", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"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```\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```" | ||
"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\nCookie storage is more configurable, see https://github.com/js-cookie/js-cookie#cookie-attributes for full options\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```\n" | ||
} |
@@ -51,3 +51,3 @@ # @jmondi/browser-storage | ||
Session storage is reset when the browser is closed. | ||
Cookie storage is more configurable, see https://github.com/js-cookie/js-cookie#cookie-attributes for full options | ||
@@ -69,2 +69,2 @@ ```typescript | ||
// { email: "hermoine@hogwarts.com", name: "Hermoine" } | ||
``` | ||
``` |
@@ -8,3 +8,3 @@ import cookies, { CookieAttributes } from "js-cookie"; | ||
constructor(storagePrefix: string = "@jmondi:") { | ||
constructor(storagePrefix: string = "") { | ||
this.storagePrefix = storagePrefix; | ||
@@ -44,12 +44,18 @@ } | ||
export class CookieStorage { | ||
readonly storagePrefix: string; | ||
constructor(storagePrefix: string = "") { | ||
this.storagePrefix = storagePrefix; | ||
} | ||
get<T>(key: string): T | null { | ||
return fromStore(cookies.get(key)); | ||
return fromStore(cookies.get(this.storagePrefix + key)); | ||
} | ||
remove(key: string, options?: CookieAttributes): void { | ||
cookies.remove(key, options); | ||
cookies.remove(this.storagePrefix + key, options); | ||
} | ||
set(key: string, value: string, options?: CookieAttributes): void { | ||
cookies.set(key, toStore(value), options); | ||
cookies.set(this.storagePrefix + key, toStore(value), options); | ||
} | ||
@@ -56,0 +62,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39594
1.78%228
4.59%69
1.47%