UNPKG

magicbell

Version:
39 lines 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.error = exports.debug = exports.info = void 0; exports.withRequestLogging = withRequestLogging; const tslib_1 = require("tslib"); const debug_1 = tslib_1.__importDefault(require("debug")); const fetch_addons_1 = require("fetch-addons"); exports.info = (0, debug_1.default)('magicbell:info'); exports.debug = (0, debug_1.default)('magicbell:debug'); exports.error = (0, debug_1.default)('magicbell:error'); function mask(str) { // note, can't use real ellipsis, that'd throw // error: Cannot convert argument to a ByteString because the character at index 4 has a value of 8230 which is greater than 255. return `${str.slice(0, 4)}...${str.slice(-4)}`; } const secretHeaders = ['x-magicbell-api-secret']; // Exclude these headers, as they don't hold valuable info for debugging // and we don't want these values to be replayed when users copy/paste the curl // command. It would potentially mess up our telemetry. const excludeHeaders = ['user-agent', 'x-magicbell-client-user-agent', 'x-magicbell-client-telemetry']; function withRequestLogging() { return { beforeRequest: [(request) => (0, exports.debug)(`${request.method} ${request.url}`)], beforeError: [ async (err) => { const req = err.request.clone(); excludeHeaders.forEach((header) => req.headers.delete(header)); req.headers.forEach((value, key) => { if (secretHeaders.includes(key)) req.headers.set(key, mask(value)); }); const curl = await (0, fetch_addons_1.toCurl)(req).then((x) => x.replace(/\\\n/g, ' ').replace(/\s+/g, ' ')); (0, exports.error)(`${err.message}: ${curl}`); return err; }, ], }; } //# sourceMappingURL=log.js.map