d2-ui
Version:
136 lines (115 loc) • 4.98 kB
JavaScript
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 _apiApi = require('../api/Api');
var _apiApi2 = _interopRequireDefault(_apiApi);
var _libCheck = require('../lib/check');
/**
* @class UserSettings
*
* @description
* Handles communication with the userSettings endpoint. Can be used to get or save userSettings.
*/
var UserSettings = (function () {
function UserSettings(userSettings) {
var api = arguments.length <= 1 || arguments[1] === undefined ? _apiApi2['default'].getApi() : arguments[1];
_classCallCheck(this, UserSettings);
this.api = api;
if (userSettings) {
Object.assign(this, userSettings);
}
}
/**
* @method all
*
* @returns {Promise} Promise that resolves with the usersettings object from the api.
*
* @description
* Loads all the user settings of current user and returns them as an object from the promise.
* ```js
* d2.currentUser.userSettings.all()
* .then(userSettings => {
* console.log('UI Locale: ' + userSettings.keyUiLocale);
* });
* ```
*/
_createClass(UserSettings, [{
key: 'all',
value: function all() {
return this.api.get('userSettings');
}
/**
* @method get
*
* @param {String} key The identifier of the user 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.currentUser.userSettings.get('keyUiLocale')
* .then(userSettingValue => {
* console.log('UI Locale: ' + userSettingValue);
* });
* ```
*/
}, {
key: 'get',
value: function get(key) {
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)(key)) {
throw new TypeError('A "key" parameter should be specified when calling get() on userSettings');
}
_this.api.get(['userSettings', key].join('/'), undefined, { dataType: 'text' }).then(function (response) {
var value = processValue(response);
// Store the value on the user settings object
_this[key] = value;
if (value) {
resolve(processValue(response));
}
reject(new Error('The requested userSetting has no value or does not exist.'));
});
});
}
/**
* @method set
*
* @param {String} key The identifier of the user setting that should be saved.
* @param {String} value The new value of the user setting.
* @returns {Promise} A promise that will resolve when the new value has been saved, or fail if saving fails.
*
* @description
* ```js
* d2.currentUser.userSettings.set('keyUiLocale', 'fr')
* .then(() => {
* console.log('UI Locale is now "fr");
* });
* ```
*/
}, {
key: 'set',
value: function set(key, value) {
var settingUrl = ['userSettings', key].join('/');
if (value === null || ('' + value).length === 0) {
return this.api['delete'](settingUrl, { dataType: 'text' }).then(this[key] = undefined);
}
return this.api.post(settingUrl, value, { dataType: 'text', contentType: 'text/plain' }).then(this[key] = value);
}
}]);
return UserSettings;
})();
exports['default'] = UserSettings;
module.exports = exports['default'];
//# sourceMappingURL=UserSettings.js.map
;