UNPKG

d2-ui

Version:
122 lines (102 loc) 4.55 kB
/** * @module System * * @requires lib/check * @requires api/Api */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var _libCheck = require('../lib/check'); var _apiApi = require('../api/Api'); var _apiApi2 = _interopRequireDefault(_apiApi); /** * @class SystemSettings * * @description * Handles communication with the systemSettings endpoint. Can be used to get or save systemSettings. */ // TODO: Return the values from the local cache if we have not updated it? We could var SystemSettings = (function () { function SystemSettings() { var api = arguments.length <= 0 || arguments[0] === undefined ? _apiApi2['default'].getApi() : arguments[0]; _classCallCheck(this, SystemSettings); this.api = api; } /** * @method all * * @returns {Promise} Promise that resolves with the systemsettings object from the api. * * @description * Loads all the system settings in the system and returns them as an object from the promise. * ```js * d2.system.settings.all() * .then(systemSettings => { * console.log('Analytics was last updated on: ' + systemSettings.keyLastSuccessfulResourceTablesUpdate); * }); * ``` */ _createClass(SystemSettings, [{ key: 'all', value: function all() { return this.api.get('systemSettings'); } /** * @method get * * @param {String} systemSettingsKey The identifier of the system setting that should be retrieved. * @returns {Promise} A promise that resolves with the value or will fail if the value is not available. * * @description * ```js * d2.system.settings.get('keyLastSuccessfulResourceTablesUpdate') * .then(systemSettingsValue => { * console.log('Analytics was last updated on: ' + systemSettingsValue); * }); * ``` */ }, { key: 'get', value: function get(systemSettingsKey) { var _this = this; function processValue(value) { // Attempt to parse the response as JSON. If this fails we return the value as is. try { return JSON.parse(value); } catch (e) { return value; } } return new Promise(function (resolve, reject) { if (!(0, _libCheck.isString)(systemSettingsKey)) { throw new TypeError('A "key" parameter should be specified when calling get() on systemSettings'); } _this.api.get(['systemSettings', systemSettingsKey].join('/'), undefined, { dataType: 'text' }).then(function (response) { var systemSettingValue = processValue(response); if (systemSettingValue) { resolve(processValue(response)); } reject(new Error('The requested systemSetting has no value or does not exist.')); }); }); } }, { key: 'set', value: function set(systemSettingsKey, value) { var settingUrl = ['systemSettings', systemSettingsKey].join('/'); if (value === null || ('' + value).length === 0) { return this.api['delete'](settingUrl, { dataType: 'text' }); } return this.api.post(settingUrl, value, { dataType: 'text', contentType: 'text/plain' }); } }]); return SystemSettings; })(); exports['default'] = SystemSettings; module.exports = exports['default']; //# sourceMappingURL=SystemSettings.js.map