@jmondi/browser-storage
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -30,2 +30,12 @@ export type Serializer = { | ||
remove(key: string): void; | ||
defineGroup<GenericRecord extends Record<string, string>>(group: GenericRecord): Record<keyof GenericRecord, { | ||
get: () => unknown; | ||
set: (value: unknown, innerConfig?: unknown) => boolean; | ||
remove: () => void; | ||
}>; | ||
define<T>(key: string, config?: unknown): { | ||
get: () => T | null; | ||
set: (value: unknown, innerConfig?: unknown) => boolean; | ||
remove: () => void; | ||
}; | ||
private toStore; | ||
@@ -32,0 +42,0 @@ private fromStore; |
@@ -42,2 +42,17 @@ export class BrowserStorage { | ||
} | ||
defineGroup(group) { | ||
return Object | ||
.keys(group) | ||
.reduce((prev, next) => ({ | ||
...prev, | ||
[next]: this.define(group[next]), | ||
}), {}); | ||
} | ||
define(key, config) { | ||
return { | ||
get: () => this.get(key), | ||
set: (value, innerConfig) => this.set(key, value, innerConfig ?? config), | ||
remove: () => this.remove(key), | ||
}; | ||
} | ||
toStore(value) { | ||
@@ -61,3 +76,4 @@ switch (typeof value) { | ||
} | ||
catch { } | ||
catch (e) { | ||
} | ||
return item ?? null; | ||
@@ -64,0 +80,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"name": "@jmondi/browser-storage", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Utilities for local and session browser storage.", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
111
README.md
@@ -31,3 +31,3 @@ # @jmondi/browser-storage | ||
### Local Storage | ||
### Local Storage Adapter | ||
@@ -41,7 +41,3 @@ Local storage is persistent after close. | ||
storage.set("user1", null); | ||
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" }); | ||
console.log(storage.get("user1")); | ||
// null | ||
console.log(storage.get("user2")); | ||
@@ -51,3 +47,3 @@ // { email: "hermoine@hogwarts.com", name: "Hermoine" } | ||
### Session Storage | ||
### Session Storage Adapter | ||
@@ -61,7 +57,3 @@ Session storage is reset when the browser is closed. | ||
storage.set("user1", null); | ||
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" }); | ||
console.log(storage.get("user1")); | ||
// null | ||
console.log(storage.get("user2")); | ||
@@ -71,30 +63,4 @@ // { email: "hermoine@hogwarts.com", name: "Hermoine" } | ||
### Custom Storage Adapter | ||
## Configuration | ||
You can optionally provide a configuration object. | ||
- `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. | ||
}); | ||
``` | ||
## Custom Storage Adapter | ||
The BrowserStorage class gives you the option to use a custom storage adapter. | ||
@@ -104,3 +70,3 @@ | ||
For a custom storage provider to work correctly, it needs to adhere to the browser's [Storage interface](https://developer.mozilla.org/en-US/docs/Web/API/Storage) – that is, it must implement methods such as `getItem`, `setItem`, `removeItem`, and `clear`, along with the `length` property. As an example, the provided `MemoryStorageProvider` class is a valid storage provider that stores data in an in-memory JavaScript map. | ||
For a custom storage provider to work correctly, it needs to implement the `Adapter` interface. | ||
@@ -127,5 +93,32 @@ ```ts | ||
export const cookieStorageService = new BrowserStorage({ prefix, adapter: new CookieAdapter() }); | ||
export const storage = new BrowserStorage({ prefix, adapter: new CookieAdapter() }); | ||
storage.set("user2", { email: "hermoine@hogwarts.com", name: "Hermoine" }, { expires: 5 }); | ||
console.log(storage.get("user2")); | ||
``` | ||
## Configuration | ||
You can optionally provide a configuration object. | ||
- `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. | ||
}); | ||
``` | ||
## Custom Serializers | ||
@@ -148,1 +141,41 @@ | ||
``` | ||
## 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" } | ||
``` |
@@ -30,2 +30,12 @@ export type Serializer = { | ||
remove(key: string): void; | ||
defineGroup<GenericRecord extends Record<string, string>>(group: GenericRecord): Record<keyof GenericRecord, { | ||
get: () => unknown; | ||
set: (value: unknown, innerConfig?: unknown) => boolean; | ||
remove: () => void; | ||
}>; | ||
define<T>(key: string, config?: unknown): { | ||
get: () => T | null; | ||
set: (value: unknown, innerConfig?: unknown) => boolean; | ||
remove: () => void; | ||
}; | ||
private toStore; | ||
@@ -32,0 +42,0 @@ private fromStore; |
@@ -45,2 +45,17 @@ "use strict"; | ||
} | ||
defineGroup(group) { | ||
return Object | ||
.keys(group) | ||
.reduce((prev, next) => ({ | ||
...prev, | ||
[next]: this.define(group[next]), | ||
}), {}); | ||
} | ||
define(key, config) { | ||
return { | ||
get: () => this.get(key), | ||
set: (value, innerConfig) => this.set(key, value, innerConfig ?? config), | ||
remove: () => this.remove(key), | ||
}; | ||
} | ||
toStore(value) { | ||
@@ -64,3 +79,4 @@ switch (typeof value) { | ||
} | ||
catch { } | ||
catch (e) { | ||
} | ||
return item ?? null; | ||
@@ -67,0 +83,0 @@ } |
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
19759
390
174