npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wbet/storage-api

v1.0.6

Published

Wrapper over the Chrome and Browser storage API

Downloads

4

Readme

StorageAPI

Wrapper over the Chrome and Browser storage API that aims to provide a straight forward approach of using the storage API.

For example chrome.storage.local.get on chrome uses a callback while on firefox returns a promise, this wrapper will return a promise for both of them.

How to use

Interfaces

All the available interfaces will be presented bellow in an explicit way.

export type BasicTypes = string | number | boolean | string[] | number[] | boolean[];
export type StorageAvailableTypes = BasicTypes | StorageObjectType | StorageObjectType[];

// Represents an object with random keys that can have as value a basic type, an object like itself or an array of objects like itself.
export interface StorageObjectType {
    [key: string]: BasicTypes | StorageObjectType | StorageObjectType[];
}

Enums

All the available enums will be presented bellow:

export const enum StorageType {
    Local = 'local',
    Sync = 'sync',
    Managed = 'managed'
}
// used as a normal object with properties
StorageType.Sync;
// or with just the string
('sync');

Available APIs

This set of functions aims to reduce the clutter in your code providing a straight forward approach of using the storage. All the functions will return a promise whenever possible.

Save to storage

Typescript

function saveManyToLocalStorage(data: StorageObjectType);
function saveManyToSyncStorage(data: StorageObjectType);
function saveToLocalStorage(key: string, data: StorageAvailableTypes);
function saveToSyncStorage(key: string, data: StorageAvailableTypes);
function saveToStorage(key: string, data: StorageAvailableTypes, type: StorageType);
function saveManyToStorage(data: StorageObjectType, type: StorageType);

Javascript

function saveManyToLocalStorage(data)
function saveManyToSyncStorage(data)
function saveToLocalStorage(key, data)
function saveToSyncStorage(key, data)
function saveToStorage(key, data, type)
function saveManyToStorage(data, type)

Example

saveManyToLocalStorage({
    secretKey: 'secret value saved in local storage',
    obj: {
        date: '20/2/2020',
        value: 100,
        isItTrue: true
    }
});
saveManyToSyncStorage({
    secretKey: 'secret value synced across devices',
    obj: {
        date: '20/2/2020',
        value: 100,
        isItTrue: true
    }
});
saveToLocalStorage('secretKey', 'secret value synced across devices');
saveToSyncStorage('secretKey', 'secret value synced across devices'); // with string
saveToSyncStorage('obj', {
    date: '20/2/2020',
    value: 100,
    isItTrue: true
});

saveToStorage(
    'obj',
    {
        date: '20/2/2020',
        value: 100,
        isItTrue: true
    },
    StorageType.Sync
);

saveToStorage(
    'obj',
    {
        date: '20/2/2020',
        value: 100,
        isItTrue: true
    },
    'sync'
);

saveManyToStorage(
    {
        secretKey: 'secret value synced across devices',
        obj: {
            date: '20/2/2020',
            value: 100,
            isItTrue: true
        }
    },
    StorageType.Sync
);

// you can also chain actions using then. Take care, the promise returns void
saveToLocalStorage('secretKey', 'secret value synced across devices').then(() => {
    // action
});

Remove from storage

Typescript

function removeAllLocalStorage();
function removeAllSyncStorage();
function removeFromLocalStorage(keys: string | string[]);
function removeFromSyncStorage(keys: string | string[]);
function removeFromStorage(keys?: string | string[], type: StorageType);

Javascript

function removeAllLocalStorage()
function removeAllSyncStorage()
function removeFromLocalStorage(keys)
function removeFromSyncStorage(keys)
function removeFromStorage(keys, type)

Example

removeAllLocalStorage();
removeAllSyncStorage();

removeFromLocalStorage('secretKey');
removeFromSyncStorage('secretKey');
removeFromSyncStorage('obj');
removeFromSyncStorage(['secretKey', 'obj']);

removeFromStorage('obj', StorageType.Sync);
removeFromStorage(['secretKey', 'obj'], StorageType.Sync);

// you can also chain actions using then. Take care, the promise returns void
removeAllLocalStorage().then(() => {
    // action
});

Save to storage

Typescript

function getAllLocalStorage<T = StorageAvailableTypes>();
function getAllSyncStorage<T = StorageAvailableTypes>();
function getAllManagedStorage<T = StorageAvailableTypes>();
function getFromLocalStorage<T = StorageAvailableTypes>(keys: string | string[]);
function getFromSyncStorage<T = StorageAvailableTypes>(keys: string | string[]);
function getFromManagedStorage<T = StorageAvailableTypes>(keys: string | string[]);
function getFromStorage<T = StorageAvailableTypes>(keys?: string | string[], type: StorageType): Promise<StorageObjectType<T>>;

Javascript

function getAllLocalStorage()
function getAllSyncStorage()
function getAllManagedStorage()
function getFromLocalStorage(keys)
function getFromSyncStorage(keys)
function getFromManagedStorage(keys)
function getFromStorage(keys, type)

Example

// All functions return a promise with an object that contains the searched key and the value.
getAllLocalStorage().then((allLocalStorage) => {
    //...
});
getAllSyncStorage().then((x) => {
    //...
});
getAllManagedStorage().then((x) => {
    //...
});
getFromLocalStorage('secretKey').then((x) => {
    //...
});
getFromLocalStorage(['secretKey', 'obj']).then((x) => {
    //...
});
getFromSyncStorage('secretKey').then((x) => {
    //...
});
getFromManagedStorage('secretKey').then((x) => {
    //...
});
getFromStorage('secretKey', StorageType.Sync).then((x) => {
    //...
});

// Using async/await
const allLocalStorage = await getAllLocalStorage();

// Using generics
getFromLocalStorage<{ secretKey: string }>('secretKey').then((x) => {
    //...
});

type StoredObject = {
    date: string;
    value: number;
    isItTrue: boolean;
};
getFromLocalStorage<StoredObject>('obj').then((x) => {
    //...
});
//or
const storedObjectFromLocalStorage = await getFromLocalStorage<StoredObject>('obj');

Importing the library

This library is available both as an unminified ES6 module and as a minfied libraries created by webpack.

  • Using the unminified ES6 module each individual functions can be imported own their own. This is very useful if you use modern TS/JS, especially with a bundling system that can remove unused functions.
import { onMessage } from '@wbet/message-api/message-receiver';
  • The minified libraries are compatible with ES6, AMD, CommonJS and the script tag.
// import using CommonJS(in nodeJS)
const storageApi = require('@wbet/storage-api');

// import using AMD
require(['storageApi'], (storageApi) => {
    // ...
});

// import using ES6 modules
import * as storageApi from '@wbet/storage-api';
// import using the script tag - importing minified UML - I will use relative paths to exemplify
<script src="./dist/umd/index.min.js"></script>
<script>
    // The 'storageApi' library is added as a property to the window object
    window.storageApi.getFromStorageByKey(...)
    // ...
</script>

// import using the script tag - importing minified script from the Web folder - I will use relative paths to exemplify
<script src="./dist/web/index.min.js"></script>
<script>
     // The 'storageApi' library is saved in a global variable
    storageApi.getFromStorageByKey(...)
</script>

// if you want to load the script from a CDN you can use
<script src="https://unpkg.com/@wbet/storage-api/dist/umd/index.min.js"></script>

// or
<script src="https://unpkg.com/@wbet/storage-api/dist/web/index.min.js"></script>

Final note

This library will be improved over time with APIs like connect and sendNativeMessage.

If you found any bug please open an issue and it will be addressed as soon as humanly possible.