From ac0eeb10cd0a01c6ab694129d51e25c88dc6d874 Mon Sep 17 00:00:00 2001 From: MonsieurDMA Date: Wed, 1 May 2024 19:47:45 +0200 Subject: [PATCH] Add timeout on apiPost request --- src/utils.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 577e35c748e..fdf314fc1bd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -258,9 +258,24 @@ export function apiPost(path: string, data?: any, contentType: string = 'applica if (sId) headers['Authorization'] = sId; } - fetch(`${apiUrl}/${path}`, { method: 'POST', headers: headers, body: data }) - .then(response => resolve(response)) - .catch(err => reject(err)); + + const controller = new AbortController(); + const signal = controller.signal; + + const timeoutId = setTimeout(() => { + controller.abort(); + reject(new Error('Request timed out')); + }, 8000); + + fetch(`${apiUrl}/${path}`, { method: 'POST', headers: headers, body: data, signal }) + .then(response => { + clearTimeout(timeoutId); + resolve(response); + }) + .catch(err => { + clearTimeout(timeoutId); + reject(err); + }); }); }