UNPKG

smartystreets-javascript-sdk

Version:
2 lines (1 loc) 907 B
"use strict";var e,t;exports.__require=function(){return t?e:(t=1,e=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}async send(e){let t=await this.inner.send(e);for(let s=0;s<this.maxRetries&&this.statusToRetry.includes(parseInt(t.statusCode));s++){if(parseInt(t.statusCode)===this.statusTooManyRequests){let e=10;if(t.headers){const s=t.headers["Retry-After"];Number.isInteger(s)&&(e=s)}await this.rateLimitBackOff(e)}else await this.backoff(s);t=await this.inner.send(e)}return t}async backoff(e){const t=Math.min(e,this.maxBackoffDuration);console.log(`There was an error processing the request. Retrying in ${t} seconds...`),await this.sleeper.sleep(t)}async rateLimitBackOff(e){console.log(`Rate limit reached. Retrying in ${e} seconds...`),await this.sleeper.sleep(e)}})};