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.7.1 to 1.8.0-alpha.1

42

esm/index.d.ts

@@ -16,2 +16,9 @@ export type Serializer = {

};
export type AsyncAdapter<SetConfig = unknown> = {
clear?(): Promise<void>;
getItem(key: string): Promise<string | null>;
removeItem(key: string): Promise<void>;
setItem(key: string, value: string): Promise<void>;
setItem(key: string, value: string, config?: SetConfig): Promise<void>;
};
export type StorageConfig = {

@@ -22,2 +29,7 @@ prefix?: string;

};
export type AsyncStorageConfig = {
prefix?: string;
serializer?: Serializer;
adapter?: AsyncAdapter;
};
export type DefineResponse<SetConfig = unknown> = {

@@ -28,3 +40,15 @@ get<T = unknown>(): T | null;

};
export declare class BrowserStorage<SetConfig = unknown> {
export type AsyncDefineResponse<SetConfig = unknown> = {
get<T = unknown>(): Promise<T | null>;
set(value: unknown, config?: SetConfig): Promise<boolean>;
remove(): Promise<void>;
};
export declare abstract class AbstractBrowserStorage<SetConfig = unknown> {
abstract adapter: Adapter<SetConfig> | AsyncAdapter<SetConfig>;
abstract prefix: string;
abstract serializer: Serializer;
protected toStore(value: unknown): string;
protected fromStore<T = unknown>(item: unknown): T | null;
}
export declare class BrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> {
readonly adapter: Adapter<SetConfig>;

@@ -40,5 +64,17 @@ readonly prefix: string;

define<DefinedType>(key: string, defaultConfig?: SetConfig): DefineResponse<SetConfig>;
private toStore;
private fromStore;
}
export declare class AsyncBrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> {
readonly adapter: AsyncAdapter<SetConfig>;
readonly prefix: string;
readonly serializer: Serializer;
constructor(config: StorageConfig & {
adapter: AsyncAdapter<SetConfig>;
});
clear(): void;
get<T>(key: string): Promise<T | null>;
set(key: string, value?: unknown, config?: SetConfig): Promise<boolean>;
remove(key: string): Promise<void>;
defineGroup<GenericRecord extends Record<string, string>>(group: GenericRecord): Record<keyof GenericRecord, AsyncDefineResponse<SetConfig>>;
define<DefinedType>(key: string, defaultConfig?: SetConfig): AsyncDefineResponse<SetConfig>;
}
export declare class LocalStorage extends BrowserStorage {

@@ -45,0 +81,0 @@ constructor(config?: Omit<StorageConfig, "adapter">);

@@ -1,3 +0,28 @@

export class BrowserStorage {
export class AbstractBrowserStorage {
toStore(value) {
switch (typeof value) {
case "string":
return value;
case "undefined":
return this.serializer.stringify(null);
default:
return this.serializer.stringify(value);
}
}
fromStore(item) {
if (item === "null")
return null;
if (typeof item !== "string")
return null;
try {
return this.serializer.parse(item);
}
catch (e) {
}
return item ?? null;
}
}
export class BrowserStorage extends AbstractBrowserStorage {
constructor(config = {}) {
super();
Object.defineProperty(this, "adapter", {

@@ -58,24 +83,61 @@ enumerable: true,

}
toStore(value) {
switch (typeof value) {
case "string":
return value;
case "undefined":
return this.serializer.stringify(null);
default:
return this.serializer.stringify(value);
}
}
export class AsyncBrowserStorage extends AbstractBrowserStorage {
constructor(config) {
super();
Object.defineProperty(this, "adapter", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "prefix", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "serializer", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.adapter = config.adapter;
this.prefix = config.prefix ?? "";
this.serializer = config.serializer ?? JSON;
}
fromStore(item) {
if (item === "null")
return null;
if (typeof item !== "string")
return null;
clear() {
this.adapter.clear?.();
}
async get(key) {
return this.fromStore(await this.adapter.getItem(this.prefix + key));
}
async set(key, value, config) {
try {
return this.serializer.parse(item);
await this.adapter.setItem(this.prefix + key, this.toStore(value), config);
return true;
}
catch (e) {
catch {
}
return item ?? null;
return false;
}
async remove(key) {
await this.adapter.removeItem(this.prefix + key);
}
defineGroup(group) {
return Object
.keys(group)
.reduce((prev, next) => ({
...prev,
[next]: this.define(group[next]),
}), {});
}
define(key, defaultConfig) {
return {
get: () => this.get(key),
set: (value, config) => this.set(key, value, config ?? defaultConfig),
remove: () => this.remove(key),
};
}
}

@@ -82,0 +144,0 @@ export class LocalStorage extends BrowserStorage {

2

package.json

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

"name": "@jmondi/browser-storage",
"version": "1.7.1",
"version": "1.8.0-alpha.1",
"description": "Utilities for local and session browser storage.",

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

@@ -16,2 +16,9 @@ export type Serializer = {

};
export type AsyncAdapter<SetConfig = unknown> = {
clear?(): Promise<void>;
getItem(key: string): Promise<string | null>;
removeItem(key: string): Promise<void>;
setItem(key: string, value: string): Promise<void>;
setItem(key: string, value: string, config?: SetConfig): Promise<void>;
};
export type StorageConfig = {

@@ -22,2 +29,7 @@ prefix?: string;

};
export type AsyncStorageConfig = {
prefix?: string;
serializer?: Serializer;
adapter?: AsyncAdapter;
};
export type DefineResponse<SetConfig = unknown> = {

@@ -28,3 +40,15 @@ get<T = unknown>(): T | null;

};
export declare class BrowserStorage<SetConfig = unknown> {
export type AsyncDefineResponse<SetConfig = unknown> = {
get<T = unknown>(): Promise<T | null>;
set(value: unknown, config?: SetConfig): Promise<boolean>;
remove(): Promise<void>;
};
export declare abstract class AbstractBrowserStorage<SetConfig = unknown> {
abstract adapter: Adapter<SetConfig> | AsyncAdapter<SetConfig>;
abstract prefix: string;
abstract serializer: Serializer;
protected toStore(value: unknown): string;
protected fromStore<T = unknown>(item: unknown): T | null;
}
export declare class BrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> {
readonly adapter: Adapter<SetConfig>;

@@ -40,5 +64,17 @@ readonly prefix: string;

define<DefinedType>(key: string, defaultConfig?: SetConfig): DefineResponse<SetConfig>;
private toStore;
private fromStore;
}
export declare class AsyncBrowserStorage<SetConfig = unknown> extends AbstractBrowserStorage<SetConfig> {
readonly adapter: AsyncAdapter<SetConfig>;
readonly prefix: string;
readonly serializer: Serializer;
constructor(config: StorageConfig & {
adapter: AsyncAdapter<SetConfig>;
});
clear(): void;
get<T>(key: string): Promise<T | null>;
set(key: string, value?: unknown, config?: SetConfig): Promise<boolean>;
remove(key: string): Promise<void>;
defineGroup<GenericRecord extends Record<string, string>>(group: GenericRecord): Record<keyof GenericRecord, AsyncDefineResponse<SetConfig>>;
define<DefinedType>(key: string, defaultConfig?: SetConfig): AsyncDefineResponse<SetConfig>;
}
export declare class LocalStorage extends BrowserStorage {

@@ -45,0 +81,0 @@ constructor(config?: Omit<StorageConfig, "adapter">);

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryStorageProvider = exports.MemoryStorageAdapter = exports.SessionStorage = exports.LocalStorage = exports.BrowserStorage = void 0;
class BrowserStorage {
exports.MemoryStorageProvider = exports.MemoryStorageAdapter = exports.SessionStorage = exports.LocalStorage = exports.AsyncBrowserStorage = exports.BrowserStorage = exports.AbstractBrowserStorage = void 0;
class AbstractBrowserStorage {
toStore(value) {
switch (typeof value) {
case "string":
return value;
case "undefined":
return this.serializer.stringify(null);
default:
return this.serializer.stringify(value);
}
}
fromStore(item) {
if (item === "null")
return null;
if (typeof item !== "string")
return null;
try {
return this.serializer.parse(item);
}
catch (e) {
}
return item ?? null;
}
}
exports.AbstractBrowserStorage = AbstractBrowserStorage;
class BrowserStorage extends AbstractBrowserStorage {
constructor(config = {}) {
super();
Object.defineProperty(this, "adapter", {

@@ -61,26 +87,64 @@ enumerable: true,

}
toStore(value) {
switch (typeof value) {
case "string":
return value;
case "undefined":
return this.serializer.stringify(null);
default:
return this.serializer.stringify(value);
}
}
exports.BrowserStorage = BrowserStorage;
class AsyncBrowserStorage extends AbstractBrowserStorage {
constructor(config) {
super();
Object.defineProperty(this, "adapter", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "prefix", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "serializer", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.adapter = config.adapter;
this.prefix = config.prefix ?? "";
this.serializer = config.serializer ?? JSON;
}
fromStore(item) {
if (item === "null")
return null;
if (typeof item !== "string")
return null;
clear() {
this.adapter.clear?.();
}
async get(key) {
return this.fromStore(await this.adapter.getItem(this.prefix + key));
}
async set(key, value, config) {
try {
return this.serializer.parse(item);
await this.adapter.setItem(this.prefix + key, this.toStore(value), config);
return true;
}
catch (e) {
catch {
}
return item ?? null;
return false;
}
async remove(key) {
await this.adapter.removeItem(this.prefix + key);
}
defineGroup(group) {
return Object
.keys(group)
.reduce((prev, next) => ({
...prev,
[next]: this.define(group[next]),
}), {});
}
define(key, defaultConfig) {
return {
get: () => this.get(key),
set: (value, config) => this.set(key, value, config ?? defaultConfig),
remove: () => this.remove(key),
};
}
}
exports.BrowserStorage = BrowserStorage;
exports.AsyncBrowserStorage = AsyncBrowserStorage;
class LocalStorage extends BrowserStorage {

@@ -87,0 +151,0 @@ constructor(config = {}) {

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