Vaulting
Vault 🔒
The Vault System provides robust mechanisms for secure data storage and management within the mobile app.
Custom Block Code Editor version: 1.8+
Functions
initialize
initialize
Description
Initializes a new Vault instance using the provided vaultKey
and scope
. This method returns a promise that resolves to a vault object with various methods for interacting with the Vault. This method can throw an VaultUserException
or any of its children.
Parameters
Parameter | Type | Description |
---|---|---|
vaultKey | string | The key used to identify the Vault. |
scope | string | The scope of the Vault. This can either be "user" or "global" (defaults to "user" ) |
Returns
A Promise that resolves to a Vault Object with the following methods or throws:
Method | Description |
---|---|
get(key):Promise | Retrieves the value associated with the given key. |
set(key, value):Promise | Sets the value associated with the given key. |
clear():Promise | Clears all values from the Vault. |
remove(key):Promise | Removes the value associated with the given key. |
keys():Promise | Retrieves a list of all keys in the Vault. |
observe(key, observer, errorHandler):Promise | Observes changes to the value associated with the given key. |
Scoping
The scope defines who should have access to the data stored within the vault.
Scope | Description |
---|---|
"user" | Vault is accessible only to the currently logged in user. |
"global" | Vault is available to unauthenticated users and same data is available multiple users. |
For safety, if a vault is created with a "user" scope, when the user logs out all interactions with the vault from that moment forward will throw a VaultAccessException
Example
const vault = await Tapcart.actions.vault.initialize("test", "user");
vault.get('myKey')
.then(value => console.log(value))
.catch((error)=>{console.log(error)});
Vault Object
Description
The vault object returned by the initialize method provides the following methods for interacting with the Vault.
Get
get(key)
Gets the value associated with the given key.
Returns a promise that resolves to the associated value.
Example
const vault = await Tapcart.actions.vault.initialize("test", "user");
vault.get('myKey')
.then(value => console.log(value))
.catch((error)=>{console.log(error)});
const vault = await Tapcart.actions.vault.initialize("test", "user");
vault.get('myKey')
.then(value => console.log(value))
.catch((error)=>{console.log(error)});
Set
set(key, value)
Sets the value associated with the given key. Calling set with a null value is the same as calling remove(key)
Params
Parameter | Type | Description |
---|---|---|
key | string | The key to set. |
value | string | The value to set. |
Return
A Promise
that resolves true
when the action was succesful.
Example
const vault = await Tapcart.actions.vault.initialize("test", "user");
await vault.set('myKey','foo')
console.log(await vault.get('myKey')) //will print 'foo'
Clear
clear()
Clears all values from the Vault.
Return
Returns a Promise that resolves true
when the action was succesful.
Example
const vault = await Tapcart.actions.vault.initialize("test", "user");
await vault.set('myKey','foo')
console.log(await vault.get('myKey')) //will print 'foo'
await vault.clear()
console.log(await vault.get('myKey')) //will return undefined
Remove
remove(key)
Removes the value associated with the given key.
Params
Method | Type | Description |
---|---|---|
key | string | The key to set. |
Return
A Promise
that resolves true
when the action was succesful.
Example
const vault = await Tapcart.actions.vault.initialize("test", "user");
await vault.set('myKey','foo')
console.log(await vault.get('myKey')) //will print 'foo'
await vault.remove('myKey')
console.log(await vault.get('myKey')) //will return undefined
Keys
keys()
Retrieves a list of all keys in the Vault.
Return
Returns a promise that resolves to an array of key values.
Example
const vault = await Tapcart.actions.vault.initialize("test", "user");
await vault.set('myKey','foo')
await vault.set('myKey2','bar')
console.log(await vault.keys()) //will return ['foo','bar']
Observe
observe(key, observer, errorHandler)
Observes changes to the value associated with the given key.
Method | Type | Description |
---|---|---|
key | string | The key to observe. |
observer | function | A function to call when the value changes. Will receive the current value upon registration. |
errorHandler | function | A function to call when an error occurs. |
Return
Returns a promise that resolves when the observer
is fully registered and can begin listening for updates.
Example
const vault = await Tapcart.actions.vault.initialize("test", "user");
await vault.set('myKey','foo')
await vault.observe('myKey',(value)=>{console.log(value}),(error)=>{console.log(error)}}
await vault.set('myKey','bar') // ^ the observer will log 'bar'
Error Handling
The Vault System introduces various classes for handling different types of errors:
These are availble for use with instanceof
under
Tapcart.actions.vault.errorTypes //returns an array of the possible Exception types
Exception | Description |
---|---|
VaultUserException | Thrown when there is a general error related to the Vault. |
VaultAccessException | Thrown when there is an access-related error. |
VaultBuildException | Thrown when there is a Vault construction-related error. |
VaultInvalidArgumentException | Thrown when an invalid argument is passed. |
VaultInvalidScopeException | Thrown when there is an error related to the scope. |
VaultScopeModificationException | Thrown when a vault is already loaded with a different scope. |
VaultUserIdentifierException | Thrown when there is an identifier-related error. |
Updated 6 months ago