Skip to content

A type-safe and schema based LocalStorage and SessionStorage implementation using TypeScript.

Notifications You must be signed in to change notification settings

solydhq/typed-local-store

Repository files navigation

typed-local-storage Tests Actions Status

A wrapper to provide type safe access to the localStorage and sessionStorage.

Installation

npm install typed-local-store
# or
yarn add typed-local-store

Usage

Create a schema of your desired storage structure:

interface Schema {
  counter: number;
  message: string;
  user: {
    id: string;
    name: string;
    email: string;
    isAdmin: boolean;
  };
  posts: string[];
}

Then create your storage using the defined schema:

import TypedLocalStore from 'typed-local-store';

const typedStorage = new TypedLocalStore<Schema>();

Options

The constructor can receive a options object to configure the store.

options

Property Required Default Description
storage false "localStorage" Choose the storage type, "localStorage" or "sessionStorage"

API

The API of typed-local-store mimics the Storage API except for one exception, the Storage APIs length property is implemented as a method.

getItem

The getItem method has three retrieval modes, whereas 'fail' is the default mode

Mode Description
fail If a something to be restored from the store can not be parsed by JSON.parse a error is thrown
raw If parsing of the retrieval value fails, the unparsed value is returned
safe If parsing of the retrieval value fails, null is returned

About

A type-safe and schema based LocalStorage and SessionStorage implementation using TypeScript.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published