magicbell
Version:
MagicBell API wrapper
37 lines • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withRequestTelemetry = withRequestTelemetry;
const history = [];
let enabled = true;
function trackRequest(request) {
request.startTime = Date.now();
const record = history.pop();
// we still track response time locally, so we can print it in the console
if (!record || !enabled)
return;
request.headers.set('x-magicbell-client-telemetry', JSON.stringify(record));
}
function captureResponse(request, option, response) {
if (!response?.status)
return;
const { startTime } = request;
if (!startTime)
return;
history.push({
id: response.headers.get('x-request-id'),
runtime: Number(response.headers.get('x-runtime')),
duration: Date.now() - startTime,
status: response.status,
});
// returning is a must, otherwise node keeps hanging on the unused response (clone)
// see: https://github.com/sindresorhus/ky-universal/issues/46#issuecomment-1620553766
return response;
}
function withRequestTelemetry(options) {
enabled = options.telemetry !== false;
return {
beforeRequest: [trackRequest],
afterResponse: [captureResponse],
};
}
//# sourceMappingURL=telemetry.js.map