UNPKG

@commercelayer/sdk-utils

Version:
35 lines (34 loc) 1.46 kB
// src/rate_limit.ts var headerRateLimits = (headers) => { if (!headers) throw new Error("Unable to find rate limit headers in response"); return { interval: Number(headers["x-ratelimit-interval"]), limit: Number(headers["x-ratelimit-limit"]), remaining: Number(headers["x-ratelimit-remaining"]) }; }; var computeRateLimits = (rateLimit, resourceType, allRequests, env) => { const apiEnv = env || "test"; const BURST_INTERVAL = 10 * 1e3; const BURST_MAX_REQUESTS = apiEnv === "live" ? 50 : 25; const resType = typeof resourceType === "string" ? resourceType : resourceType.resourceType; const rli = { limit: rateLimit.limit, interval: rateLimit.interval, delay: 0 }; const allRequests_ = allRequests || rateLimit?.limit * 2 || 0; if (rateLimit) { const totalRequests = typeof allRequests_ === "number" ? allRequests_ : allRequests_.length; rli.requests = totalRequests; if (totalRequests >= rateLimit.limit) rli.delay = Math.ceil(rateLimit.interval * 1e3 / rateLimit.limit); else { const resourceRequests = typeof allRequests_ === "number" ? totalRequests : allRequests_.filter((t) => t.resourceType === resType).length; if (resourceRequests >= BURST_MAX_REQUESTS) rli.delay = Math.ceil(BURST_INTERVAL / BURST_MAX_REQUESTS); } } return rli; }; export { computeRateLimits, headerRateLimits }; //# sourceMappingURL=chunk-5DLBI7WA.js.map //# sourceMappingURL=chunk-5DLBI7WA.js.map