Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jill64/typed-storage
🗃️ Type-Safe Web Storage API Wrapper
https://github.com/jill64/typed-storage
localstorage sessionstorage type-safe utility
Last synced: about 1 month ago
JSON representation
🗃️ Type-Safe Web Storage API Wrapper
- Host: GitHub
- URL: https://github.com/jill64/typed-storage
- Owner: jill64
- License: mit
- Created: 2023-09-07T12:46:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-01T18:15:57.000Z (about 2 months ago)
- Last Synced: 2024-11-01T19:20:14.310Z (about 2 months ago)
- Topics: localstorage, sessionstorage, type-safe, utility
- Language: TypeScript
- Homepage: https://npmjs.com/package/@jill64/typed-storage
- Size: 609 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @jill64/typed-storage
🗃️ Type-Safe Web Storage API Wrapper
## Installation
```sh
npm i @jill64/typed-storage
```## Example
See [ts-serde](https://github.com/jill64/ts-serde#readme) for more information on type guard
```ts
import { typedStorage } from '@jill64/typed-storage'
import { json } from '@jill64/typed-storage/serde'const key = 'localStorageKey'
const value = ['value1', 'value2', 'value3']const guard = (x: unknown): x is string[] =>
Array.isArray(x) && x.every((y) => typeof y === 'string')const store = typedStorage(key, json(guard, []), {
// Optional
// Use sessionStorage
// sessionStorage?: boolean
})// string[]
const storedValue = store.get()store.set(value)
const unsubscriber = store.subscribe((newValue) => {
// called when localStorage value changes
console.log(newValue)
})// unsubscribe
unsubscriber()
```## License
[MIT](LICENSE)