All files / lib store.js

100% Statements 18/18
100% Branches 14/14
100% Functions 12/12
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78  3x                   45x                 6x 3x 3x                 3x 3x                       6x 3x   3x 3x                     9x 6x 3x                   3x 3x          
function onError(error) {
  return Promise.reject(error);
}
/**
 * Class used by an extension to store your data in {@link external:localStorage}.
 */
class Store {
  /**
   * @hideconstructor
   */
  constructor(connection) {
    this._connection = connection;
  }
  /**
     * Gets the value of key
     * @param  {string} key Key of the stored data
     * @example extension.store.get('key').then((value) => console.log(value)) // will log value for the given key
     * @return {external:Promise}
     */
  get(key) {
    if (!key || typeof key !== 'string') { throw new Error('Kindly provide valid parameters'); }
    return this._connection.sendToParent('store', { action: 'get', key })
      .then(event => Promise.resolve(event.data)).catch(onError);
  }
 
  /**
     * Gets an object with all the stored key-value pairs.
     * @example extension.store.getAll().then((obj) => obj)
     * @return {external:Promise}
     */
  getAll() {
    return this._connection.sendToParent('store', { action: 'getAll' })
      .then(({ data = {} }) => Promise.resolve(data)).catch(onError);
  }
 
  /**
     * Sets the value of a key
     * @param  {string} key Key of the stored data.
     * @param {*} value Data to be stored.
     * @example extension.store.set('key', 'value').then((success) => console.log(success)) // will log ‘true’ when value is set
     * @return {external:Promise}
     */
 
  set(key, value) {
    if (!key || !value || typeof key !== 'string') {
      throw new Error('Kindly provide valid parameters');
    }
    return this._connection.sendToParent('store', { action: 'set', key, value })
      .then(() => Promise.resolve(true)).catch(onError);
  }
 
  /**
     * Removes the value of a key
     * @param  {string} key  Key of the data to be removed from the store
     * @example extension.store.remove('key').then((success) => console.log(success)) // will log ‘true’ when value is removed
     * @return {external:Promise}
     */
 
  remove(key) {
    if (!key || typeof key !== 'string') { throw new Error('Kindly provide valid parameters'); }
    return this._connection.sendToParent('store', { action: 'remove', key })
      .then(() => Promise.resolve(true)).catch(onError);
  }
 
  /**
     * Clears all the stored data of an extension
     * @example extension.store.clear().then((success) => console.log(success)) // will log ‘true’ when values are cleared
     * @return {external:Promise}
     */
 
  clear() {
    return this._connection.sendToParent('store', { action: 'clear' })
      .then(() => Promise.resolve(true)).catch(onError);
  }
}
 
export default Store;