ParseConfig.js 6.5 KB

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