api.js 3.0 KB

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