Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webext-storage

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webext-storage

A more usable typed storage API for Web Extensions

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1K
increased by33.21%
Maintainers
1
Weekly downloads
 
Created
Source

webext-storage

A more usable typed storage API for Web Extensions

  • Browsers: Chrome, Firefox, and Safari
  • Manifest: v2 and v3
  • Permissions: storage or unlimitedStorage
  • Context: They can be called from any context

Sponsored by PixieBrix :tada:

chrome.storage.local.get() is very inconvenient to use and it does not provide type safety. This module provides a better API:

// Before
const storage = await chrome.storage.local.get('user-options');
const value = storage['user-options']; // The type is `any`
await chrome.storage.local.set({['user-options']: {color: 'red'}}); // Not type-checked
chrome.storage.onChanged.addListener((storageArea, change) => {
	if (storageArea === 'local' && change['user-options']) { // Repetitive
		console.log('New options', change['user-options'].newValue)
	}
});

// After
const options = new StorageItem<Record<string, string>>('user-options');
const value = await options.get(); // The type is `Record<string, string> | undefined`
await options.set({color: 'red'}) // Type-checked
options.onChanged(newValue => {
	console.log('New options', newValue)
});

Why this is better:

  • The storage item is defined in a single place, including its storageArea, its types and default value
  • get only is only .get() instead of the awkward post-get object access that
  • Every get and set operation is type-safe
  • If you provide a defaultValue, the return type will not be | undefined
  • The onChanged example speaks for itself

Install

npm install webext-storage

Or download the standalone bundle to include in your manifest.json.

Usage

import {StorageItem} from "webext-storage";

const username = new StorageItem<string>('username')
// Or
const username = new StorageItem('username', {defaultValue: 'admin'})

await username.set('Ugo');
// Promise<void>

await username.get();
// Promise<string>

await username.remove();
// Promise<void>

await username.set({name: 'Ugo'});
// TypeScript Error: Argument of type '{ name: string; }' is not assignable to parameter of type 'string'.

username.onChanged(newName => {
	console.log('The user’s new name is', newName);
});

License

MIT © Federico Brigante

Keywords

FAQs

Package last updated on 30 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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