UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

30 lines 1.24 kB
/** * The Retry-After response HTTP header indicates how long the user agent * should wait before making a follow-up request. There are three main cases * this header is used: * * - When sent with a 503 (Service Unavailable) response, this indicates how * long the service is expected to be unavailable. * - When sent with a 429 (Too Many Requests) response, this indicates how * long to wait before making a new request. * - When sent with a redirect response, such as 301 (Moved Permanently), this * indicates the minimum time that the user agent is asked to wait before * issuing the redirected request. * * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After */ export function retryAfterDelay(headers, lastRequestTime) { let retryAfterInMs = 0; if (headers) { const retryAfter = headers["retry-after"]; if (/\d+/.test(retryAfter)) { retryAfterInMs = ~~retryAfter * 1000; } if (typeof retryAfter === "string" && isNaN(new Date(retryAfter).getTime()) === false) { retryAfterInMs = new Date(retryAfter).getTime() - lastRequestTime; } } return retryAfterInMs; } //# sourceMappingURL=retryAfterDelay.js.map