@hyperjumptech/ztorage
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Ztorage

Minified size Test coverage Monthly download

Ztorage is a flexible and type-safe wrapper for key-value storage that is designed to work with various storage mediums, including Web Storage API and other storage backends. It takes advantage of Zod, a powerful TypeScript schema validation library, to provide automatic parsing and validation of data.

Articles

Features

  • Works with various storage mediums
  • Provides type safety for key-value storage
  • Enables automatic parsing and validation of data
  • Zero dependencies

Installation

To use Ztorage, you must have Zod installed. You can install @hyperjumptech/ztorage and zod, by running the following command:

NPM

npm install @hyperjumptech/ztorage zod

Yarn

yarn add @hyperjumptech/ztorage zod

pnpm

pnpm add @hyperjumptech/ztorage zod

Usage

import { z } from "zod";
import Ztorage from "@hyperjumptech/ztorage";

// Define a schema for the data you want to store
const schema = z.object({
  isDarkModeEnabled: z.boolean(),
});

// Instantiate a Ztorage object with a schema and storage functions
const storage = new Ztorage({
  schema,
  onGetItem: async (key) => {
    return localStorage.getItem(key);
  },
  onSetItem: async (key, value) => {
    localStorage.setItem(key, value);
  },
  onRemoveItem: async (key) => {
    localStorage.removeItem(key);
  },
});

// Set item to storage
await storage.setItem("isDarkModeEnabled", true);

// Get item from storage
const isDarkModeEnabled = await storage.getItem("isDarkModeEnabled");
console.log(isDarkModeEnabled); // true

// Remove item from storage
await storage.removeItem("isDarkModeEnabled");

License

MIT License

Package Sidebar

Install

npm i @hyperjumptech/ztorage

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

6.27 kB

Total Files

5

Last publish

Collaborators

  • khermawan
  • nico-hyperjump
  • rafel
  • haricnugraha
  • budhi_npm
  • raosanfl
  • gregoryhyperjump
  • satriyopranoto
  • garychristianto