UNPKG

smartystreets-javascript-sdk

Version:
2 lines (1 loc) 1.08 kB
"use strict";var e=require("./node_modules/tslib/tslib.es6.cjs");exports.default=class{constructor(e=5,t,s){this.maxRetries=e,this.statusToRetry=[408,429,500,502,503,504],this.statusTooManyRequests=429,this.maxBackoffDuration=10,this.inner=t,this.sleeper=s}send(t){return e.__awaiter(this,void 0,void 0,function*(){let e=yield this.inner.send(t);for(let s=0;s<this.maxRetries;s++){const i=parseInt(String(e.statusCode));if(!this.statusToRetry.includes(i))break;if(i===this.statusTooManyRequests){let t=10;if(e.headers){const s=e.headers["Retry-After"];Number.isInteger(Number(s))&&(t=Number(s))}yield this.rateLimitBackOff(t)}else yield this.backoff(s);e=yield this.inner.send(t)}return e})}backoff(t){return e.__awaiter(this,void 0,void 0,function*(){const e=Math.min(t,this.maxBackoffDuration);console.log(`There was an error processing the request. Retrying in ${e} seconds...`),yield this.sleeper.sleep(e)})}rateLimitBackOff(t){return e.__awaiter(this,void 0,void 0,function*(){console.log(`Rate limit reached. Retrying in ${t} seconds...`),yield this.sleeper.sleep(t)})}};