ParseHooks.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import CoreManager from './CoreManager';
  2. import decode from './decode';
  3. import ParseError from './ParseError';
  4. export function getFunctions() {
  5. return CoreManager.getHooksController().get("functions");
  6. }
  7. export function getTriggers() {
  8. return CoreManager.getHooksController().get("triggers");
  9. }
  10. export function getFunction(name) {
  11. return CoreManager.getHooksController().get("functions", name);
  12. }
  13. export function getTrigger(className, triggerName) {
  14. return CoreManager.getHooksController().get("triggers", className, triggerName);
  15. }
  16. export function createFunction(functionName, url) {
  17. return create({
  18. functionName: functionName,
  19. url: url
  20. });
  21. }
  22. export function createTrigger(className, triggerName, url) {
  23. return create({
  24. className: className,
  25. triggerName: triggerName,
  26. url: url
  27. });
  28. }
  29. export function create(hook) {
  30. return CoreManager.getHooksController().create(hook);
  31. }
  32. export function updateFunction(functionName, url) {
  33. return update({
  34. functionName: functionName,
  35. url: url
  36. });
  37. }
  38. export function updateTrigger(className, triggerName, url) {
  39. return update({
  40. className: className,
  41. triggerName: triggerName,
  42. url: url
  43. });
  44. }
  45. export function update(hook) {
  46. return CoreManager.getHooksController().update(hook);
  47. }
  48. export function removeFunction(functionName) {
  49. return remove({
  50. functionName: functionName
  51. });
  52. }
  53. export function removeTrigger(className, triggerName) {
  54. return remove({
  55. className: className,
  56. triggerName: triggerName
  57. });
  58. }
  59. export function remove(hook) {
  60. return CoreManager.getHooksController().remove(hook);
  61. }
  62. const DefaultController = {
  63. get(type, functionName, triggerName) {
  64. let url = "/hooks/" + type;
  65. if (functionName) {
  66. url += "/" + functionName;
  67. if (triggerName) {
  68. url += "/" + triggerName;
  69. }
  70. }
  71. return this.sendRequest("GET", url);
  72. },
  73. create(hook) {
  74. let url;
  75. if (hook.functionName && hook.url) {
  76. url = "/hooks/functions";
  77. } else if (hook.className && hook.triggerName && hook.url) {
  78. url = "/hooks/triggers";
  79. } else {
  80. return Promise.reject({
  81. error: 'invalid hook declaration',
  82. code: 143
  83. });
  84. }
  85. return this.sendRequest("POST", url, hook);
  86. },
  87. remove(hook) {
  88. let url;
  89. if (hook.functionName) {
  90. url = "/hooks/functions/" + hook.functionName;
  91. delete hook.functionName;
  92. } else if (hook.className && hook.triggerName) {
  93. url = "/hooks/triggers/" + hook.className + "/" + hook.triggerName;
  94. delete hook.className;
  95. delete hook.triggerName;
  96. } else {
  97. return Promise.reject({
  98. error: 'invalid hook declaration',
  99. code: 143
  100. });
  101. }
  102. return this.sendRequest("PUT", url, {
  103. "__op": "Delete"
  104. });
  105. },
  106. update(hook) {
  107. let url;
  108. if (hook.functionName && hook.url) {
  109. url = "/hooks/functions/" + hook.functionName;
  110. delete hook.functionName;
  111. } else if (hook.className && hook.triggerName && hook.url) {
  112. url = "/hooks/triggers/" + hook.className + "/" + hook.triggerName;
  113. delete hook.className;
  114. delete hook.triggerName;
  115. } else {
  116. return Promise.reject({
  117. error: 'invalid hook declaration',
  118. code: 143
  119. });
  120. }
  121. return this.sendRequest('PUT', url, hook);
  122. },
  123. sendRequest(method, url, body) {
  124. return CoreManager.getRESTController().request(method, url, body, {
  125. useMasterKey: true
  126. }).then(res => {
  127. const decoded = decode(res);
  128. if (decoded) {
  129. return Promise.resolve(decoded);
  130. }
  131. return Promise.reject(new ParseError(ParseError.INVALID_JSON, 'The server returned an invalid response.'));
  132. });
  133. }
  134. };
  135. CoreManager.setHooksController(DefaultController);