ParseConfig.js 6.9 KB

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