• Create persistent or temporary client-side storage with a portable, type-safe wrapper around the Web Storage API.

    Type Parameters

    • T

    Parameters

    • key: string

      The key used to access the stored value.

    • Optionalsession: boolean = false

      Save data in sessionStorage (true) or in localStorage (false). Default is false.

    Returns StoreBuddyInit<T>

    import storeBuddy from "store-buddy";

    // "foo" is the key used to access the stored value, which is created in the
    // method `init`
    const storage1 = storeBuddy("foo").init("bar");

    // Using sessionStorage instead of localStorage is possible by specifying
    // `true` in the `session` parameter
    const storage2 = storeBuddy("foo", true).init("bar");

    // Type safety can be enabled by providing an argument to the type parameter
    type Data = {
    hello: string;
    world: number;
    }

    const storage3 = storeBuddy<Data>("foo").init({
    hello: "foo",
    world: 123
    })