UNPKG

@binance/sentry-miniapp

Version:

86 lines 3 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.bindRequest = void 0; var tslib_1 = require("tslib"); var core_1 = require("@sentry/core"); function isInstanceOf(wat, base) { try { return wat instanceof base; } catch (_e) { return false; } } function getFetchMethod(fetchArgs) { if (fetchArgs === void 0) { fetchArgs = []; } if ('Request' in global && isInstanceOf(fetchArgs[0], Request) && fetchArgs[0].method) { return String(fetchArgs[0].method).toUpperCase(); } if (fetchArgs[1] && fetchArgs[1].method) { return String(fetchArgs[1].method).toUpperCase(); } return 'GET'; } /** Extract `url` from fetch call arguments */ function getFetchUrl(fetchArgs) { if (fetchArgs === void 0) { fetchArgs = []; } if (typeof fetchArgs[0] === 'string') { return fetchArgs[0]; } return fetchArgs[0].url; } function fetchBreadcrumb(handlerData) { // We only capture complete fetch requests if (!handlerData.endTimestamp) { return; } if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') { // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests) return; } if (handlerData.error) { core_1.getCurrentHub().addBreadcrumb({ category: 'fetch', data: tslib_1.__assign(tslib_1.__assign({}, handlerData.fetchData), { status_code: handlerData.response.statusCode }), // level: , type: 'http', }, { data: handlerData.error, input: handlerData.args, }); } else { core_1.getCurrentHub().addBreadcrumb({ category: 'fetch', data: tslib_1.__assign(tslib_1.__assign({}, handlerData.fetchData), { status_code: handlerData.response.statusCode }), type: 'http', }, { input: handlerData.args, response: handlerData.response.data, }); } } exports.bindRequest = function (originFetch) { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var handlerData = { args: args, fetchData: { method: getFetchMethod(args), url: getFetchUrl(args), }, startTimestamp: Date.now(), }; fetchBreadcrumb(handlerData); return originFetch.apply(global, args).then(function (response) { fetchBreadcrumb(tslib_1.__assign(tslib_1.__assign({}, handlerData), { endTimestamp: Date.now(), response: response })); return response; }, function (error) { fetchBreadcrumb(tslib_1.__assign(tslib_1.__assign({}, handlerData), { endTimestamp: Date.now(), error: error })); throw error; }); }; }; //# sourceMappingURL=bindRequest.js.map