2018-11-12 13:54:56 +03:00
|
|
|
/**
|
2018-06-12 11:23:00 +03:00
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-10-04 13:17:09 +03:00
|
|
|
import axios from '@nextcloud/axios'
|
2020-03-22 12:46:04 +03:00
|
|
|
import confirmPassword from '@nextcloud/password-confirmation'
|
2018-03-09 19:46:34 +03:00
|
|
|
|
|
|
|
const sanitize = function(url) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return url.replace(/\/$/, '') // Remove last url slash
|
|
|
|
}
|
2018-03-09 19:46:34 +03:00
|
|
|
|
|
|
|
export default {
|
2018-04-09 12:03:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This Promise is used to chain a request that require an admin password confirmation
|
|
|
|
* Since chaining Promise have a very precise behavior concerning catch and then,
|
|
|
|
* you'll need to be careful when using it.
|
|
|
|
* e.g
|
|
|
|
* // store
|
2020-05-08 11:01:54 +03:00
|
|
|
* action(context) {
|
|
|
|
* return api.requireAdmin().then((response) => {
|
|
|
|
* return api.get('url')
|
|
|
|
* .then((response) => {API success})
|
|
|
|
* .catch((error) => {API failure});
|
|
|
|
* }).catch((error) => {requireAdmin failure});
|
|
|
|
* }
|
2018-04-09 12:03:21 +03:00
|
|
|
* // vue
|
2020-05-08 11:01:54 +03:00
|
|
|
* this.$store.dispatch('action').then(() => {always executed})
|
2018-04-09 12:03:21 +03:00
|
|
|
*
|
|
|
|
* Since Promise.then().catch().then() will always execute the last then
|
|
|
|
* this.$store.dispatch('action').then will always be executed
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
2018-04-09 12:03:21 +03:00
|
|
|
* If you want requireAdmin failure to also catch the API request failure
|
|
|
|
* you will need to throw a new error in the api.get.catch()
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
2018-04-09 12:03:21 +03:00
|
|
|
* e.g
|
2020-05-08 11:01:54 +03:00
|
|
|
* api.requireAdmin().then((response) => {
|
|
|
|
* api.get('url')
|
|
|
|
* .then((response) => {API success})
|
|
|
|
* .catch((error) => {throw error;});
|
|
|
|
* }).catch((error) => {requireAdmin OR API failure});
|
2019-09-25 19:19:42 +03:00
|
|
|
*
|
2018-04-14 11:34:40 +03:00
|
|
|
* @returns {Promise}
|
2018-04-09 12:03:21 +03:00
|
|
|
*/
|
|
|
|
requireAdmin() {
|
2019-09-25 19:19:42 +03:00
|
|
|
return confirmPassword()
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
2020-12-16 12:41:41 +03:00
|
|
|
get(url, options) {
|
|
|
|
return axios.get(sanitize(url), options)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
post(url, data) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return axios.post(sanitize(url), data)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
patch(url, data) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return axios.patch(sanitize(url), data)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
put(url, data) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return axios.put(sanitize(url), data)
|
2018-04-09 12:03:21 +03:00
|
|
|
},
|
|
|
|
delete(url, data) {
|
2020-10-08 09:11:13 +03:00
|
|
|
return axios.delete(sanitize(url), { params: data })
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|