UNPKG

2.83 kBJavaScriptView Raw
1import { urlEncode, makeDsn, dsnToString } from '@sentry/utils';
2
3var SENTRY_API_VERSION = '7';
4
5/** Returns the prefix to construct Sentry ingestion API endpoints. */
6function getBaseApiEndpoint(dsn) {
7 var protocol = dsn.protocol ? `${dsn.protocol}:` : '';
8 var port = dsn.port ? `:${dsn.port}` : '';
9 return `${protocol}//${dsn.host}${port}${dsn.path ? `/${dsn.path}` : ''}/api/`;
10}
11
12/** Returns the ingest API endpoint for target. */
13function _getIngestEndpoint(dsn) {
14 return `${getBaseApiEndpoint(dsn)}${dsn.projectId}/envelope/`;
15}
16
17/** Returns a URL-encoded string with auth config suitable for a query string. */
18function _encodedAuth(dsn, sdkInfo) {
19 return urlEncode({
20 // We send only the minimum set of required information. See
21 // https://github.com/getsentry/sentry-javascript/issues/2572.
22 sentry_key: dsn.publicKey,
23 sentry_version: SENTRY_API_VERSION,
24 ...(sdkInfo && { sentry_client: `${sdkInfo.name}/${sdkInfo.version}` }),
25 });
26}
27
28/**
29 * Returns the envelope endpoint URL with auth in the query string.
30 *
31 * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
32 */
33function getEnvelopeEndpointWithUrlEncodedAuth(
34 dsn,
35 // TODO (v8): Remove `tunnelOrOptions` in favor of `options`, and use the substitute code below
36 // options: ClientOptions = {} as ClientOptions,
37 tunnelOrOptions = {} ,
38) {
39 // TODO (v8): Use this code instead
40 // const { tunnel, _metadata = {} } = options;
41 // return tunnel ? tunnel : `${_getIngestEndpoint(dsn)}?${_encodedAuth(dsn, _metadata.sdk)}`;
42
43 var tunnel = typeof tunnelOrOptions === 'string' ? tunnelOrOptions : tunnelOrOptions.tunnel;
44 var sdkInfo =
45 typeof tunnelOrOptions === 'string' || !tunnelOrOptions._metadata ? undefined : tunnelOrOptions._metadata.sdk;
46
47 return tunnel ? tunnel : `${_getIngestEndpoint(dsn)}?${_encodedAuth(dsn, sdkInfo)}`;
48}
49
50/** Returns the url to the report dialog endpoint. */
51function getReportDialogEndpoint(
52 dsnLike,
53 dialogOptions
54
55,
56) {
57 var dsn = makeDsn(dsnLike);
58 var endpoint = `${getBaseApiEndpoint(dsn)}embed/error-page/`;
59
60 let encodedOptions = `dsn=${dsnToString(dsn)}`;
61 for (var key in dialogOptions) {
62 if (key === 'dsn') {
63 continue;
64 }
65
66 if (key === 'user') {
67 var user = dialogOptions.user;
68 if (!user) {
69 continue;
70 }
71 if (user.name) {
72 encodedOptions += `&name=${encodeURIComponent(user.name)}`;
73 }
74 if (user.email) {
75 encodedOptions += `&email=${encodeURIComponent(user.email)}`;
76 }
77 } else {
78 encodedOptions += `&${encodeURIComponent(key)}=${encodeURIComponent(dialogOptions[key] )}`;
79 }
80 }
81
82 return `${endpoint}?${encodedOptions}`;
83}
84
85export { getEnvelopeEndpointWithUrlEncodedAuth, getReportDialogEndpoint };
86//# sourceMappingURL=api.js.map