owtlab-tracking
Version:
A simple Tracking system
55 lines (48 loc) • 1.34 kB
JavaScript
import 'promise-polyfill/src/polyfill';
import 'whatwg-fetch';
import configDefault from '../config-default.js';
// import Owtlab from '@owtlab/owtlab';
export default function (url, options) {
const config = {
...configDefault,
...(options.retry || {}),
};
const retriesLimit = config.retry.limit;
const retryInitialDelay = config.retry.initialDelay;
const retryOn = config.retry.retryOnResponseStatuses;
let retriesCount = 0;
if (retryOn && !(retryOn instanceof Array)) {
throw {
name: 'ArgumentError',
message: 'retryOn property expects an array',
};
}
return new Promise(((resolve, reject) => {
const wrappedFetch = function (n) {
fetch(url, options)
.then((response) => {
if (retryOn.indexOf(response.status) === -1) {
resolve(response);
} else if (n > 0) {
retry();
} else {
reject(response);
}
})
.catch((error) => {
if (n > 0) {
retry();
} else {
reject(error);
}
});
};
function retry() {
retriesCount += 1;
setTimeout(() => {
wrappedFetch(retriesLimit - retriesCount);
}, 2 ^ (retriesCount * retryInitialDelay));
}
wrappedFetch(retriesLimit - retriesCount);
}));
}