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 - npm Package Compare versions

Comparing version 1.6.0 to 1.6.1

2

package.json

@@ -5,3 +5,3 @@ {

"name": "@jmondi/browser-storage",
"version": "1.6.0",
"version": "1.6.1",
"description": "Utilities for local and session browser storage.",

@@ -8,0 +8,0 @@ "keywords": [

@@ -10,5 +10,8 @@ # @jmondi/browser-storage

[![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 bundle size](https://img.shields.io/bundlephobia/minzip/%40jmondi%2Fbrowser-storage)
## Install (npm)
## Installation
```bash

@@ -18,10 +21,5 @@ pnpm add @jmondi/browser-storage

### Deno
For Deno:
```ts
import {
LocaleStorage,
SessionStorage,
BrowserStorage
} from "https://deno.land/x/browser_storage"
import { LocaleStorage, SessionStorage, BrowserStorage } from "https://deno.land/x/browser_storage"
```

@@ -31,7 +29,7 @@

The `LocalStorage` and `SessionStorage` classes serve as helper abstractions over the built-in `window.localStorage` and `window.sessionStorage` web storage mechanisms respectively.
`LocalStorage` and `SessionStorage` are wrappers for `window.localStorage` and `window.sessionStorage`.
### Local Storage Adapter
### LocalStorage
Local storage is persistent after close.
Persists after closing browser:

@@ -42,11 +40,9 @@ ```typescript

const storage = new LocalStorage();
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" });
console.log(storage.get("user2"));
// { email: "hermoine@hogwarts.com", name: "Hermoine" }
console.log(storage.get("user2")); // { email: "hermoine@hogwarts.com", name: "Hermoine" }
```
### Session Storage Adapter
### SessionStorage
Session storage is reset when the browser is closed.
Resets on browser close:

@@ -57,16 +53,8 @@ ```typescript

const storage = new SessionStorage();
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" });
console.log(storage.get("user2"));
// { email: "hermoine@hogwarts.com", name: "Hermoine" }
console.log(storage.get("user2")); // { email: "hermoine@hogwarts.com", name: "Hermoine" }
```
### Custom Storage Adapter
### Custom storage adapter:
The BrowserStorage class gives you the option to use a custom storage adapter.
Underneath, both `LocalStorage` and `SessionStorage` extend the `BrowserStorage` class, which operates over an arbitrary storage adapter. This design enables you to extend `BrowserStorage` to interface with any custom storage provider of your choice.
For a custom storage provider to work correctly, it needs to implement the `Adapter` interface.
```ts

@@ -77,18 +65,8 @@ import { type Adapter, BrowserStorage } from "@jmondi/browser-storage";

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);
}
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 prefix = "app_"
export const storage = new BrowserStorage({ prefix, adapter: new CookieAdapter() });
const storage = new BrowserStorage({ prefix: "app_", adapter: new CookieAdapter() });
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" }, { expires: 5 });

@@ -98,25 +76,30 @@ console.log(storage.get("user2"));

## Defining Storage Groups
- Use `define` for individual keys:
```typescript
const storage = new BrowserStorage();
const USER_COOKIE = storage.define<{ email: string }>("user_info");
USER_COOKIE.set({ email: "jason@example.com" });
```
- Use `defineGroup` for key groups:
```typescript
const storage = new BrowserStorage();
const GROUP = storage.defineGroup({ token: "refresh_token", user: "user_info" });
GROUP.token.set("newtoken");
GROUP.user.set({ email: "jason@example.com" });
```
## Configuration
You can optionally provide a configuration object.
Optional settings: `prefix` (key prefix), `serializer` (defaults to `JSON`).
- `prefix`: This optional value will be prepended to every key when stored.
- `serializer`: This optional value can be any object that implements the `StorageSerializer` interface. By default, this is `JSON`.
```ts
import { BrowserStorage } from "./index.ts";
const localStorage = new LocalStorage({
prefix: 'app_', // Optional. Defaults to "".
serializer: JSON, // Optional. Defaults to JSON.
});
const sessionStorage = new SessionStorage({
prefix: 'app_', // Optional. Defaults to "".
serializer: JSON, // Optional. Defaults to JSON.
});
const browserStorage = new BrowserStorage({
prefix: 'app_', // Optional. Defaults to "".
serializer: JSON, // Optional. Defaults to JSON.
adapter: Adapter, // Optional. Defaults to an InMemoryStorageProvider.
});
const storage = new LocalStorage({ prefix: 'app_', serializer: JSON });
```

@@ -126,3 +109,3 @@

The `StorageSerializer` is an interface which requires the implementation of two methods: `parse` and `stringify`. The default example of this is the built in [JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) object.
To create a custom serializer, implement `parse` and `stringify`.

@@ -134,49 +117,5 @@ ```ts

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);
}
parse<T = unknown>(value: string): T { return superjson.parse(value); }
stringify<T = unknown>(value: T): string { return superjson.stringify(value); }
}
```
## Defining a named group of keys
### The `define` method
This method allows the creation of named keys in storage. Each key is associated with a type. Here's an example:
```typescript
const storage = new BrowserStorage(); // or LocalStorage, SessionStorage, etc.
const GROUP = {
token: storage.define<string>("access_token"),
user: storage.define<{ email: string }>("user_info"),
};
GROUP.token.set("ABC123");
GROUP.user.set({ email: "jason@example.com" });
GROUP.token.get(); // "ABC123"
GROUP.user.get(); // { email: "jason@example" }
```
In this example, `GROUP` has two keys: `token` and `user`.
### The `defineGroup` method
The `defineGroup` method provides a more concise way to define named keys. Here's an example:
```typescript
const storage = new BrowserStorage(); // or LocalStorage, SessionStorage, etc.
const GROUP = storage.defineGroup({
token: "refresh_token",
user: "user_info",
});
GROUP.token.set("newtoken");
GROUP.user.set({ email: "jason@example.com" });
GROUP.token.get(); // "newtoken"
GROUP.user.get(); // { email: "jason@example" }
```
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