TypeScript: Sync variables with localStorage https://www.npmjs.com/package/webstorage-decorators
Go to file
Zakary Timson 034134a08f
Build / Build NPM Project (push) Successful in 36s Details
Build / Tag Version (push) Successful in 7s Details
Build / Publish (push) Successful in 16s Details
Updated documentation
2024-01-07 19:11:26 -05:00
.github/workflows Updated documentation 2024-01-07 19:11:26 -05:00
src Updated documentation 2024-01-07 19:11:26 -05:00
tests Updated documentation 2024-01-07 19:11:26 -05:00
.gitignore Bump to 4.1.0, removed lib 2021-05-15 20:18:48 -04:00
LICENSE Updated documentation 2024-01-07 19:11:26 -05:00
README.md Updated documentation 2024-01-07 19:11:26 -05:00
jest.config.js Updated documentation 2024-01-07 19:11:26 -05:00
package-lock.json Updated documentation 2024-01-07 19:11:26 -05:00
package.json Updated documentation 2024-01-07 19:11:26 -05:00
tsconfig.json Updated documentation 2024-01-07 19:11:26 -05:00

README.md


Logo

webstorage-decorators

TypeScript: Sync variables with localStorage

Version Pull Requests Issues



Table of Contents

About

Deprecated: Please use the replacement library var-persist.

WebStorage-Decorators is a library that adds property decorators to sync a class property with the local or session storage. It is useful for persisting state through reloads without making any changes to existing code.

This library has some caveats that var-persist rectifies:

  • Only supports decorators
  • Impure functions can make changes to data which do not get synced
  • [Proto]types and functions are lost

Disclaimer: JavaScript's decorators are currently undergoing changes to the API overseen by TC39 and currently have no support for property decorators. Experimental decorators must be enabled to work properly.

Examples

import {LocalStorage, SessionStorage} from 'webstorage-decorators';

export class MyCustomClass {
   @LocalStorage('light_theme', {key: 'site_theme'}) theme: string;
   @SessionStorage(null, {encryptWith: config.entryptionKey}) thisUser: User;
   @SessionStorage() searchBar: string;
    
   constructor() {
       console.log(this.theme, localStorage.getItem('theme')); // Output: 'light_theme', 'light_theme'
       console.log(this.user, localStorage.getItem('user')); // Output: null, undefined
       user = {first: 'John', last: 'Smith', ...}
       console.log(this.user, localStorage.getItem('user')); // Output: {first: 'John', last: 'Smith', ...}, **Some encrypted value**
   }
}

Built With

TypeScript

Setup

Production

Prerequisites

Instructions

  1. Install persist: npm i webstorage-decorators
  2. Enable decorators inside tsconfig.json:
{
	"compilerOptions": {
		"experimentalDecorators": true,
		...
	},
	...
}
  1. Import & use, see examples above

Development

Prerequisites

Instructions

  1. Install the dependencies: npm i
  2. Build library & docs: npm build
  3. Run unit tests: npm test

Documentation

Decorators

Decorator Description
@LocalStorage(defaultValue: any, options: WebStorageOptions) Syncs property to LocalStorage item under the same name
@SessionStorage(defaultValue: any, options: WebStorageOptions) Syncs property to SessionStorage item under the same name

WebStorageOptions

Options Description
default Default value, same as decorator's first argument
key Key to reference value inside local/session storage (Defaults to the property name)

License

Copyright © 2023 Zakary Timson | Available under MIT Licensing

See the license for more information.