🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

typed-local-store

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-local-store

A type wrapper for the store api

2.0.2
latest
npm
Version published
Weekly downloads
354
-8.76%
Maintainers
1
Weekly downloads
 
Created
Source

typed-local-storage Tests Actions Status

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

:floppy_disk: Installation

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

:technologist: 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>();

:monocle_face: API

The API of typed-local-store mimics the Web Storage API. This allows for a easy transition from using localStorage directly to using TypedLocalStore. |

Options

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

PropertyRequiredDefaultDescription
storage: stringNo'localStorage'Choose the storage type, "localStorage" or "sessionStorage"
fallbackStorage: StorageNoundefinedProvide a fallback storage in case localStorage and or SessionStorage are not available
ignoreMissingStorage: booleanNofalsePrevent error to be thrown when no storage is present.

getItem

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

ModeDescription
'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

MemoryStorage

Sometimes it is desireable to not rely on the browser API ( e.g. in case of SSR). This package ships a in-memory replacement for the Web Storage API which can be used in places where the browser API is not present. It can be used alone or passed to the TypedLocalStore via the fallbackStorage option:

import TypedLocalStore, { MemoryStorage } from 'typed-local-store';

const memoryStorage = new MemoryStorage();
const typedStorage = new TypedLocalStore<Schema>({
  fallbackStorage: memoryStorage,
});

:hammer_and_wrench: Contributing

Interested in contributing? Great!

To fix a bug or add a feature, follow these steps:

  • Create a Fork of the repo
  • Create a new branch (git checkout -b your-branch)
  • Add your changes and new tests if necessary
  • Make sure all tests pass
  • Commit your changes (git commit -am 'feat: fantastic feature')
  • Push the branch (git push origin your-branch)
  • Create a Pull Request

Development

The required packages to start development can be installed with

npm install
# or
yarn install

The tests can be run with

npm run test
# or
yarn test

Keywords

typed

FAQs

Package last updated on 31 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts