ParseConfig.js 8.0 KB

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