2020-10-04 09:33:17 -04:00
|
|
|
/**
|
2024-05-27 11:39:07 -04:00
|
|
|
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-10-04 09:33:17 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a cancelable axios 'request object'.
|
|
|
|
|
*
|
2021-12-02 12:32:57 -05:00
|
|
|
* @param {Function} request the axios promise request
|
|
|
|
|
* @return {object}
|
2020-10-04 09:33:17 -04:00
|
|
|
*/
|
|
|
|
|
function cancelableRequest(request) {
|
2023-03-31 08:56:11 -04:00
|
|
|
const controller = new AbortController()
|
|
|
|
|
const signal = controller.signal
|
2020-10-04 09:33:17 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the request
|
|
|
|
|
*
|
|
|
|
|
* @param {string} url the url to send the request to
|
2021-12-02 12:32:57 -05:00
|
|
|
* @param {object} [options] optional config for the request
|
2020-10-04 09:33:17 -04:00
|
|
|
*/
|
|
|
|
|
const fetch = async function(url, options) {
|
2023-03-31 08:56:11 -04:00
|
|
|
const response = await request(
|
2020-10-04 09:33:17 -04:00
|
|
|
url,
|
2024-06-24 18:00:31 -04:00
|
|
|
{ signal, ...options },
|
2020-10-04 09:33:17 -04:00
|
|
|
)
|
2023-03-31 08:56:11 -04:00
|
|
|
return response
|
2020-10-04 09:33:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
request: fetch,
|
2023-03-31 08:56:11 -04:00
|
|
|
abort: () => controller.abort(),
|
2020-10-04 09:33:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default cancelableRequest
|