From 16d183db99e5d5d2f22228df3a0228cb0791641a Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Tue, 9 Jan 2024 13:00:35 +0100 Subject: [PATCH] Allow AbortController in get requests, rethrow cancel error --- openeo.d.ts | 3 ++- src/connection.js | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/openeo.d.ts b/openeo.d.ts index 12f529e..4907d39 100644 --- a/openeo.d.ts +++ b/openeo.d.ts @@ -2400,11 +2400,12 @@ declare module OpenEO { * @param {string} path * @param {object.} query * @param {string} responseType - Response type according to axios, defaults to `json`. + * @param {?AbortController} [abortController=null] - An AbortController object that can be used to cancel the request. * @returns {Promise} * @throws {Error} * @see https://github.com/axios/axios#request-config */ - protected _get(path: string, query: object, responseType: string): Promise; + protected _get(path: string, query: object, responseType: string, abortController?: AbortController | null): Promise; /** * Sends a POST request. * diff --git a/src/connection.js b/src/connection.js index dc9c20e..1b19cc2 100644 --- a/src/connection.js +++ b/src/connection.js @@ -1104,11 +1104,12 @@ class Connection { * @param {string} path * @param {object.} query * @param {string} responseType - Response type according to axios, defaults to `json`. + * @param {?AbortController} [abortController=null] - An AbortController object that can be used to cancel the request. * @returns {Promise} * @throws {Error} * @see https://github.com/axios/axios#request-config */ - async _get(path, query, responseType) { + async _get(path, query, responseType, abortController = null) { return await this._send({ method: 'get', responseType: responseType, @@ -1117,7 +1118,7 @@ class Connection { // Without timeout connecting with a wrong server url may take forever. timeout: path === '/' ? 5000 : 0, params: query - }); + }, abortController); } /** @@ -1272,6 +1273,9 @@ class Connection { } return response; } catch(error) { + if (axios.isCancel(error)) { + throw error; + } const checkContentType = type => (typeof type === 'string' && type.indexOf('/json') !== -1); const enrichError = (origin, response) => { if (typeof response.message === 'string') {