# ztorage

> A flexible and type-safe wrapper for key-value storage

Latest version **1.0.0** (published 2023-02-19) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install ztorage
pnpm add ztorage
yarn add ztorage
bun add ztorage
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-02-19 |
| First published | 2023-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Kevin Hermawan |
| Maintainers | khermawan |
| Keywords | wrapper, type-safe, type validation, key-value storage, web storage, localStorage, sessionStorage |

## Links

- npm: https://www.npmjs.com/package/ztorage
- Repository: https://github.com/kevinhermawan/ztorage
- Homepage: https://github.com/kevinhermawan/ztorage#readme
- Issues: https://github.com/kevinhermawan/ztorage/issues
- npm.io page: https://npm.io/package/ztorage

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2023-02-19

## README

# Ztorage

![Minified size](https://img.shields.io/bundlephobia/min/ztorage) ![Test coverage](https://img.shields.io/codecov/c/github/kevinhermawan/ztorage) ![Monthly download](https://img.shields.io/npm/dm/ztorage)

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](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API) and other storage backends. It takes advantage of [Zod](https://github.com/colinhacks/zod), a powerful TypeScript schema validation library, to provide automatic parsing and validation of data.

## 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 `ztorage` and `zod`, by running the following command:

**NPM**

```
npm install ztorage zod
```

**Yarn**

```
yarn add ztorage zod
```

**pnpm**

```
pnpm add ztorage zod
```

## Usage

```ts
import { z } from "zod";
import Ztorage from "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](/LICENSE)

---
_Source: https://npm.io/package/ztorage · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
