UNPKG

6.32 kBJavaScriptView Raw
1import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
2import { addInstrumentationHandler, getGlobalObject, logger, SyncPromise } from '@sentry/utils';
3import { BrowserClient } from './client';
4import { wrap as internalWrap } from './helpers';
5import { Breadcrumbs, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';
6export var defaultIntegrations = [
7 new CoreIntegrations.InboundFilters(),
8 new CoreIntegrations.FunctionToString(),
9 new TryCatch(),
10 new Breadcrumbs(),
11 new GlobalHandlers(),
12 new LinkedErrors(),
13 new UserAgent(),
14];
15/**
16 * The Sentry Browser SDK Client.
17 *
18 * To use this SDK, call the {@link init} function as early as possible when
19 * loading the web page. To set context information or send manual events, use
20 * the provided methods.
21 *
22 * @example
23 *
24 * ```
25 *
26 * import { init } from '@sentry/browser';
27 *
28 * init({
29 * dsn: '__DSN__',
30 * // ...
31 * });
32 * ```
33 *
34 * @example
35 * ```
36 *
37 * import { configureScope } from '@sentry/browser';
38 * configureScope((scope: Scope) => {
39 * scope.setExtra({ battery: 0.7 });
40 * scope.setTag({ user_mode: 'admin' });
41 * scope.setUser({ id: '4711' });
42 * });
43 * ```
44 *
45 * @example
46 * ```
47 *
48 * import { addBreadcrumb } from '@sentry/browser';
49 * addBreadcrumb({
50 * message: 'My Breadcrumb',
51 * // ...
52 * });
53 * ```
54 *
55 * @example
56 *
57 * ```
58 *
59 * import * as Sentry from '@sentry/browser';
60 * Sentry.captureMessage('Hello, world!');
61 * Sentry.captureException(new Error('Good bye'));
62 * Sentry.captureEvent({
63 * message: 'Manual',
64 * stacktrace: [
65 * // ...
66 * ],
67 * });
68 * ```
69 *
70 * @see {@link BrowserOptions} for documentation on configuration options.
71 */
72export function init(options) {
73 if (options === void 0) { options = {}; }
74 if (options.defaultIntegrations === undefined) {
75 options.defaultIntegrations = defaultIntegrations;
76 }
77 if (options.release === undefined) {
78 var window_1 = getGlobalObject();
79 // This supports the variable that sentry-webpack-plugin injects
80 if (window_1.SENTRY_RELEASE && window_1.SENTRY_RELEASE.id) {
81 options.release = window_1.SENTRY_RELEASE.id;
82 }
83 }
84 if (options.autoSessionTracking === undefined) {
85 options.autoSessionTracking = true;
86 }
87 initAndBind(BrowserClient, options);
88 if (options.autoSessionTracking) {
89 startSessionTracking();
90 }
91}
92/**
93 * Present the user with a report dialog.
94 *
95 * @param options Everything is optional, we try to fetch all info need from the global scope.
96 */
97export function showReportDialog(options) {
98 if (options === void 0) { options = {}; }
99 if (!options.eventId) {
100 options.eventId = getCurrentHub().lastEventId();
101 }
102 var client = getCurrentHub().getClient();
103 if (client) {
104 client.showReportDialog(options);
105 }
106}
107/**
108 * This is the getter for lastEventId.
109 *
110 * @returns The last event id of a captured event.
111 */
112export function lastEventId() {
113 return getCurrentHub().lastEventId();
114}
115/**
116 * This function is here to be API compatible with the loader.
117 * @hidden
118 */
119export function forceLoad() {
120 // Noop
121}
122/**
123 * This function is here to be API compatible with the loader.
124 * @hidden
125 */
126export function onLoad(callback) {
127 callback();
128}
129/**
130 * A promise that resolves when all current events have been sent.
131 * If you provide a timeout and the queue takes longer to drain the promise returns false.
132 *
133 * @param timeout Maximum time in ms the client should wait.
134 */
135export function flush(timeout) {
136 var client = getCurrentHub().getClient();
137 if (client) {
138 return client.flush(timeout);
139 }
140 return SyncPromise.reject(false);
141}
142/**
143 * A promise that resolves when all current events have been sent.
144 * If you provide a timeout and the queue takes longer to drain the promise returns false.
145 *
146 * @param timeout Maximum time in ms the client should wait.
147 */
148export function close(timeout) {
149 var client = getCurrentHub().getClient();
150 if (client) {
151 return client.close(timeout);
152 }
153 return SyncPromise.reject(false);
154}
155/**
156 * Wrap code within a try/catch block so the SDK is able to capture errors.
157 *
158 * @param fn A function to wrap.
159 *
160 * @returns The result of wrapped function call.
161 */
162// eslint-disable-next-line @typescript-eslint/no-explicit-any
163export function wrap(fn) {
164 return internalWrap(fn)();
165}
166/**
167 * Enable automatic Session Tracking for the initial page load.
168 */
169function startSessionTracking() {
170 var window = getGlobalObject();
171 var document = window.document;
172 if (typeof document === 'undefined') {
173 logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
174 return;
175 }
176 var hub = getCurrentHub();
177 // The only way for this to be false is for there to be a version mismatch between @sentry/browser (>= 6.0.0) and
178 // @sentry/hub (< 5.27.0). In the simple case, there won't ever be such a mismatch, because the two packages are
179 // pinned at the same version in package.json, but there are edge cases where it's possible. See
180 // https://github.com/getsentry/sentry-javascript/issues/3207 and
181 // https://github.com/getsentry/sentry-javascript/issues/3234 and
182 // https://github.com/getsentry/sentry-javascript/issues/3278.
183 if (typeof hub.startSession !== 'function' || typeof hub.captureSession !== 'function') {
184 return;
185 }
186 // The session duration for browser sessions does not track a meaningful
187 // concept that can be used as a metric.
188 // Automatically captured sessions are akin to page views, and thus we
189 // discard their duration.
190 hub.startSession({ ignoreDuration: true });
191 hub.captureSession();
192 // We want to create a session for every navigation as well
193 addInstrumentationHandler({
194 callback: function (_a) {
195 var from = _a.from, to = _a.to;
196 // Don't create an additional session for the initial route or if the location did not change
197 if (from === undefined || from === to) {
198 return;
199 }
200 hub.startSession({ ignoreDuration: true });
201 hub.captureSession();
202 },
203 type: 'history',
204 });
205}
206//# sourceMappingURL=sdk.js.map
\No newline at end of file