Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jmondi/browser-storage

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jmondi/browser-storage

Utilities for local and session browser storage.

  • 1.8.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

@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 Npmjs.org Version GitHub Workflow Status Test Coverage NPM Downloads npm bundle size npm gzip bundle size

Installation

pnpm add @jmondi/browser-storage

For Deno:

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

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

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

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).

import { BrowserStorage } from "./index.ts";

const storage = new LocalStorage({ prefix: 'app_', serializer: JSON });

Custom Serializers

To create a custom serializer, implement parse and stringify.

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,

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.

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();

Keywords

FAQs

Package last updated on 23 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc