ParseConfig.js 6.1 KB

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