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 🙏

© 2025 – Pkg Stats / Ryan Hefner

safeway

v1.1.0

Published

Type-safe serialisation and validation wrapper for string storage APIs

Downloads

8

Readme

safeway

A type-safe serialisation and validation wrapper for string storage APIs like localStorage and sessionStorage, using Standard Schema for validation.

Installation

npm install safeway

Usage

Synchronous Storage

To create a type safe store, provide a key and a schema. The key will be where the value is stored, and the schema will be used to validate the value when getting it from storage. (When setting a value, no runtime validation happens, only TypeScript type checking.)

Defaults to localStorage and JSON serialisation.

import { createStore } from "safeway";
import { z } from "zod";

const store = createStore("count", z.number());

store.set(1); // typed based on schema input (number)
console.log(store.get()); // 1 - typed based on schema output (number | undefined)

store.remove();
console.log(store.get()); // undefined (still typed as number | undefined)

Schemas are allowed to include transformations, in which case store.set's parameter will be based on the schema's expected input.

import { createStore } from "safeway";
import { z } from "zod";

const store = createStore(
  "count",
  z.number().transform((count) => ({ count })),
);

store.set(1); // typed based on schema input (number)
console.log(store.get()); // { count: 1 } - typed based on schema output ({ count: number } | undefined)

Multi-store

If you want to create a store with multiple keys and types, you can use createMultiStore instead.

import { createMultiStore } from "safeway";
import { z } from "zod";

const store = createMultiStore({
  count: z.number(),
  name: z.string(),
});

store.set("count", 1); // typed based on schema input (number)
console.log(store.get("count")); // 1 - typed based on schema output (number | undefined)

Custom serialisation

If JSON doesn't cover all your needs, you can provide your own serialisation methods (or use a compatible library like superjson). Should include parse and stringify methods.

import { createStore } from "safeway";
import { z } from "zod";
import superjson from "superjson";

const store = createStore("counts", z.set(z.number()), {
  serializer: superjson,
});

Custom storage

If you want to use a different storage instance, you can provide it. Should include getItem, setItem and removeItem methods.

import { createStore } from "safeway";
import { z } from "zod";

const store = createStore("count", z.number(), {
  storage: sessionStorage,
});

Building a custom store creator

Instead of providing the same config every time, you can build a custom store creator with your own defaults.

import { buildStoreCreator } from "safeway";
import { z } from "zod";
import superjson from "superjson";

const createSuperStore = buildStoreCreator({
  serializer: superjson,
  storage: sessionStorage,
});

const store = createSuperStore("count", z.number());

buildStoreCreator also has a multi-store equivalent, buildMultiStoreCreator.

Asynchronous Storage

createStore requires both storage and schemas to be synchronous. If you need asynchronous storage and/or schemas, use createAsyncStore instead. (createMultiStore also has an asynchronous equivalent, createAsyncMultiStore)

The API is the same as createStore, but all methods return promises.

Defaults to localStorage and JSON serialisation.

import { createAsyncStore } from "safeway";
import { z } from "zod";
import AsyncStorage from "@react-native-async-storage/async-storage";

const store = createAsyncStore("count", z.number(), {
  storage: AsyncStorage,
});

await store.set(1); // typed based on schema input (number)
console.log(await store.get()); // 1 - typed based on schema output (number | undefined)

await store.remove();
console.log(await store.get()); // undefined (still typed as number | undefined)

Supports all the same customisation options as the synchronous storage, but with asynchronous storage methods allowed. A store creator can be built with buildAsyncStoreCreator. (buildAsyncMultiStoreCreator also exists)