ParseConfig.js 6.8 KB

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