@jmondi/browser-storage
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -1,9 +0,19 @@ | ||
export type StorageSerializer = { | ||
export type Serializer = { | ||
parse<T = unknown>(value: string): T; | ||
stringify<T = unknown>(value: T): string; | ||
}; | ||
export type Adapter = Pick<Storage, "clear" | "getItem" | "removeItem" | "setItem">; | ||
/** | ||
* @deprecated use Serializer instead | ||
*/ | ||
export type StorageSerializer = Serializer; | ||
export type Adapter = { | ||
clear?(): void; | ||
getItem(key: string): string | null; | ||
removeItem(key: string): void; | ||
setItem(key: string, value: string): void; | ||
setItem(key: string, value: string, config?: unknown): void; | ||
}; | ||
export type StorageConfig = { | ||
prefix?: string; | ||
serializer?: StorageSerializer; | ||
serializer?: Serializer; | ||
adapter?: Adapter; | ||
@@ -14,8 +24,8 @@ }; | ||
readonly prefix: string; | ||
readonly serializer: StorageSerializer; | ||
readonly serializer: Serializer; | ||
constructor(config?: StorageConfig); | ||
clear(): void; | ||
get<T>(key: string): T | null; | ||
set(key: string, value?: unknown): boolean; | ||
set(key: string, value?: unknown, config?: unknown): boolean; | ||
remove(key: string): void; | ||
clear(): void; | ||
private toStore; | ||
@@ -30,3 +40,3 @@ private fromStore; | ||
} | ||
export declare class MemoryStorageProvider implements Adapter { | ||
export declare class MemoryStorageAdapter implements Adapter { | ||
private storage; | ||
@@ -38,1 +48,7 @@ clear(): void; | ||
} | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
export declare class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor(); | ||
} |
@@ -21,12 +21,15 @@ export class BrowserStorage { | ||
}); | ||
this.adapter = config.adapter ?? new MemoryStorageProvider(); | ||
this.adapter = config.adapter ?? new MemoryStorageAdapter(); | ||
this.prefix = config.prefix ?? ""; | ||
this.serializer = config.serializer ?? JSON; | ||
} | ||
clear() { | ||
this.adapter.clear?.(); | ||
} | ||
get(key) { | ||
return this.fromStore(this.adapter.getItem(this.prefix + key)); | ||
} | ||
set(key, value) { | ||
set(key, value, config) { | ||
try { | ||
this.adapter.setItem(this.prefix + key, this.toStore(value)); | ||
this.adapter.setItem(this.prefix + key, this.toStore(value), config); | ||
return true; | ||
@@ -40,5 +43,2 @@ } | ||
} | ||
clear() { | ||
this.adapter.clear(); | ||
} | ||
toStore(value) { | ||
@@ -62,4 +62,3 @@ switch (typeof value) { | ||
} | ||
catch (e) { | ||
} | ||
catch { } | ||
return item ?? null; | ||
@@ -78,3 +77,3 @@ } | ||
} | ||
export class MemoryStorageProvider { | ||
export class MemoryStorageAdapter { | ||
constructor() { | ||
@@ -101,1 +100,10 @@ Object.defineProperty(this, "storage", { | ||
} | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
export class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor() { | ||
super(); | ||
console.log("MemoryStorageProvider is deprecated, use MemoryStorageAdapter instead"); | ||
} | ||
} |
@@ -5,3 +5,3 @@ { | ||
"name": "@jmondi/browser-storage", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Utilities for local and session browser storage.", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -102,11 +102,6 @@ # @jmondi/browser-storage | ||
```ts | ||
import { type Adapter } from "@jmondi/browser-storage"; | ||
import Cookies from "js-cookie"; | ||
import { type Adapter, BrowserStorage } from "@jmondi/browser-storage"; | ||
import Cookies, { type CookieAttributes } from "js-cookie"; | ||
export class CookieStorage implements Adapter { | ||
clear(): void { | ||
Cookies.clear(); | ||
} | ||
export class CookieAdapter implements Adapter { | ||
getItem(key: string): string | null { | ||
@@ -120,6 +115,10 @@ return Cookies.get(key) ?? null; | ||
setItem(key: string, value: string): void { | ||
Cookies.set(key, value); | ||
setItem(key: string, value: string, config: CookieAttributes): void { | ||
Cookies.set(key, value, config); | ||
} | ||
} | ||
const prefix = "app_" | ||
export const cookieStorageService = new BrowserStorage({ prefix, adapter: new CookieAdapter() }); | ||
``` | ||
@@ -126,0 +125,0 @@ |
@@ -1,9 +0,19 @@ | ||
export type StorageSerializer = { | ||
export type Serializer = { | ||
parse<T = unknown>(value: string): T; | ||
stringify<T = unknown>(value: T): string; | ||
}; | ||
export type Adapter = Pick<Storage, "clear" | "getItem" | "removeItem" | "setItem">; | ||
/** | ||
* @deprecated use Serializer instead | ||
*/ | ||
export type StorageSerializer = Serializer; | ||
export type Adapter = { | ||
clear?(): void; | ||
getItem(key: string): string | null; | ||
removeItem(key: string): void; | ||
setItem(key: string, value: string): void; | ||
setItem(key: string, value: string, config?: unknown): void; | ||
}; | ||
export type StorageConfig = { | ||
prefix?: string; | ||
serializer?: StorageSerializer; | ||
serializer?: Serializer; | ||
adapter?: Adapter; | ||
@@ -14,8 +24,8 @@ }; | ||
readonly prefix: string; | ||
readonly serializer: StorageSerializer; | ||
readonly serializer: Serializer; | ||
constructor(config?: StorageConfig); | ||
clear(): void; | ||
get<T>(key: string): T | null; | ||
set(key: string, value?: unknown): boolean; | ||
set(key: string, value?: unknown, config?: unknown): boolean; | ||
remove(key: string): void; | ||
clear(): void; | ||
private toStore; | ||
@@ -30,3 +40,3 @@ private fromStore; | ||
} | ||
export declare class MemoryStorageProvider implements Adapter { | ||
export declare class MemoryStorageAdapter implements Adapter { | ||
private storage; | ||
@@ -38,1 +48,7 @@ clear(): void; | ||
} | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
export declare class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor(); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MemoryStorageProvider = exports.SessionStorage = exports.LocalStorage = exports.BrowserStorage = void 0; | ||
exports.MemoryStorageProvider = exports.MemoryStorageAdapter = exports.SessionStorage = exports.LocalStorage = exports.BrowserStorage = void 0; | ||
class BrowserStorage { | ||
@@ -24,12 +24,15 @@ constructor(config = {}) { | ||
}); | ||
this.adapter = config.adapter ?? new MemoryStorageProvider(); | ||
this.adapter = config.adapter ?? new MemoryStorageAdapter(); | ||
this.prefix = config.prefix ?? ""; | ||
this.serializer = config.serializer ?? JSON; | ||
} | ||
clear() { | ||
this.adapter.clear?.(); | ||
} | ||
get(key) { | ||
return this.fromStore(this.adapter.getItem(this.prefix + key)); | ||
} | ||
set(key, value) { | ||
set(key, value, config) { | ||
try { | ||
this.adapter.setItem(this.prefix + key, this.toStore(value)); | ||
this.adapter.setItem(this.prefix + key, this.toStore(value), config); | ||
return true; | ||
@@ -43,5 +46,2 @@ } | ||
} | ||
clear() { | ||
this.adapter.clear(); | ||
} | ||
toStore(value) { | ||
@@ -65,4 +65,3 @@ switch (typeof value) { | ||
} | ||
catch (e) { | ||
} | ||
catch { } | ||
return item ?? null; | ||
@@ -84,3 +83,3 @@ } | ||
exports.SessionStorage = SessionStorage; | ||
class MemoryStorageProvider { | ||
class MemoryStorageAdapter { | ||
constructor() { | ||
@@ -107,2 +106,12 @@ Object.defineProperty(this, "storage", { | ||
} | ||
exports.MemoryStorageAdapter = MemoryStorageAdapter; | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor() { | ||
super(); | ||
console.log("MemoryStorageProvider is deprecated, use MemoryStorageAdapter instead"); | ||
} | ||
} | ||
exports.MemoryStorageProvider = MemoryStorageProvider; |
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
17298
338
141