Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: abort request on timeout #98

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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