Skip to content

Commit

Permalink
feat: add more apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Dashkevich committed Jun 5, 2024
1 parent e3c913d commit f70f45c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ function initParams(uri, options, callback) {
}

params.callback = callback
params.retry = createXHR.retryManager.createRetry();

if (createXHR.retryManager.getIsEnabled()) {
params.retry = createXHR.retryManager.createRetry();
}

return params
}

Expand All @@ -97,8 +101,20 @@ function _createXHR(options) {
}

// call all registered request interceptors for a given request type:
if (options.requestType) {
options = createXHR.requestInterceptorsStorage.extend(options.requestType, options);
if (options.requestType && createXHR.requestInterceptorsStorage.getIsEnabled()) {
const requestInterceptorPayload = {
uri: options.uri || options.url,
headers: options.headers || {},
body: options.body,
metadata: options.metadata || {},
}

const updatedPayload = createXHR.requestInterceptorsStorage.execute(options.requestType, requestInterceptorPayload);

options.uri = updatedPayload.uri;
options.headers = updatedPayload.headers;
options.body = updatedPayload.body;
options.metadata = updatedPayload.metadata;
}

var retryTimeout = options.retryTimeout;
Expand Down Expand Up @@ -143,10 +159,8 @@ function _createXHR(options) {
}
evt.statusCode = 0

var response = failureResponse

// we would like to retry on error:
if (options.retry && options.retry.shouldRetry) {
if (!aborted && createXHR.retryManager.getIsEnabled() && options.retry && options.retry.shouldRetry) {
options.retryTimeout = setTimeout(function() {
options.retry.moveToNextAttempt();
// we want to re-use the same options and the same xhr object:
Expand All @@ -158,11 +172,21 @@ function _createXHR(options) {
}

// call all registered response interceptors for a given request type:
if (options.requestType) {
response = createXHR.responseInterceptorsStorage.execute(options.requestType, response);
if (options.requestType && createXHR.responseInterceptorsStorage.getIsEnabled()) {
const responseInterceptorPayload = {
headers: failureResponse.headers || {},
body: failureResponse.body,
responseUrl: xhr.responseUrl,
responseType: xhr.responseType,
}

const updatedPayload = createXHR.responseInterceptorsStorage.execute(options.requestType, responseInterceptorPayload);

failureResponse.body = updatedPayload.body;
failureResponse.headers = updatedPayload.headers;
}

return callback(evt, response)
return callback(evt, failureResponse)
}

// will load the data & process the response in a special response object
Expand Down Expand Up @@ -197,8 +221,18 @@ function _createXHR(options) {
}

// call all registered response interceptors for a given request type:
if (options.requestType) {
response = createXHR.responseInterceptorsStorage.execute(options.requestType, response);
if (options.requestType && createXHR.responseInterceptorsStorage.getIsEnabled()) {
const responseInterceptorPayload = {
headers: response.headers || {},
body: response.body,
responseUrl: xhr.responseUrl,
responseType: xhr.responseType,
}

const updatedPayload = createXHR.responseInterceptorsStorage.execute(options.requestType, responseInterceptorPayload);

response.body = updatedPayload.body;
response.headers = updatedPayload.headers;
}

return callback(err, response, response.body)
Expand Down
18 changes: 18 additions & 0 deletions src/interceptors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
class InterceptorsStorage {
constructor() {
this.typeToInterceptorsMap_ = new Map();
this.enabled_ = false;
}

getIsEnabled() {
return this.enabled_;
}

enable() {
this.enabled_ = true;
}

disable() {
this.enabled_ = false;
}

reset() {
this.typeToInterceptorsMap_ = new Map();
this.enabled_ = false;
}

addInterceptor(type, interceptor) {
Expand Down
21 changes: 21 additions & 0 deletions src/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ class RetryManager {
this.delayFactor_ = 0.1;
this.fuzzFactor_ = 0.1;
this.initialDelay_ = 1000;
this.enabled_ = false;
}

getIsEnabled() {
return this.enabled_;
}

enable() {
this.enabled_ = true;
}

disable() {
this.enabled_ = false;
}

reset() {
this.maxAttempts_ = 1;
this.delayFactor_ = 0.1;
this.fuzzFactor_ = 0.1;
this.initialDelay_ = 1000;
this.enabled_ = false;
}

getMaxAttempts() {
Expand Down

0 comments on commit f70f45c

Please sign in to comment.