ParseConfig.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. import CoreManager from './CoreManager';
  12. import decode from './decode';
  13. import encode from './encode';
  14. import escape from './escape';
  15. import ParseError from './ParseError';
  16. import Storage from './Storage';
  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. /*:: attributes: { [key: string]: any };*/
  25. /*:: _escapedAttributes: { [key: string]: any };*/
  26. constructor() {
  27. this.attributes = {};
  28. this._escapedAttributes = {};
  29. }
  30. /**
  31. * Gets the value of an attribute.
  32. * @param {String} attr The name of an attribute.
  33. */
  34. get(attr
  35. /*: string*/
  36. )
  37. /*: any*/
  38. {
  39. return this.attributes[attr];
  40. }
  41. /**
  42. * Gets the HTML-escaped value of an attribute.
  43. * @param {String} attr The name of an attribute.
  44. */
  45. escape(attr
  46. /*: string*/
  47. )
  48. /*: string*/
  49. {
  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 = escape(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. * @return {Config} The most recently-fetched Parse.Config if it
  68. * exists, else an empty Parse.Config.
  69. */
  70. static current() {
  71. const controller = CoreManager.getConfigController();
  72. return controller.current();
  73. }
  74. /**
  75. * Gets a new configuration object from the server.
  76. * @static
  77. * @return {Promise} A promise that is resolved with a newly-created
  78. * configuration object when the get completes.
  79. */
  80. static get() {
  81. const controller = CoreManager.getConfigController();
  82. return controller.get();
  83. }
  84. /**
  85. * Save value keys to the server.
  86. * @static
  87. * @return {Promise} A promise that is resolved with a newly-created
  88. * configuration object or with the current with the update.
  89. */
  90. static save(attrs
  91. /*: { [key: string]: any }*/
  92. ) {
  93. const controller = CoreManager.getConfigController(); //To avoid a mismatch with the local and the cloud config we get a new version
  94. return controller.save(attrs).then(() => {
  95. return controller.get();
  96. }, error => {
  97. return Promise.reject(error);
  98. });
  99. }
  100. }
  101. let currentConfig = null;
  102. const CURRENT_CONFIG_KEY = 'currentConfig';
  103. function decodePayload(data) {
  104. try {
  105. const json = JSON.parse(data);
  106. if (json && typeof json === 'object') {
  107. return decode(json);
  108. }
  109. } catch (e) {
  110. return null;
  111. }
  112. }
  113. const DefaultController = {
  114. current() {
  115. if (currentConfig) {
  116. return currentConfig;
  117. }
  118. const config = new ParseConfig();
  119. const storagePath = Storage.generatePath(CURRENT_CONFIG_KEY);
  120. let configData;
  121. if (!Storage.async()) {
  122. configData = Storage.getItem(storagePath);
  123. if (configData) {
  124. const attributes = decodePayload(configData);
  125. if (attributes) {
  126. config.attributes = attributes;
  127. currentConfig = config;
  128. }
  129. }
  130. return config;
  131. } // Return a promise for async storage controllers
  132. return Storage.getItemAsync(storagePath).then(configData => {
  133. if (configData) {
  134. const attributes = decodePayload(configData);
  135. if (attributes) {
  136. config.attributes = attributes;
  137. currentConfig = config;
  138. }
  139. }
  140. return config;
  141. });
  142. },
  143. get() {
  144. const RESTController = CoreManager.getRESTController();
  145. return RESTController.request('GET', 'config', {}, {}).then(response => {
  146. if (!response || !response.params) {
  147. const error = new ParseError(ParseError.INVALID_JSON, 'Config JSON response invalid.');
  148. return Promise.reject(error);
  149. }
  150. const config = new ParseConfig();
  151. config.attributes = {};
  152. for (const attr in response.params) {
  153. config.attributes[attr] = decode(response.params[attr]);
  154. }
  155. currentConfig = config;
  156. return Storage.setItemAsync(Storage.generatePath(CURRENT_CONFIG_KEY), JSON.stringify(response.params)).then(() => {
  157. return config;
  158. });
  159. });
  160. },
  161. save(attrs
  162. /*: { [key: string]: any }*/
  163. ) {
  164. const RESTController = CoreManager.getRESTController();
  165. const encodedAttrs = {};
  166. for (const key in attrs) {
  167. encodedAttrs[key] = encode(attrs[key]);
  168. }
  169. return RESTController.request('PUT', 'config', {
  170. params: encodedAttrs
  171. }, {
  172. useMasterKey: true
  173. }).then(response => {
  174. if (response && response.result) {
  175. return Promise.resolve();
  176. } else {
  177. const error = new ParseError(ParseError.INTERNAL_SERVER_ERROR, 'Error occured updating Config.');
  178. return Promise.reject(error);
  179. }
  180. });
  181. }
  182. };
  183. CoreManager.setConfigController(DefaultController);
  184. export default ParseConfig;