@commercelayer/sdk-utils
Version:
Commerce Layer Javascript SDK Utils library
38 lines (36 loc) • 1.52 kB
JavaScript
;
// 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;
};
exports.computeRateLimits = computeRateLimits;
exports.headerRateLimits = headerRateLimits;
//# sourceMappingURL=chunk-WSJGLDZH.cjs.map
//# sourceMappingURL=chunk-WSJGLDZH.cjs.map