You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import axios from 'axios';
  23. const requestToken = document.getElementsByTagName('head')[0].getAttribute('data-requesttoken');
  24. const tokenHeaders = { headers: { requesttoken: requestToken } };
  25. const sanitize = function(url) {
  26. return url.replace(/\/$/, ''); // Remove last url slash
  27. };
  28. export default {
  29. /**
  30. * This Promise is used to chain a request that require an admin password confirmation
  31. * Since chaining Promise have a very precise behavior concerning catch and then,
  32. * you'll need to be careful when using it.
  33. * e.g
  34. * // store
  35. * action(context) {
  36. * return api.requireAdmin().then((response) => {
  37. * return api.get('url')
  38. * .then((response) => {API success})
  39. * .catch((error) => {API failure});
  40. * }).catch((error) => {requireAdmin failure});
  41. * }
  42. * // vue
  43. * this.$store.dispatch('action').then(() => {always executed})
  44. *
  45. * Since Promise.then().catch().then() will always execute the last then
  46. * this.$store.dispatch('action').then will always be executed
  47. *
  48. * If you want requireAdmin failure to also catch the API request failure
  49. * you will need to throw a new error in the api.get.catch()
  50. *
  51. * e.g
  52. * api.requireAdmin().then((response) => {
  53. * api.get('url')
  54. * .then((response) => {API success})
  55. * .catch((error) => {throw error;});
  56. * }).catch((error) => {requireAdmin OR API failure});
  57. *
  58. * @returns {Promise}
  59. */
  60. requireAdmin() {
  61. return new Promise(function(resolve, reject) {
  62. // TODO: migrate the OC.dialog to Vue and avoid this mess
  63. // wait for password confirmation
  64. let passwordTimeout;
  65. let waitForpassword = function() {
  66. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  67. passwordTimeout = setTimeout(waitForpassword, 500);
  68. return;
  69. }
  70. clearTimeout(passwordTimeout);
  71. clearTimeout(promiseTimeout);
  72. resolve();
  73. };
  74. // automatically reject after 5s if not resolved
  75. let promiseTimeout = setTimeout(() => {
  76. clearTimeout(passwordTimeout);
  77. // close dialog
  78. if (document.getElementsByClassName('oc-dialog-close').length>0) {
  79. document.getElementsByClassName('oc-dialog-close')[0].click();
  80. }
  81. OC.Notification.showTemporary(t('settings', 'You did not enter the password in time'));
  82. reject('Password request cancelled');
  83. }, 7000);
  84. // request password
  85. OC.PasswordConfirmation.requirePasswordConfirmation();
  86. waitForpassword();
  87. });
  88. },
  89. get(url) {
  90. return axios.get(sanitize(url), tokenHeaders)
  91. .then((response) => Promise.resolve(response))
  92. .catch((error) => Promise.reject(error));
  93. },
  94. post(url, data) {
  95. return axios.post(sanitize(url), data, tokenHeaders)
  96. .then((response) => Promise.resolve(response))
  97. .catch((error) => Promise.reject(error));
  98. },
  99. patch(url, data) {
  100. return axios.patch(sanitize(url), data, tokenHeaders)
  101. .then((response) => Promise.resolve(response))
  102. .catch((error) => Promise.reject(error));
  103. },
  104. put(url, data) {
  105. return axios.put(sanitize(url), data, tokenHeaders)
  106. .then((response) => Promise.resolve(response))
  107. .catch((error) => Promise.reject(error));
  108. },
  109. delete(url, data) {
  110. return axios.delete(sanitize(url), { data: data, headers: tokenHeaders.headers })
  111. .then((response) => Promise.resolve(response))
  112. .catch((error) => Promise.reject(error));
  113. }
  114. };