@grnx-utils/local-storage
Provides a convenient API for working with localStorage.
Features:
- Automatic parsing if the value is in JSON format
- Automatic JSON.stringify if you want to set non-primitive type to localStorage
-
Next.js
support (check iftypeof window !== 'undefined'
) - Ability to add a prefix to keys (useful if you have several applications on the same domain. For example, turns
AUTH_TOKEN
intomyapp__AUTH_TOKEN
) - Ability to disable setting new values if necessary
- LocalStorage key typing (with typescript)
Installation
yarn add @grnx-utils/local-storage -D
Basic Usage
import { createStorage } from '@grnx-utils/local-storage'
export enum LocalStorage {
someKey = 'someKey'
}
const storage = createStorage<LocalStorage>({
layer: 'my-app'
})
storage.set('someKey', {
someKey: 'value'
})
storage.get('someKey') // { someKey: 'value' }
storage.clear('someKey')
storage.get('someKey') // void
storage.disable() // will stop work
Comparison with regular localStorage
// @grnx-utils/local-storage
storage.set('someKey', {
someKey: 'value'
})
// common localStorage
if (typeof window !== 'undefined') {
localStorage.setItem(
'my-app__someKey',
JSON.stringify({
someKey: 'value'
})
)
}
Configure API
const storage = createStorage<LocalStorage>({
/** Application layer
* @type string
* @default - undefined
*/
layer: 'my-app',
/**
* @type boolean
* @default - false
*/
isDisabled: true
})