smartystreets-javascript-sdk
Version:
Quick and easy Smarty address validation.
2 lines (1 loc) • 1.58 kB
JavaScript
import{__awaiter as e,__generator as t}from"./node_modules/tslib/tslib.es6.mjs";var s=function(){function s(e,t,s){void 0===e&&(e=5),this.maxRetries=e,this.statusToRetry=[408,429,500,502,503,504],this.statusTooManyRequests=429,this.maxBackoffDuration=10,this.inner=t,this.sleeper=s}return s.prototype.send=function(s){return e(this,void 0,void 0,(function(){var e,r,n,i,a;return t(this,(function(t){switch(t.label){case 0:return[4,this.inner.send(s)];case 1:e=t.sent(),r=0,t.label=2;case 2:return r<this.maxRetries?(n=parseInt(String(e.statusCode)),this.statusToRetry.includes(n)?n!==this.statusTooManyRequests?[3,4]:(i=10,e.headers&&(a=e.headers["Retry-After"],Number.isInteger(Number(a))&&(i=Number(a))),[4,this.rateLimitBackOff(i)]):[3,9]):[3,9];case 3:return t.sent(),[3,6];case 4:return[4,this.backoff(r)];case 5:t.sent(),t.label=6;case 6:return[4,this.inner.send(s)];case 7:e=t.sent(),t.label=8;case 8:return r++,[3,2];case 9:return[2,e]}}))}))},s.prototype.backoff=function(s){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return e=Math.min(s,this.maxBackoffDuration),console.log("There was an error processing the request. Retrying in ".concat(e," seconds...")),[4,this.sleeper.sleep(e)];case 1:return t.sent(),[2]}}))}))},s.prototype.rateLimitBackOff=function(s){return e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return console.log("Rate limit reached. Retrying in ".concat(s," seconds...")),[4,this.sleeper.sleep(s)];case 1:return e.sent(),[2]}}))}))},s}();export{s as default};