summaryrefslogtreecommitdiffstats
path: root/apps/comments/src/utils/cancelableRequest.js
blob: 47baa5050831c7f19e0b756977d73c52fb2990b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

/**
 * Creates a cancelable axios 'request object'.
 *
 * @param {Function} request the axios promise request
 * @return {object}
 */
const cancelableRequest = function(request) {
	const controller = new AbortController()
	const signal = controller.signal

	/**
	 * Execute the request
	 *
	 * @param {string} url the url to send the request to
	 * @param {object} [options] optional config for the request
	 */
	const fetch = async function(url, options) {
		const response = await request(
			url,
			Object.assign({ signal }, options)
		)
		return response
	}

	return {
		request: fetch,
		abort: () => controller.abort(),
	}
}

export default cancelableRequest