@rollbar/react
Version:
Effortlessly track and debug errors in your React applications with Rollbar. This package includes advanced error tracking features and a set of React-specific enhancements to help you identify and fix issues more quickly.
90 lines (86 loc) • 3.67 kB
JavaScript
"use client";
import { defineProperty as _defineProperty, typeof as _typeof, inherits as _inherits, createClass as _createClass, classCallCheck as _classCallCheck, callSuper as _callSuper } from './_virtual/_rollupPluginBabelHelpers.js';
import React, { createContext, Component } from 'react';
import PropTypes from 'prop-types';
import Rollbar from 'rollbar';
import invariant from './external/tiny-invariant/dist/esm/tiny-invariant.js';
import { isRollbarInstance } from './utils.js';
var Context = /*#__PURE__*/createContext();
Context.displayName = 'Rollbar';
var RollbarInstance = Symbol('RollbarInstance');
var BaseOptions = Symbol('BaseOptions');
var RollbarCtor = Symbol('RollbarCtor');
function getRollbarFromContext(context) {
var rollbar = context[RollbarInstance];
return rollbar;
}
function getRollbarConstructorFromContext(context) {
var ctor = context[RollbarCtor];
return ctor;
}
var Provider = /*#__PURE__*/function (_Component) {
_inherits(Provider, _Component);
function Provider(props) {
var _this;
_classCallCheck(this, Provider);
_this = _callSuper(this, Provider, [props]);
var _this$props = _this.props,
config = _this$props.config,
_this$props$Rollbar = _this$props.Rollbar,
ctor = _this$props$Rollbar === void 0 ? Rollbar : _this$props$Rollbar,
instance = _this$props.instance;
invariant(!instance || isRollbarInstance(instance), '`instance` must be a configured instance of Rollbar');
var options = typeof config === 'function' ? config() : config;
var rollbar = instance || new ctor(options);
// TODO: use isUncaught to filter if this is 2nd Provider added
// unless customer wants that
_this.state = {
rollbar: rollbar,
options: options
};
return _this;
}
// componentDidUpdate()
_createClass(Provider, [{
key: "render",
value: function render() {
var _this$props2 = this.props,
children = _this$props2.children,
_this$props2$Rollbar = _this$props2.Rollbar,
ctor = _this$props2$Rollbar === void 0 ? Rollbar : _this$props2$Rollbar;
var _this$state = this.state,
rollbar = _this$state.rollbar,
options = _this$state.options;
return /*#__PURE__*/React.createElement(Context.Provider, {
value: _defineProperty(_defineProperty(_defineProperty({}, RollbarInstance, rollbar), BaseOptions, options), RollbarCtor, ctor)
}, children);
}
}]);
return Provider;
}(Component);
_defineProperty(Provider, "propTypes", {
Rollbar: PropTypes.func,
config: function config(props, propName, componentName) {
if (!props.config && !props.instance) {
return new Error("One of the required props 'config' or 'instance' must be set for ".concat(componentName, "."));
}
if (props.config) {
var configType = _typeof(props.config);
if (configType === 'function' || configType === 'object' && !Array.isArray(configType)) {
return;
}
return new Error("".concat(propName, " must be either an Object or a Function"));
}
},
instance: function instance(props, propName, componentName) {
if (!props.config && !props.instance) {
return new Error("One of the required props 'config' or 'instance' must be set for ".concat(componentName, "."));
}
if (props.instance && !isRollbarInstance(props.instance)) {
return new Error("".concat(propName, " must be a configured instance of Rollbar"));
}
},
children: PropTypes.node
});
export { BaseOptions, Context, Provider, RollbarCtor, RollbarInstance, getRollbarConstructorFromContext, getRollbarFromContext };
//# sourceMappingURL=Provider.js.map