@jmondi/browser-storage
Advanced tools
Comparing version 1.8.1 to 1.8.2
@@ -39,2 +39,6 @@ export type Serializer = { | ||
}; | ||
/** | ||
* Abstract base class for browser storage implementations. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
export declare abstract class AbstractBrowserStorage<SetConfig = unknown> { | ||
@@ -47,2 +51,6 @@ abstract adapter: Adapter<SetConfig> | AsyncAdapter<SetConfig>; | ||
} | ||
/** | ||
* Synchronous browser storage class. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
export declare class BrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> { | ||
@@ -60,2 +68,6 @@ readonly adapter: Adapter<SetConfig>; | ||
} | ||
/** | ||
* Asynchronous browser storage class with in memory cached storage. | ||
* @template SetConfig - Optional configuration object for the setItem method. | ||
*/ | ||
export declare class AsyncBrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> { | ||
@@ -78,8 +90,17 @@ readonly adapter: AsyncAdapter<SetConfig>; | ||
} | ||
/** | ||
* Local storage class that extends BrowserStorage. | ||
* Uses localStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
export declare class LocalStorage extends BrowserStorage { | ||
constructor(config?: Omit<StorageConfig, "adapter">); | ||
} | ||
/** | ||
* Session storage class that extends BrowserStorage. | ||
* Uses sessionStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
export declare class SessionStorage extends BrowserStorage { | ||
constructor(config?: Omit<StorageConfig, "adapter">); | ||
} | ||
/** In-memory storage adapter implementing the Adapter interface. */ | ||
export declare class MemoryStorageAdapter implements Adapter { | ||
@@ -93,11 +114,1 @@ private storage; | ||
} | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
export declare class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor(); | ||
} | ||
/** | ||
* @deprecated use Serializer instead | ||
*/ | ||
export type StorageSerializer = Serializer; |
@@ -0,1 +1,5 @@ | ||
/** | ||
* Abstract base class for browser storage implementations. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
export class AbstractBrowserStorage { | ||
@@ -25,2 +29,6 @@ toStore(value) { | ||
} | ||
/** | ||
* Synchronous browser storage class. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
export class BrowserStorage extends AbstractBrowserStorage { | ||
@@ -85,2 +93,6 @@ constructor(config = {}) { | ||
} | ||
/** | ||
* Asynchronous browser storage class with in memory cached storage. | ||
* @template SetConfig - Optional configuration object for the setItem method. | ||
*/ | ||
export class AsyncBrowserStorage extends AbstractBrowserStorage { | ||
@@ -166,2 +178,6 @@ constructor(config) { | ||
} | ||
/** | ||
* Local storage class that extends BrowserStorage. | ||
* Uses localStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
export class LocalStorage extends BrowserStorage { | ||
@@ -179,2 +195,6 @@ constructor(config = {}) { | ||
} | ||
/** | ||
* Session storage class that extends BrowserStorage. | ||
* Uses sessionStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
export class SessionStorage extends BrowserStorage { | ||
@@ -192,2 +212,3 @@ constructor(config = {}) { | ||
} | ||
/** In-memory storage adapter implementing the Adapter interface. */ | ||
export class MemoryStorageAdapter { | ||
@@ -218,10 +239,1 @@ constructor() { | ||
} | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
export class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor() { | ||
super(); | ||
console.log("[@jmondi/browser-storage] MemoryStorageProvider is deprecated, use MemoryStorageAdapter instead"); | ||
} | ||
} |
@@ -5,4 +5,7 @@ { | ||
"name": "@jmondi/browser-storage", | ||
"version": "1.8.1", | ||
"version": "1.8.2", | ||
"description": "Utilities for local and session browser storage.", | ||
"scripts": { | ||
"postinstall": "node npm-deprecated.js" | ||
}, | ||
"keywords": [ | ||
@@ -38,2 +41,6 @@ "browser-storage", | ||
"require": "./script/mod.js" | ||
}, | ||
"./npm-deprecated.js": { | ||
"import": "./esm/npm-deprecated.js", | ||
"require": "./script/npm-deprecated.js" | ||
} | ||
@@ -40,0 +47,0 @@ }, |
138
README.md
# @jmondi/browser-storage | ||
An abstracted storage library for **browser** applications that interfaces with localStorage, sessionStorage, in-memory storage, or any custom serializer. It provides serialization capabilities with optional key prefixing for better storage management. | ||
[![Deno Version](https://shield.deno.dev/x/browser_storage?style=flat-square)](https://deno.land/x/browser_storage) | ||
[![Npmjs.org Version](https://img.shields.io/npm/v/@jmondi/browser-storage?style=flat-square)](https://www.npmjs.com/package/@jmondi/browser-storage) | ||
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/jasonraimondi/browser-storage/test.yml?branch=main&label=Unit%20Tests&style=flat-square)](https://github.com/jasonraimondi/browser-storage) | ||
[![Test Coverage](https://img.shields.io/codeclimate/coverage/jasonraimondi/browser-storage?style=flat-square)](https://codeclimate.com/github/jasonraimondi/browser-storage/test_coverage) | ||
[![NPM Downloads](https://img.shields.io/npm/dt/@jmondi/browser-storage?label=npm%20downloads&style=flat-square)](https://www.npmjs.com/package/@jmondi/browser-storage) | ||
![npm bundle size](https://img.shields.io/bundlephobia/min/%40jmondi%2Fbrowser-storage) | ||
![npm gzip bundle size](https://img.shields.io/bundlephobia/minzip/%40jmondi%2Fbrowser-storage) | ||
## Installation | ||
```bash | ||
pnpm add @jmondi/browser-storage | ||
``` | ||
For Deno: | ||
```ts | ||
import { LocaleStorage, SessionStorage, BrowserStorage } from "https://deno.land/x/browser_storage" | ||
``` | ||
## Usage | ||
`LocalStorage` and `SessionStorage` are wrappers for `window.localStorage` and `window.sessionStorage`. | ||
### The LocalStorage class | ||
Persists after closing browser | ||
```typescript | ||
import { LocalStorage } from "@jmondi/browser-storage"; | ||
const storage = new LocalStorage(); | ||
storage.set("user2", { email: "hermoine@hogwarts.com" }); | ||
console.log(storage.get("user2")); | ||
// { email: "hermoine@hogwarts.com" } | ||
``` | ||
### The SessionStorage class | ||
Resets on browser close | ||
```typescript | ||
import { SessionStorage } from "@jmondi/browser-storage"; | ||
const storage = new SessionStorage(); | ||
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" }); | ||
console.log(storage.get("user2")); // { email: "hermoine@hogwarts.com", name: "Hermoine" } | ||
``` | ||
### Example Custom storage adapter | ||
```ts | ||
import { type Adapter, BrowserStorage } from "@jmondi/browser-storage"; | ||
import Cookies, { type CookieAttributes } from "js-cookie"; | ||
export class CookieAdapter implements Adapter { | ||
getItem(key: string): string | null { | ||
return Cookies.get(key) ?? null; | ||
} | ||
removeItem(key: string): void { | ||
Cookies.remove(key); | ||
} | ||
setItem(key: string, value: string, config: CookieAttributes): void { | ||
Cookies.set(key, value, config); | ||
} | ||
} | ||
const storage = new BrowserStorage<CookieAttributes>({ prefix: "app_", adapter: new CookieAdapter() }); | ||
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" }, { expires: 5 }); | ||
console.log(storage.get("user2")); | ||
``` | ||
## Configuration | ||
Optional settings: `prefix` (key prefix), `serializer` (defaults to `JSON`). | ||
```ts | ||
import { BrowserStorage } from "./index.ts"; | ||
const storage = new LocalStorage({ prefix: 'app_', serializer: JSON }); | ||
``` | ||
## Custom Serializers | ||
To create a custom serializer, implement `parse` and `stringify`. | ||
```ts | ||
import superjson from "superjson"; | ||
import { StorageSerializer } from "@jmondi/browser-storage"; | ||
export class SuperJsonSerializer implements StorageSerializer { | ||
parse<T = unknown>(value: string): T { | ||
return superjson.parse(value); | ||
} | ||
stringify<T = unknown>(value: T): string { | ||
return superjson.stringify(value); | ||
} | ||
} | ||
``` | ||
## Advanced Usage / Organization Techniques | ||
With defined storage groups you can organize related data into named groups, simplifying access and management. | ||
### Defining Storage Key | ||
Use `define` for individual keys. In this example, | ||
```typescript | ||
type UserInfo = { email: string }; | ||
const storage = new LocalStorage(); // define is available on all BrowserStorage implementations | ||
const USER_INFO = storage.define<UserInfo>("user_info"); | ||
USER_INFO.set({ email: "jason@example.com" }); | ||
USER_INFO.get(); // gets the latest value | ||
USER_INFO.remove(); // removes the value | ||
``` | ||
### Defining Storage Groups | ||
Use `defineGroup` for key groups. | ||
```typescript | ||
const storage = new LocalStorage(); // defineGroup is available on all BrowserStorage implementations | ||
const GROUP = storage.defineGroup({ token: "refresh_token", user: "user_info" }); | ||
GROUP.token.set("newtoken"); | ||
GROUP.token.get(); | ||
GROUP.token.remove(); | ||
GROUP.user.set({ email: "jason@example.com" }); | ||
GROUP.user.get(); | ||
GROUP.user.remove(); | ||
``` | ||
This package is deprecated, please use https://jsr.io/@jmondi/browser-storage instead. |
@@ -39,2 +39,6 @@ export type Serializer = { | ||
}; | ||
/** | ||
* Abstract base class for browser storage implementations. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
export declare abstract class AbstractBrowserStorage<SetConfig = unknown> { | ||
@@ -47,2 +51,6 @@ abstract adapter: Adapter<SetConfig> | AsyncAdapter<SetConfig>; | ||
} | ||
/** | ||
* Synchronous browser storage class. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
export declare class BrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> { | ||
@@ -60,2 +68,6 @@ readonly adapter: Adapter<SetConfig>; | ||
} | ||
/** | ||
* Asynchronous browser storage class with in memory cached storage. | ||
* @template SetConfig - Optional configuration object for the setItem method. | ||
*/ | ||
export declare class AsyncBrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> { | ||
@@ -78,8 +90,17 @@ readonly adapter: AsyncAdapter<SetConfig>; | ||
} | ||
/** | ||
* Local storage class that extends BrowserStorage. | ||
* Uses localStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
export declare class LocalStorage extends BrowserStorage { | ||
constructor(config?: Omit<StorageConfig, "adapter">); | ||
} | ||
/** | ||
* Session storage class that extends BrowserStorage. | ||
* Uses sessionStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
export declare class SessionStorage extends BrowserStorage { | ||
constructor(config?: Omit<StorageConfig, "adapter">); | ||
} | ||
/** In-memory storage adapter implementing the Adapter interface. */ | ||
export declare class MemoryStorageAdapter implements Adapter { | ||
@@ -93,11 +114,1 @@ private storage; | ||
} | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
export declare class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor(); | ||
} | ||
/** | ||
* @deprecated use Serializer instead | ||
*/ | ||
export type StorageSerializer = Serializer; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MemoryStorageProvider = exports.MemoryStorageAdapter = exports.SessionStorage = exports.LocalStorage = exports.AsyncBrowserStorage = exports.BrowserStorage = exports.AbstractBrowserStorage = void 0; | ||
exports.MemoryStorageAdapter = exports.SessionStorage = exports.LocalStorage = exports.AsyncBrowserStorage = exports.BrowserStorage = exports.AbstractBrowserStorage = void 0; | ||
/** | ||
* Abstract base class for browser storage implementations. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
class AbstractBrowserStorage { | ||
@@ -29,2 +33,6 @@ toStore(value) { | ||
exports.AbstractBrowserStorage = AbstractBrowserStorage; | ||
/** | ||
* Synchronous browser storage class. | ||
* @template SetConfig - Optional configuration type for the setItem method. | ||
*/ | ||
class BrowserStorage extends AbstractBrowserStorage { | ||
@@ -90,2 +98,6 @@ constructor(config = {}) { | ||
exports.BrowserStorage = BrowserStorage; | ||
/** | ||
* Asynchronous browser storage class with in memory cached storage. | ||
* @template SetConfig - Optional configuration object for the setItem method. | ||
*/ | ||
class AsyncBrowserStorage extends AbstractBrowserStorage { | ||
@@ -172,2 +184,6 @@ constructor(config) { | ||
exports.AsyncBrowserStorage = AsyncBrowserStorage; | ||
/** | ||
* Local storage class that extends BrowserStorage. | ||
* Uses localStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
class LocalStorage extends BrowserStorage { | ||
@@ -186,2 +202,6 @@ constructor(config = {}) { | ||
exports.LocalStorage = LocalStorage; | ||
/** | ||
* Session storage class that extends BrowserStorage. | ||
* Uses sessionStorage if available, otherwise falls back to in-memory storage. | ||
*/ | ||
class SessionStorage extends BrowserStorage { | ||
@@ -200,2 +220,3 @@ constructor(config = {}) { | ||
exports.SessionStorage = SessionStorage; | ||
/** In-memory storage adapter implementing the Adapter interface. */ | ||
class MemoryStorageAdapter { | ||
@@ -227,11 +248,1 @@ constructor() { | ||
exports.MemoryStorageAdapter = MemoryStorageAdapter; | ||
/** | ||
* @deprecated use MemoryStorageAdapter instead | ||
*/ | ||
class MemoryStorageProvider extends MemoryStorageAdapter { | ||
constructor() { | ||
super(); | ||
console.log("[@jmondi/browser-storage] 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
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
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
18
720
26593
4
1