Skip to content

Commit

Permalink
Merge pull request #96 from appbaseio/http-timeout
Browse files Browse the repository at this point in the history
feat: add httpTimeoutRequest to rsv3 calls
  • Loading branch information
mohdashraf010897 authored Jul 12, 2023
2 parents 30dfaf6 + ea23cab commit 4597786
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
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.2.0",
"version": "5.3.3",
"main": "dist/appbase-js.cjs.js",
"jsnext:main": "dist/appbase-js.es.js",
"module": "dist/appbase-js.es.js",
Expand Down
1 change: 1 addition & 0 deletions src/core/api/reactiveSearchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function reactiveSearchApi(query, settings, params) {
isRSAPI: true,
isMongoRequest: !!this.mongodb,
params,
httpRequestTimeout: this.httpRequestTimeout || 0,
});
}
export default reactiveSearchApi;
1 change: 1 addition & 0 deletions src/core/api/reactiveSearchv3Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function reactiveSearchv3Api(query, settings, params) {
isRSAPI: true,
isMongoRequest: !!this.mongodb,
params,
httpRequestTimeout: this.httpRequestTimeout || 0,
});
}
export default reactiveSearchv3Api;
5 changes: 5 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import SCHEMA from '../utils/schema/index';
* @param {Boolean} config.enableTelemetry
* @param {Object} config.mongodb
* @param {Object} config.endpoint
* @param {Object} config.httpRequestTimeout
* A callback function which will be invoked before a fetch request made
*/
function AppBase(config) {
Expand Down Expand Up @@ -93,6 +94,10 @@ function AppBase(config) {
this.mongodb = config.mongodb;
}

if (config.httpRequestTimeout) {
this.httpRequestTimeout = config.httpRequestTimeout;
}

if (typeof config.enableTelemetry === 'boolean') {
this.enableTelemetry = config.enableTelemetry;
}
Expand Down
29 changes: 21 additions & 8 deletions src/core/request/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { btoa, removeUndefined } from '../../utils/index';
* @param {Object} args.body
* @param {Object} args.headers
* @param {boolean} args.isSuggestionsAPI
* @param {number} args.httpRequestTimeout - Timeout duration in milliseconds
*/
function fetchRequest(args) {
return new Promise((resolve, reject) => {
Expand All @@ -24,6 +25,7 @@ function fetchRequest(args) {
isRSAPI,
isSuggestionsAPI,
isMongoRequest = false,
httpRequestTimeout = 0, // Default timeout is 0 (no timeout)
} = parsedArgs;
const app = isSuggestionsAPI ? '.suggestions' : this.app;
let bodyCopy = body;
Expand Down Expand Up @@ -68,10 +70,10 @@ function fetchRequest(args) {
this.transformRequest
&& typeof this.transformRequest === 'function'
) {
const tarnsformRequestPromise = this.transformRequest(res);
return tarnsformRequestPromise instanceof Promise
? tarnsformRequestPromise
: Promise.resolve(tarnsformRequestPromise);
const transformRequestPromise = this.transformRequest(res);
return transformRequestPromise instanceof Promise
? transformRequestPromise
: Promise.resolve(transformRequestPromise);
}
return Promise.resolve(res);
};
Expand Down Expand Up @@ -99,7 +101,7 @@ function fetchRequest(args) {
const transformedRequest = Object.assign({}, ts);
const { url } = transformedRequest;
delete transformedRequest.url;
return fetch(
const fetchPromise = fetch(
url || finalURL,
Object.assign({}, transformedRequest, {
// apply timestamp header for RS API
Expand All @@ -110,7 +112,17 @@ function fetchRequest(args) {
})
: transformedRequest.headers,
}),
)
);

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

return Promise.race([fetchPromise, timeoutPromise])
.then((res) => {
if (res.status >= 500) {
return reject(res);
Expand Down Expand Up @@ -151,7 +163,7 @@ function fetchRequest(args) {
}
});
}
// reject only when all responses has error
// reject only when all responses have an error
if (
errorResponses > 0
&& allResponses === errorResponses
Expand All @@ -165,7 +177,7 @@ function fetchRequest(args) {
if (data && data.responses instanceof Array) {
const allResponses = data.responses.length;
const errorResponses = data.responses.filter(entry => Object.prototype.hasOwnProperty.call(entry, 'error')).length;
// reject only when all responses has error
// reject only when all responses have an error
if (allResponses === errorResponses) {
return reject(data);
}
Expand All @@ -186,4 +198,5 @@ function fetchRequest(args) {
}
});
}

export default fetchRequest;

0 comments on commit 4597786

Please sign in to comment.