ParseConfig.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  11. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  12. var _decode = _interopRequireDefault(require("./decode"));
  13. var _encode = _interopRequireDefault(require("./encode"));
  14. var _escape2 = _interopRequireDefault(require("./escape"));
  15. var _ParseError = _interopRequireDefault(require("./ParseError"));
  16. var _Storage = _interopRequireDefault(require("./Storage"));
  17. /**
  18. * Copyright (c) 2015-present, Parse, LLC.
  19. * All rights reserved.
  20. *
  21. * This source code is licensed under the BSD-style license found in the
  22. * LICENSE file in the root directory of this source tree. An additional grant
  23. * of patent rights can be found in the PATENTS file in the same directory.
  24. *
  25. * @flow
  26. */
  27. /**
  28. * Parse.Config is a local representation of configuration data that
  29. * can be set from the Parse dashboard.
  30. *
  31. * @alias Parse.Config
  32. */
  33. var ParseConfig =
  34. /*#__PURE__*/
  35. function () {
  36. function ParseConfig() {
  37. (0, _classCallCheck2.default)(this, ParseConfig);
  38. (0, _defineProperty2.default)(this, "attributes", void 0);
  39. (0, _defineProperty2.default)(this, "_escapedAttributes", void 0);
  40. this.attributes = {};
  41. this._escapedAttributes = {};
  42. }
  43. /**
  44. * Gets the value of an attribute.
  45. * @param {String} attr The name of an attribute.
  46. */
  47. (0, _createClass2.default)(ParseConfig, [{
  48. key: "get",
  49. value: function (attr
  50. /*: string*/
  51. )
  52. /*: any*/
  53. {
  54. return this.attributes[attr];
  55. }
  56. /**
  57. * Gets the HTML-escaped value of an attribute.
  58. * @param {String} attr The name of an attribute.
  59. */
  60. }, {
  61. key: "escape",
  62. value: function (attr
  63. /*: string*/
  64. )
  65. /*: string*/
  66. {
  67. var html = this._escapedAttributes[attr];
  68. if (html) {
  69. return html;
  70. }
  71. var val = this.attributes[attr];
  72. var escaped = '';
  73. if (val != null) {
  74. escaped = (0, _escape2.default)(val.toString());
  75. }
  76. this._escapedAttributes[attr] = escaped;
  77. return escaped;
  78. }
  79. /**
  80. * Retrieves the most recently-fetched configuration object, either from
  81. * memory or from local storage if necessary.
  82. *
  83. * @static
  84. * @return {Config} The most recently-fetched Parse.Config if it
  85. * exists, else an empty Parse.Config.
  86. */
  87. }], [{
  88. key: "current",
  89. value: function () {
  90. var controller = _CoreManager.default.getConfigController();
  91. return controller.current();
  92. }
  93. /**
  94. * Gets a new configuration object from the server.
  95. * @static
  96. * @return {Promise} A promise that is resolved with a newly-created
  97. * configuration object when the get completes.
  98. */
  99. }, {
  100. key: "get",
  101. value: function () {
  102. var controller = _CoreManager.default.getConfigController();
  103. return controller.get();
  104. }
  105. /**
  106. * Save value keys to the server.
  107. * @static
  108. * @return {Promise} A promise that is resolved with a newly-created
  109. * configuration object or with the current with the update.
  110. */
  111. }, {
  112. key: "save",
  113. value: function (attrs
  114. /*: { [key: string]: any }*/
  115. ) {
  116. var controller = _CoreManager.default.getConfigController(); //To avoid a mismatch with the local and the cloud config we get a new version
  117. return controller.save(attrs).then(function () {
  118. return controller.get();
  119. }, function (error) {
  120. return Promise.reject(error);
  121. });
  122. }
  123. }]);
  124. return ParseConfig;
  125. }();
  126. var currentConfig = null;
  127. var CURRENT_CONFIG_KEY = 'currentConfig';
  128. function decodePayload(data) {
  129. try {
  130. var json = JSON.parse(data);
  131. if (json && (0, _typeof2.default)(json) === 'object') {
  132. return (0, _decode.default)(json);
  133. }
  134. } catch (e) {
  135. return null;
  136. }
  137. }
  138. var DefaultController = {
  139. current: function () {
  140. if (currentConfig) {
  141. return currentConfig;
  142. }
  143. var config = new ParseConfig();
  144. var storagePath = _Storage.default.generatePath(CURRENT_CONFIG_KEY);
  145. var configData;
  146. if (!_Storage.default.async()) {
  147. configData = _Storage.default.getItem(storagePath);
  148. if (configData) {
  149. var attributes = decodePayload(configData);
  150. if (attributes) {
  151. config.attributes = attributes;
  152. currentConfig = config;
  153. }
  154. }
  155. return config;
  156. } // Return a promise for async storage controllers
  157. return _Storage.default.getItemAsync(storagePath).then(function (configData) {
  158. if (configData) {
  159. var _attributes = decodePayload(configData);
  160. if (_attributes) {
  161. config.attributes = _attributes;
  162. currentConfig = config;
  163. }
  164. }
  165. return config;
  166. });
  167. },
  168. get: function () {
  169. var RESTController = _CoreManager.default.getRESTController();
  170. return RESTController.request('GET', 'config', {}, {}).then(function (response) {
  171. if (!response || !response.params) {
  172. var error = new _ParseError.default(_ParseError.default.INVALID_JSON, 'Config JSON response invalid.');
  173. return Promise.reject(error);
  174. }
  175. var config = new ParseConfig();
  176. config.attributes = {};
  177. for (var attr in response.params) {
  178. config.attributes[attr] = (0, _decode.default)(response.params[attr]);
  179. }
  180. currentConfig = config;
  181. return _Storage.default.setItemAsync(_Storage.default.generatePath(CURRENT_CONFIG_KEY), JSON.stringify(response.params)).then(function () {
  182. return config;
  183. });
  184. });
  185. },
  186. save: function (attrs
  187. /*: { [key: string]: any }*/
  188. ) {
  189. var RESTController = _CoreManager.default.getRESTController();
  190. var encodedAttrs = {};
  191. for (var _key in attrs) {
  192. encodedAttrs[_key] = (0, _encode.default)(attrs[_key]);
  193. }
  194. return RESTController.request('PUT', 'config', {
  195. params: encodedAttrs
  196. }, {
  197. useMasterKey: true
  198. }).then(function (response) {
  199. if (response && response.result) {
  200. return Promise.resolve();
  201. } else {
  202. var error = new _ParseError.default(_ParseError.default.INTERNAL_SERVER_ERROR, 'Error occured updating Config.');
  203. return Promise.reject(error);
  204. }
  205. });
  206. }
  207. };
  208. _CoreManager.default.setConfigController(DefaultController);
  209. var _default = ParseConfig;
  210. exports.default = _default;