UNPKG

balena-settings-storage

Version:
151 lines (148 loc) 4.13 kB
"use strict"; /* Copyright 2016 Balena Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getStorage = void 0; const tslib_1 = require("tslib"); const balena_errors_1 = require("balena-errors"); /** * @module storage */ /** * @summary Get an instance of storage module * @function * @static * @public * * @param {Object} options - options * @param {String|False} [options.dataDirectory] - the directory to use for storage in Node.js or false to create an isolated in memory instance. Values other than false are ignored in the browser. * * @return {storage} * @example * // with es6 imports * import { getStorage } from 'balena-settings-storage'; * // or with node require * const { getStorage } = require('balena-settings-storage'); * * const storage = getStorage({ * dataDirectory: '/opt/cache/balena' * }); */ const getStorage = (store) => { /** * @summary Set a value * @function * @public * * @param {String} name - name * @param {*} value - value * * @return {Promise} * * @example * storage.set('token', '1234') */ const set = (name, value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { if (typeof value !== 'string') { value = JSON.stringify(value); } return store.setItem(name, value); }); /** * @summary Get a value * @function * @public * * @param {String} name - name * * @return {Promise<*>} value or undefined * * @example * storage.get('token').then((token) => { * console.log(token) * }); */ const get = (name) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const result = yield store.getItem(name); if (result == null) { return undefined; } if (/^-?\d+\.?\d*$/.test(result)) { return parseFloat(result); } try { return JSON.parse(result); } catch (error) { // do nothing } return result; } catch (err) { if (err.code === 'EACCES') { throw new balena_errors_1.BalenaSettingsPermissionError(err); } return undefined; } }); /** * @summary Check if the value exists * @function * @public * * @param {String} name - name * * @return {Promise<Boolean>} has value * * @example * storage.has('token').then((hasToken) => { * if (hasToken) { * console.log('Yes') * } else { * console.log('No') * }); */ const has = (name) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const value = yield get(name); return value != null; }); /** * @summary Remove a value * @function * @public * * @param {String} name - name * * @return {Promise} * * @example * storage.remove('token') */ const remove = (name) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return store.removeItem(name); }); /** * @summary Remove all values * @function * @public * * * @return {Promise} * * @example * storage.clear() */ const clear = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return store.clear(); }); return { set, get, has, remove, clear }; }; exports.getStorage = getStorage; //# sourceMappingURL=storage.js.map