@segment/analytics-next
Version:
Analytics Next (aka Analytics 2.0) is the latest version of Segment’s JavaScript SDK - enabling you to send your data to any tool without having to learn, test, or use a new API every time.
31 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var fetch_1 = require("../../lib/fetch");
var ratelimit_error_1 = require("./ratelimit-error");
function default_1(config) {
function dispatch(url, body) {
return (0, fetch_1.fetch)(url, {
keepalive: config === null || config === void 0 ? void 0 : config.keepalive,
headers: { 'Content-Type': 'text/plain' },
method: 'post',
body: JSON.stringify(body),
}).then(function (res) {
var _a;
if (res.status >= 500) {
throw new Error("Bad response from server: ".concat(res.status));
}
if (res.status === 429) {
var retryTimeoutStringSecs = (_a = res.headers) === null || _a === void 0 ? void 0 : _a.get('x-ratelimit-reset');
var retryTimeoutMS = retryTimeoutStringSecs
? parseInt(retryTimeoutStringSecs) * 1000
: 5000;
throw new ratelimit_error_1.RateLimitError("Rate limit exceeded: ".concat(res.status), retryTimeoutMS);
}
});
}
return {
dispatch: dispatch,
};
}
exports.default = default_1;
//# sourceMappingURL=fetch-dispatcher.js.map