UNPKG

@vtex/api

Version:
43 lines (42 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cloneAndSanitizeHeaders = exports.getTraceInfo = void 0; const node_error_report_1 = require("@vtex/node-error-report"); function getTraceInfo(span) { var _a, _b; const spanContext = span === null || span === void 0 ? void 0 : span.context(); return { isSampled: (_b = (_a = spanContext === null || spanContext === void 0 ? void 0 : spanContext.isSampled) === null || _a === void 0 ? void 0 : _a.call(spanContext)) !== null && _b !== void 0 ? _b : false, traceId: spanContext === null || spanContext === void 0 ? void 0 : spanContext.toTraceId(), }; } exports.getTraceInfo = getTraceInfo; /** * Do a shallow copy of a headers object and redacts sensitive information. * * @param headersObj The headers object * @param resultFieldsPrefix The prefix that will be added to each field on the result object */ const cloneAndSanitizeHeaders = (headersObj, resultFieldsPrefix = '') => { const ret = {}; const entries = Object.entries(headersObj); for (const [key, val] of entries) { // Most of the time val is a string, but there are some cases, for example when we do a axios interceptor, // that a header field can contain an object, for example: // ``` // "common": { // "Accept": "application/json, text/plain, */*" // }, // "delete": { }, // "get": { }, // "head": { }, // "post": { // "Content-Type": "application/x-www-form-urlencoded" // }, // ``` // In those corner cases we probably won't have a sensitive string as key, so we don't treat them here ret[`${resultFieldsPrefix}${key}`] = node_error_report_1.authFields.includes(key) ? (0, node_error_report_1.sanitizeAuth)(val) : val; } return ret; }; exports.cloneAndSanitizeHeaders = cloneAndSanitizeHeaders;