Storage.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
  4. var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
  5. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  6. /**
  7. * Copyright (c) 2015-present, Parse, LLC.
  8. * All rights reserved.
  9. *
  10. * This source code is licensed under the BSD-style license found in the
  11. * LICENSE file in the root directory of this source tree. An additional grant
  12. * of patent rights can be found in the PATENTS file in the same directory.
  13. *
  14. * @flow
  15. */
  16. var Storage = {
  17. async: function ()
  18. /*: boolean*/
  19. {
  20. var controller = _CoreManager.default.getStorageController();
  21. return !!controller.async;
  22. },
  23. getItem: function (path
  24. /*: string*/
  25. )
  26. /*: ?string*/
  27. {
  28. var controller = _CoreManager.default.getStorageController();
  29. if (controller.async === 1) {
  30. throw new Error('Synchronous storage is not supported by the current storage controller');
  31. }
  32. return controller.getItem(path);
  33. },
  34. getItemAsync: function (path
  35. /*: string*/
  36. )
  37. /*: Promise<string>*/
  38. {
  39. var controller = _CoreManager.default.getStorageController();
  40. if (controller.async === 1) {
  41. return controller.getItemAsync(path);
  42. }
  43. return _promise.default.resolve(controller.getItem(path));
  44. },
  45. setItem: function (path
  46. /*: string*/
  47. , value
  48. /*: string*/
  49. )
  50. /*: void*/
  51. {
  52. var controller = _CoreManager.default.getStorageController();
  53. if (controller.async === 1) {
  54. throw new Error('Synchronous storage is not supported by the current storage controller');
  55. }
  56. return controller.setItem(path, value);
  57. },
  58. setItemAsync: function (path
  59. /*: string*/
  60. , value
  61. /*: string*/
  62. )
  63. /*: Promise<void>*/
  64. {
  65. var controller = _CoreManager.default.getStorageController();
  66. if (controller.async === 1) {
  67. return controller.setItemAsync(path, value);
  68. }
  69. return _promise.default.resolve(controller.setItem(path, value));
  70. },
  71. removeItem: function (path
  72. /*: string*/
  73. )
  74. /*: void*/
  75. {
  76. var controller = _CoreManager.default.getStorageController();
  77. if (controller.async === 1) {
  78. throw new Error('Synchronous storage is not supported by the current storage controller');
  79. }
  80. return controller.removeItem(path);
  81. },
  82. removeItemAsync: function (path
  83. /*: string*/
  84. )
  85. /*: Promise<void>*/
  86. {
  87. var controller = _CoreManager.default.getStorageController();
  88. if (controller.async === 1) {
  89. return controller.removeItemAsync(path);
  90. }
  91. return _promise.default.resolve(controller.removeItem(path));
  92. },
  93. getAllKeys: function ()
  94. /*: Array<string>*/
  95. {
  96. var controller = _CoreManager.default.getStorageController();
  97. if (controller.async === 1) {
  98. throw new Error('Synchronous storage is not supported by the current storage controller');
  99. }
  100. return controller.getAllKeys();
  101. },
  102. getAllKeysAsync: function ()
  103. /*: Promise<Array<string>>*/
  104. {
  105. var controller = _CoreManager.default.getStorageController();
  106. if (controller.async === 1) {
  107. return controller.getAllKeysAsync();
  108. }
  109. return _promise.default.resolve(controller.getAllKeys());
  110. },
  111. generatePath: function (path
  112. /*: string*/
  113. )
  114. /*: string*/
  115. {
  116. var _context;
  117. if (!_CoreManager.default.get('APPLICATION_ID')) {
  118. throw new Error('You need to call Moralis.start with an applicationId before using Moralis.');
  119. }
  120. if (typeof path !== 'string') {
  121. throw new Error('Tried to get a Storage path that was not a String.');
  122. }
  123. if (path[0] === '/') {
  124. path = path.substr(1);
  125. }
  126. return (0, _concat.default)(_context = "Parse/".concat(_CoreManager.default.get('APPLICATION_ID'), "/")).call(_context, path);
  127. },
  128. _clear: function () {
  129. var controller = _CoreManager.default.getStorageController();
  130. if (controller.hasOwnProperty('clear')) {
  131. controller.clear();
  132. }
  133. }
  134. };
  135. module.exports = Storage;
  136. _CoreManager.default.setStorageController(require('./StorageController.browser'));