ParseConfig.js 8.3 KB

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