1.0.1 • Published 3 years ago

@aricma/browser-storage-actions v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

browser storage actions

this is a typescript project

lint compile test coverage

A functional approach to creating common actions for all your browser storage options. Using the browser storage options is always a pain. Let's make this as easy and secure as we can.

We currently support:

  • LocalStorage

We will support:

  • Cookies
  • Session Storage
  • ... what do you need?

setup

yarn add @aricma/browser-storage-actions

or

npm install @aricma/browser-storage-actions

usage

import { createLocalStorageActions } from '@aricma/browser-storage-actions';

const localStorageActions = createLocalStorageActions({
    nameSpace: "YOU_NAME_SPACE",
    localStorage: window.localStorage,
})

interface SomeFeatureState {
    value: string,
    counter: numbre,
    selection: string,
}

const item: SomeFeatureState = localStorageActions.addItem<SomeFeatureState>({
    name: 'my-feature-state',
    value: {
        value: 'hello world!',
        counter: 3,
        selection: 'some value from selection',
    },
}).value;

domain models

export interface BrowserStorageActions {
    hasItem: (name: string) => boolean,
    addItem: <Value>(item: BrowserStorageItem<Value>) => BrowserStorageItem<Value>,
    getItemByName: <Value>(name: string) => BrowserStorageItem<Value>,
    getAllItemsForNameSpace: () => Array<BrowserStorageItem<any>>,
    updateItem: <Value>(item: BrowserStorageItem<Value>) => BrowserStorageItem<Value>,
    removeItemByName: (name: string) => void,
    removeAllItemsForNameSpace: () => void,
}

export interface BrowserStorageItem<Value = string> {
    name: string,
    value: Value,
}

createLocalStorageActions

interface createLocalStorageActions {
    (depenedncies: LocalStorageActionsDependencies): BrowserStorageActions
}

interface LocalStorageActionsDependencies {
    nameSpace: string,
    localStorage: BrowserLocalStorage
}

Feedback