api.js 2.9 KB

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