UNPKG

5.51 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var utils_1 = require("@sentry/utils");
3var SENTRY_API_VERSION = '7';
4/**
5 * Helper class to provide urls, headers and metadata that can be used to form
6 * different types of requests to Sentry endpoints.
7 * Supports both envelopes and regular event requests.
8 **/
9var API = /** @class */ (function () {
10 /** Create a new instance of API */
11 function API(dsn, metadata, tunnel) {
12 if (metadata === void 0) { metadata = {}; }
13 this.dsn = dsn;
14 this._dsnObject = new utils_1.Dsn(dsn);
15 this.metadata = metadata;
16 this._tunnel = tunnel;
17 }
18 /** Returns the Dsn object. */
19 API.prototype.getDsn = function () {
20 return this._dsnObject;
21 };
22 /** Does this transport force envelopes? */
23 API.prototype.forceEnvelope = function () {
24 return !!this._tunnel;
25 };
26 /** Returns the prefix to construct Sentry ingestion API endpoints. */
27 API.prototype.getBaseApiEndpoint = function () {
28 var dsn = this.getDsn();
29 var protocol = dsn.protocol ? dsn.protocol + ":" : '';
30 var port = dsn.port ? ":" + dsn.port : '';
31 return protocol + "//" + dsn.host + port + (dsn.path ? "/" + dsn.path : '') + "/api/";
32 };
33 /** Returns the store endpoint URL. */
34 API.prototype.getStoreEndpoint = function () {
35 return this._getIngestEndpoint('store');
36 };
37 /**
38 * Returns the store endpoint URL with auth in the query string.
39 *
40 * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
41 */
42 API.prototype.getStoreEndpointWithUrlEncodedAuth = function () {
43 return this.getStoreEndpoint() + "?" + this._encodedAuth();
44 };
45 /**
46 * Returns the envelope endpoint URL with auth in the query string.
47 *
48 * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
49 */
50 API.prototype.getEnvelopeEndpointWithUrlEncodedAuth = function () {
51 if (this.forceEnvelope()) {
52 return this._tunnel;
53 }
54 return this._getEnvelopeEndpoint() + "?" + this._encodedAuth();
55 };
56 /** Returns only the path component for the store endpoint. */
57 API.prototype.getStoreEndpointPath = function () {
58 var dsn = this.getDsn();
59 return (dsn.path ? "/" + dsn.path : '') + "/api/" + dsn.projectId + "/store/";
60 };
61 /**
62 * Returns an object that can be used in request headers.
63 * This is needed for node and the old /store endpoint in sentry
64 */
65 API.prototype.getRequestHeaders = function (clientName, clientVersion) {
66 // CHANGE THIS to use metadata but keep clientName and clientVersion compatible
67 var dsn = this.getDsn();
68 var header = ["Sentry sentry_version=" + SENTRY_API_VERSION];
69 header.push("sentry_client=" + clientName + "/" + clientVersion);
70 header.push("sentry_key=" + dsn.publicKey);
71 if (dsn.pass) {
72 header.push("sentry_secret=" + dsn.pass);
73 }
74 return {
75 'Content-Type': 'application/json',
76 'X-Sentry-Auth': header.join(', '),
77 };
78 };
79 /** Returns the url to the report dialog endpoint. */
80 API.prototype.getReportDialogEndpoint = function (dialogOptions) {
81 if (dialogOptions === void 0) { dialogOptions = {}; }
82 var dsn = this.getDsn();
83 var endpoint = this.getBaseApiEndpoint() + "embed/error-page/";
84 var encodedOptions = [];
85 encodedOptions.push("dsn=" + dsn.toString());
86 for (var key in dialogOptions) {
87 if (key === 'dsn') {
88 continue;
89 }
90 if (key === 'user') {
91 if (!dialogOptions.user) {
92 continue;
93 }
94 if (dialogOptions.user.name) {
95 encodedOptions.push("name=" + encodeURIComponent(dialogOptions.user.name));
96 }
97 if (dialogOptions.user.email) {
98 encodedOptions.push("email=" + encodeURIComponent(dialogOptions.user.email));
99 }
100 }
101 else {
102 encodedOptions.push(encodeURIComponent(key) + "=" + encodeURIComponent(dialogOptions[key]));
103 }
104 }
105 if (encodedOptions.length) {
106 return endpoint + "?" + encodedOptions.join('&');
107 }
108 return endpoint;
109 };
110 /** Returns the envelope endpoint URL. */
111 API.prototype._getEnvelopeEndpoint = function () {
112 return this._getIngestEndpoint('envelope');
113 };
114 /** Returns the ingest API endpoint for target. */
115 API.prototype._getIngestEndpoint = function (target) {
116 if (this._tunnel) {
117 return this._tunnel;
118 }
119 var base = this.getBaseApiEndpoint();
120 var dsn = this.getDsn();
121 return "" + base + dsn.projectId + "/" + target + "/";
122 };
123 /** Returns a URL-encoded string with auth config suitable for a query string. */
124 API.prototype._encodedAuth = function () {
125 var dsn = this.getDsn();
126 var auth = {
127 // We send only the minimum set of required information. See
128 // https://github.com/getsentry/sentry-javascript/issues/2572.
129 sentry_key: dsn.publicKey,
130 sentry_version: SENTRY_API_VERSION,
131 };
132 return utils_1.urlEncode(auth);
133 };
134 return API;
135}());
136exports.API = API;
137//# sourceMappingURL=api.js.map
\No newline at end of file