Options
All
  • Public
  • Public/Protected
  • All
Menu

Contains utility methods for working with Stores.

// esm
import { stores } from '@paychex/core';

// cjs
const { stores } = require('@paychex/core');

// iife
const { stores } = window['@paychex/core'];

// amd
require(['@paychex/core'], function({ stores }) { ... });
define(['@paychex/core'], function({ stores }) { ... });

Index

Functions

  • Utility method to wrap a Store implementation as a Cache instance. Uses the Request url as the cache key.

    example
    import { createRequest, fetch } from '~/path/to/datalayer';

    const dataCall = {
    method: 'GET',
    base: 'server',
    path: '/values/:key'
    };

    // NOTE: use withEncryption(store, options) if the response
    // might contain personal or sensitive information that you
    // wish to keep secret
    const store = stores.indexedDB({ store: 'myDataValues' });
    const attempt = data.utils.withCache(fetch, stores.utils.asDataCache(store));

    export async function loadData(key) {
    const params = { key };
    const request = createRequest(dataCall, params);
    const response = await attempt(request).catch(errors.rethrow(params));
    return response.data;
    }

    Parameters

    • store: Store

      The Store implementation to use as the Cache backing.

    Returns Cache

    A Cache implementation backed by the specified Store.

  • Creates a DateFactory that returns a Date the specified number of days in the future.

    see

    withExpiration

    example
    export const store = stores.utils.withExpiration(
    stores.localStore(),
    stores.utils.days(1)
    );

    Parameters

    • count: number

      The number of days in the future the Date should be.

    Returns DateFactory

    A function that returns a Date.

  • Creates a DateFactory that returns a Date the specified number of hours in the future.

    see

    withExpiration

    example
    export const store = stores.utils.withExpiration(
    stores.localStore(),
    stores.utils.hours(8)
    );

    Parameters

    • count: number

      The number of hours in the future the Date should be.

    Returns DateFactory

    A function that returns a Date.

  • Creates a DateFactory that returns a Date the specified number of minutes in the future.

    see

    withExpiration

    example
    export const store = stores.utils.withExpiration(
    stores.localStore(),
    stores.utils.minutes(90)
    );

    Parameters

    • count: number

      The number of minutes in the future the Date should be.

    Returns DateFactory

    A function that returns a Date.

  • Creates a DateFactory that returns a Date the specified number of weeks in the future.

    see

    withExpiration

    example
    export const store = stores.utils.withExpiration(
    stores.localStore(),
    stores.utils.weeks(2)
    );

    Parameters

    • count: number

      The number of weeks in the future the Date should be.

    Returns DateFactory

    A function that returns a Date.

  • Wraps a Store instance so values are encrypted and decrypted transparently when get and set. For increased security, the key used to store a value will also be used to salt the given private key, ensuring each object is stored with a unique key.

    example
    // random private key and initialization vector

    const iv = window.crypto.getRandomBytes(new UintArray(16));
    const key = window.crypto.getRandomBytes(new UintArray(8));

    export const lockbox = stores.utils.withEncryption(stores.memoryStore(), { key, iv });
    example
    // user-specific private key and initialization vector

    import { proxy } from 'path/to/proxy';
    import { getUserPrivateKey, getUserGUID } from '../data/user';

    const database = stores.indexedDB({ store: 'my-store' });

    export async function loadData(id) {
    const iv = await getUserGUID();
    const key = await getUserPrivateKey();
    const encrypted = stores.utils.withEncryption(database, { key, iv });
    try {
    return await encrypted.get(id);
    } catch (e) {
    return await someDataCall(...)
    .then(value => {
    encrypted.set(id, value);
    return value;
    });
    }
    }

    Parameters

    • store: Store

      Underlying Store instance whose values will be encrypted during set calls and decrypted during get calls.

    • config: EncryptionConfiguration

      Indicates which encryption method and encryption key to use.

    Returns Store

    A Store instance that will encrypt and decrypt values in the underlying store transparently.

  • Wraps a Store so values expire after a specified Date. Any attempts to retrieve a value after it has expired will return undefined.

    example
    export const store = stores.utils.withExpiration(
    stores.localStore(),
    stores.utils.minutes(90)
    );
    example
    import { user } from '../path/to/user';
    import { fetch, createRequest } from '../path/to/datalayer';

    const reports = stores.indexedDB({ store: 'reports' });
    const expires = stores.utils.withExpiration(reports, stores.utils.days(30));
    const encrypted = stores.utils.withEncryption(expires, {
    iv: user.id,
    key: user.privateKey,
    });

    const operation = {
    base: 'reports',
    path: '/reports/:id'
    };

    const pipeline = data.utils.withCache(fetch, stores.utils.asDataCache(encrypted));

    export async function getReportById(id) {
    const request = createRequest(operation, { id });
    const response = await pipeline(request);
    return response.data;
    }

    Parameters

    • store: Store

      The store to wrap.

    • dateFactory: DateFactory

      Function to create expiration Dates.

    Returns Store

    A Store that returns undefined if a value has expired.

  • Wraps a Store so any keys are transparently modified before access. This can be useful when storing data on a machine that will have more than 1 user, to ensure different users don't access each other's stored information.

    example
    import { user } from '../data/user';

    const store = stores.utils.withPrefix(stores.localStore(), user.guid);
    example
    import { user } from '../data/user';

    const store = stores.utils.withPrefix(stores.localStore(), function(key) {
    return `${key}|${user.guid}`;
    });

    Parameters

    • store: Store

      The store whose keys should be modified before access.

    • prefix: string | Prefixer

      A string to prepend to any keys or a function that will modify a key.

    Returns Store