ParseConfig.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. var _decode = _interopRequireDefault(require("./decode"));
  8. var _encode = _interopRequireDefault(require("./encode"));
  9. var _escape = _interopRequireDefault(require("./escape"));
  10. var _ParseError = _interopRequireDefault(require("./ParseError"));
  11. var _Storage = _interopRequireDefault(require("./Storage"));
  12. function _interopRequireDefault(obj) {
  13. return obj && obj.__esModule ? obj : {
  14. default: obj
  15. };
  16. }
  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. class ParseConfig {
  34. /*:: attributes: { [key: string]: any };*/
  35. /*:: _escapedAttributes: { [key: string]: any };*/
  36. constructor() {
  37. this.attributes = {};
  38. this._escapedAttributes = {};
  39. }
  40. /**
  41. * Gets the value of an attribute.
  42. * @param {String} attr The name of an attribute.
  43. */
  44. get(attr
  45. /*: string*/
  46. )
  47. /*: any*/
  48. {
  49. return this.attributes[attr];
  50. }
  51. /**
  52. * Gets the HTML-escaped value of an attribute.
  53. * @param {String} attr The name of an attribute.
  54. */
  55. escape(attr
  56. /*: string*/
  57. )
  58. /*: string*/
  59. {
  60. const html = this._escapedAttributes[attr];
  61. if (html) {
  62. return html;
  63. }
  64. const val = this.attributes[attr];
  65. let escaped = '';
  66. if (val != null) {
  67. escaped = (0, _escape.default)(val.toString());
  68. }
  69. this._escapedAttributes[attr] = escaped;
  70. return escaped;
  71. }
  72. /**
  73. * Retrieves the most recently-fetched configuration object, either from
  74. * memory or from local storage if necessary.
  75. *
  76. * @static
  77. * @return {Config} The most recently-fetched Parse.Config if it
  78. * exists, else an empty Parse.Config.
  79. */
  80. static current() {
  81. const controller = _CoreManager.default.getConfigController();
  82. return controller.current();
  83. }
  84. /**
  85. * Gets a new configuration object from the server.
  86. * @static
  87. * @return {Promise} A promise that is resolved with a newly-created
  88. * configuration object when the get completes.
  89. */
  90. static get() {
  91. const controller = _CoreManager.default.getConfigController();
  92. return controller.get();
  93. }
  94. /**
  95. * Save value keys to the server.
  96. * @static
  97. * @return {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
  101. /*: { [key: string]: any }*/
  102. ) {
  103. const controller = _CoreManager.default.getConfigController(); //To avoid a mismatch with the local and the cloud config we get a new version
  104. return controller.save(attrs).then(() => {
  105. return controller.get();
  106. }, error => {
  107. return Promise.reject(error);
  108. });
  109. }
  110. }
  111. let currentConfig = null;
  112. const CURRENT_CONFIG_KEY = 'currentConfig';
  113. function decodePayload(data) {
  114. try {
  115. const json = JSON.parse(data);
  116. if (json && typeof json === 'object') {
  117. return (0, _decode.default)(json);
  118. }
  119. } catch (e) {
  120. return null;
  121. }
  122. }
  123. const DefaultController = {
  124. current() {
  125. if (currentConfig) {
  126. return currentConfig;
  127. }
  128. const config = new ParseConfig();
  129. const storagePath = _Storage.default.generatePath(CURRENT_CONFIG_KEY);
  130. let configData;
  131. if (!_Storage.default.async()) {
  132. configData = _Storage.default.getItem(storagePath);
  133. if (configData) {
  134. const attributes = decodePayload(configData);
  135. if (attributes) {
  136. config.attributes = attributes;
  137. currentConfig = config;
  138. }
  139. }
  140. return config;
  141. } // Return a promise for async storage controllers
  142. return _Storage.default.getItemAsync(storagePath).then(configData => {
  143. if (configData) {
  144. const attributes = decodePayload(configData);
  145. if (attributes) {
  146. config.attributes = attributes;
  147. currentConfig = config;
  148. }
  149. }
  150. return config;
  151. });
  152. },
  153. get() {
  154. const RESTController = _CoreManager.default.getRESTController();
  155. return RESTController.request('GET', 'config', {}, {}).then(response => {
  156. if (!response || !response.params) {
  157. const error = new _ParseError.default(_ParseError.default.INVALID_JSON, 'Config JSON response invalid.');
  158. return Promise.reject(error);
  159. }
  160. const config = new ParseConfig();
  161. config.attributes = {};
  162. for (const attr in response.params) {
  163. config.attributes[attr] = (0, _decode.default)(response.params[attr]);
  164. }
  165. currentConfig = config;
  166. return _Storage.default.setItemAsync(_Storage.default.generatePath(CURRENT_CONFIG_KEY), JSON.stringify(response.params)).then(() => {
  167. return config;
  168. });
  169. });
  170. },
  171. save(attrs
  172. /*: { [key: string]: any }*/
  173. ) {
  174. const RESTController = _CoreManager.default.getRESTController();
  175. const encodedAttrs = {};
  176. for (const key in attrs) {
  177. encodedAttrs[key] = (0, _encode.default)(attrs[key]);
  178. }
  179. return RESTController.request('PUT', 'config', {
  180. params: encodedAttrs
  181. }, {
  182. useMasterKey: true
  183. }).then(response => {
  184. if (response && response.result) {
  185. return Promise.resolve();
  186. } else {
  187. const error = new _ParseError.default(_ParseError.default.INTERNAL_SERVER_ERROR, 'Error occured updating Config.');
  188. return Promise.reject(error);
  189. }
  190. });
  191. }
  192. };
  193. _CoreManager.default.setConfigController(DefaultController);
  194. var _default = ParseConfig;
  195. exports.default = _default;