JSPM

@stash-it/read-only-plugin

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q11937F
  • License MIT

Package Exports

  • @stash-it/read-only-plugin

Readme

@stash-it/read-only-plugin

logo-stash-it-color-dark 2x

@stash-it/read-only-plugin is a plugin for @stash-it/stash-it that allows you disabl chaning items, or adding new ones.

When to use it?

Let's say you have a role, for logged in users (or not logged in), that gives them limited access to things. And that would also mean not doing any changes in the storage related to the items stored there.

Installation

npm

npm install @stash-it/read-only-plugin

deno

deno add @stash-it/read-only-plugin

yarn

yarn dlx jsr add @stash-it/read-only-plugin

pnpm

pnpm dlx jsr add @stash-it/read-only-plugin

bun

bunx jsr add @stash-it/read-only-plugin

Usage

// Import stash-it main class.
import { StashIt } from "@stash-it/stash-it";

// Import the plugin.
import { createReadOnlyPlugin } from "@stash-it/read-only-plugin";
import { MemoryAdapter } from "@stash-it/memory-adapter";

// Create an instance of the adapter.
const adapter = new MemoryAdapter();

// Create an instance of stash-it.
const stash = new StashIt(adapter);

// Create an instance of the plugin.
const plugin = createReadOnlyPlugin();

// Register the plugin to the stash-it instance.
stash.registerPlugins([plugin]);

// Use it.
await stash.get("key"); // Returns an item if it exists

await stash.set("key", "value"); // Throws an error

You can also configure your own error messages:

createReadOnlyPlugin({
  setItemErrorMessage: "Custom setItem error message.",
});