Skip to content

Commit

Permalink
Merge pull request #98 from appbaseio/http-timeout
Browse files Browse the repository at this point in the history
fix: abort request on timeout
  • Loading branch information
mohdashraf010897 authored Jul 21, 2023
2 parents d3bc6b6 + 86b4cc7 commit e849a3f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "appbase-js",
"version": "5.3.3",
"version": "5.3.4",
"main": "dist/appbase-js.cjs.js",
"jsnext:main": "dist/appbase-js.es.js",
"module": "dist/appbase-js.es.js",
Expand Down
6 changes: 6 additions & 0 deletions src/core/request/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ function fetchRequest(args) {
const transformedRequest = Object.assign({}, ts);
const { url } = transformedRequest;
delete transformedRequest.url;

const controller = new AbortController();
const { signal } = controller;

const fetchPromise = fetch(
url || finalURL,
Object.assign({}, transformedRequest, {
Expand All @@ -111,13 +115,15 @@ function fetchRequest(args) {
'x-timestamp': new Date().getTime(),
})
: transformedRequest.headers,
signal, // Attach the abort signal to the fetch request
}),
);

const timeoutPromise = new Promise((_, rejectTP) => {
if (httpRequestTimeout > 0) {
setTimeout(() => {
rejectTP(new Error('Request timeout'));
controller.abort();
}, httpRequestTimeout);
}
});
Expand Down

0 comments on commit e849a3f

Please sign in to comment.