ParseConfig.js 6.8 KB

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