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 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 'nextcloud-axios'
  23. import confirmPassword from 'nextcloud-password-confirmation'
  24. const sanitize = function(url) {
  25. return url.replace(/\/$/, '') // Remove last url slash
  26. }
  27. export default {
  28. /**
  29. * This Promise is used to chain a request that require an admin password confirmation
  30. * Since chaining Promise have a very precise behavior concerning catch and then,
  31. * you'll need to be careful when using it.
  32. * e.g
  33. * // store
  34. * action(context) {
  35. * return api.requireAdmin().then((response) => {
  36. * return api.get('url')
  37. * .then((response) => {API success})
  38. * .catch((error) => {API failure});
  39. * }).catch((error) => {requireAdmin failure});
  40. * }
  41. * // vue
  42. * this.$store.dispatch('action').then(() => {always executed})
  43. *
  44. * Since Promise.then().catch().then() will always execute the last then
  45. * this.$store.dispatch('action').then will always be executed
  46. *
  47. * If you want requireAdmin failure to also catch the API request failure
  48. * you will need to throw a new error in the api.get.catch()
  49. *
  50. * e.g
  51. * api.requireAdmin().then((response) => {
  52. * api.get('url')
  53. * .then((response) => {API success})
  54. * .catch((error) => {throw error;});
  55. * }).catch((error) => {requireAdmin OR API failure});
  56. *
  57. * @returns {Promise}
  58. */
  59. requireAdmin() {
  60. return confirmPassword()
  61. },
  62. get(url) {
  63. return axios.get(sanitize(url))
  64. },
  65. post(url, data) {
  66. return axios.post(sanitize(url), data)
  67. },
  68. patch(url, data) {
  69. return axios.patch(sanitize(url), data)
  70. },
  71. put(url, data) {
  72. return axios.put(sanitize(url), data)
  73. },
  74. delete(url, data) {
  75. return axios.delete(sanitize(url), { data: data })
  76. }
  77. }