Quick Brick Context Keys Manager

The ContextKeysManager is an api which allows any plugin developer to save and persist context keys in the app. This context keys are then available in zapp pipes endpoints to enrich requests made to back end feeds.

Ultimately, context keys are simply stored in the device's storage. The ContextKeysManager provides a single getter method which can resolve keys regardless of which storage is used (session storage, local storage, secure storage), and a single setter which will use the appropriate storage based on the requested storage level (default, persistent, secure).

The ContextKeysManager also provides convenience methods to batch requests for getting, setting or removing multiple keys at once, although it is also possible to get, set or remove a single key.

The ContextKeysManager API can be found in the @applicaster/zapp-react-native-utils package, from Quick Brick version 5.4.2-rc.4. Example imports are provided below.

API documentation

getKey()

Fetches a value by key from storage (session, local or secure). In case failed to fetch returns null. Key can be:

  • object => { key: 'some_key', namespace: 'some_namespace'}
  • string with namespace => "some_namespace.some_key"
  • string without namespace => "some_key" in this case we use default namespace="applicaster.v2"

Example:

import { ContextKeysManager } from "@applicaster/zapp-react-native-utils/appUtils/contextKeyManager";
const key: KeyName = { key: "some_key", namespace: "namespace" };
const value = await ContextKeysManager.instance.getKey(key);

getKeys()

fetch values by keys from storages(session, local or secure). It return structure Map with key=>value. If failed to get some value it returns null for it.

Example:

import { ContextKeysManager } from "@applicaster/zapp-react-native-utils/appUtils/contextKeyManager";
const keys: KeyName[] = ["key1", "key2", "key3"];
const values: Map<KeyName, ValueOrNothing> =
await ContextKeysManager.instance.getKeys(keys); // Map([[key1, value1], [key2, null], [key3, value3]])

setKey()

save value by key to one specific storage(session, local or secure). For success save returns true. For failed returns false. Input: object of that shape => { key, value, storageLevel }

  • key: string | { key: 'some_key', namespace: 'some_namespace' }
  • value: string | number | boolean | Array<any> | Object
  • storageLevel?: StorageLevel.default | StorageLevel.persisted | StorageLevel.secure

When storageLevel is omitted we use StorageLevel.default.

Example:

import {
ContextKeysManager,
StorageLevel,
} from "@applicaster/zapp-react-native-utils/appUtils/contextKeyManager";
const key: KeyName = { key: "some_key", namespace: "some_namespace" };
const value = "value";
const storageLevel = StorageLevel.persisted;
const resultOfSave: boolean = await ContextKeysManager.instance.setKey({
key,
value,
storageLevel
});

setKeys()

Save array of values by keys to one specific storage(session, local or secure). For each key can be chosen specific storage. It return structure Map with key => result_of_saving. Result of saving can be true in success saving, otherwise false. Inputs - array of objects such shape as { key, value, storageLevel }

  • key: string | { key: 'some_key', namespace: 'namespace' }
  • value: string | number | boolean | Array<any> | Object
  • storageLevel?: StorageLevel.default | StorageLevel.persisted | StorageLevel.secure

When storageLevel is omitted we use StorageLevel.default.

Example:

import { ContextKeysManager } from "@applicaster/zapp-react-native-utils/appUtils/contextKeyManager";
const key: KeyName = { key: "some_key", namespace: "some_namespace" };
const value = "value";
const storageLevel = StorageLevel.persisted;
const keys = [{ key, value, storageLevel }];
const resultsOfSave: Map<KeyName, boolean> =
await ContextKeysManager.instance.setKeys(keys); // Map([[key1, true], [key2, false], [key3, true]])

removeKey()

drop value by key in all storages(session, local and secure). If removing was succeed -> return true, otherwise false. Key can be:

  • object => { key: 'some_key', namespace: 'some_namespace'}
  • string with namespace => "some_namespace.some_key"
  • string without namespace => "some_key" in this case we use default namespace="applicaster.v2"

Example:

import { ContextKeysManager } from "@applicaster/zapp-react-native-utils/appUtils/contextKeyManager";
const key: KeyName = { key: "some_key", namespace: "some_namespace" };
const resultOfRemoving: boolean = await ContextKeysManager.instance.removeKey(
key
);

removeKeys()

drop values by keys in all storages(session, local and secure). It return structure Map with key => result_of_removing. Result of removing can be true in success removing, otherwise false. Key can be:

  • object => { key: 'some_key', namespace: 'some_namespace'}
  • string with namespace => "some_namespace.some_key"
  • string without namespace => "some_key" in this case we use default namespace="applicaster.v2"

Example:

import { ContextKeysManager } from "@applicaster/zapp-react-native-utils/appUtils/contextKeyManager";
const key: KeyName = { key: "some_key", namespace: "some_namespace" };
const keys: KeyName[] = [key];
const resultsOfRemoving: Map<KeyName, boolean> =
await ContextKeysManager.instance.removeKeys(keys); // Map([[key1, true], [key2, false], [key3, true]])