Skip to content

This is a library to save key-value pairs simultaneously to one or multiple storages (e.g. SessionStorage, LocalStorage, Cookie, ...).

License

Notifications You must be signed in to change notification settings

tada5hi/browser-storage-adapter

Repository files navigation

npm version CI

Browser Storage Adapter 💾

This is a library to save key-value pairs to one or multiple storages (e.g. SessionStorage, LocalStorage, Cookie, ...).

Table of Contents

Installation

npm install browser-storage-adapter --save

Important NOTE

The README.md file is under construction ☂ at the moment. So please stay patient or contribute to it, till it covers all parts ⭐.


Usage

Create an instance of the Adapter and specify the drivers, which should be enabled. By default, all drivers are disabled, expect of the in-memory driver, which is always enabled.

Besides, it is also possible to specify a custom namespace, so different instances of the Adapter don't interfere with each other, when they use the same key.

import {
    Adapter,
    AdapterOptions
} from "browser-storage-adapter";

const options: AdapterOptions = {
    driver: {
        cookie: {
            path: '/',
        },
        localStorage: true,
        sessionStorage: true,
    },
    namespace: 'auth'
};

const warehouse = new Adapter(options);

// Set a key-value pair for the following drivers:
// cookie, localStorage & sessionStorage.
warehouse.set('token', 'xxx');

// Ensure that value is still the same for all drivers.
warehouse.sync('token', 'xxx');

// Get a key-value pair.
const token = warehouse.get('token');
console.log(token);
// xxx

About

This is a library to save key-value pairs simultaneously to one or multiple storages (e.g. SessionStorage, LocalStorage, Cookie, ...).

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •