|
| 1 | +import { DSNLike } from '@sentry/types'; |
| 2 | +import { urlEncode } from '@sentry/utils/object'; |
| 3 | +import { DSN } from './dsn'; |
| 4 | + |
| 5 | +const SENTRY_API_VERSION = '7'; |
| 6 | + |
| 7 | +/** Helper class to provide urls to different Sentry endpoints. */ |
| 8 | +export class API { |
| 9 | + /** The internally used DSN object. */ |
| 10 | + private readonly dsnObject: DSN; |
| 11 | + /** Create a new instance of API */ |
| 12 | + public constructor(public dsn: DSNLike) { |
| 13 | + this.dsnObject = new DSN(dsn); |
| 14 | + } |
| 15 | + |
| 16 | + /** Returns the DSN object. */ |
| 17 | + public getDSN(): DSN { |
| 18 | + return this.dsnObject; |
| 19 | + } |
| 20 | + |
| 21 | + /** Returns a string with auth headers in the url to the store endpoint. */ |
| 22 | + public getStoreEndpoint(): string { |
| 23 | + return `${this.getBaseUrl()}${this.getStoreEndpointPath()}`; |
| 24 | + } |
| 25 | + |
| 26 | + /** Returns the store endpoint with auth added in url encoded. */ |
| 27 | + public getStoreEndpointWithUrlEncodedAuth(): string { |
| 28 | + const dsn = this.dsnObject; |
| 29 | + const auth = { |
| 30 | + sentry_key: dsn.user, |
| 31 | + sentry_version: SENTRY_API_VERSION, |
| 32 | + }; |
| 33 | + // Auth is intentionally sent as part of query string (NOT as custom HTTP header) |
| 34 | + // to avoid preflight CORS requests |
| 35 | + return `${this.getStoreEndpoint()}?${urlEncode(auth)}`; |
| 36 | + } |
| 37 | + |
| 38 | + /** Returns the base path of the url including the port. */ |
| 39 | + private getBaseUrl(): string { |
| 40 | + const dsn = this.dsnObject; |
| 41 | + const protocol = dsn.protocol ? `${dsn.protocol}:` : ''; |
| 42 | + const port = dsn.port ? `:${dsn.port}` : ''; |
| 43 | + return `${protocol}//${dsn.host}${port}`; |
| 44 | + } |
| 45 | + |
| 46 | + /** Returns only the path component for the store endpoint. */ |
| 47 | + public getStoreEndpointPath(): string { |
| 48 | + const dsn = this.dsnObject; |
| 49 | + return `${dsn.path ? `/${dsn.path}` : ''}/api/${dsn.projectId}/store/`; |
| 50 | + } |
| 51 | + |
| 52 | + /** Returns an object that can be used in request headers. */ |
| 53 | + public getRequestHeaders(clientName: string, clientVersion: string): { [key: string]: string } { |
| 54 | + const dsn = this.dsnObject; |
| 55 | + const header = [`Sentry sentry_version=${SENTRY_API_VERSION}`]; |
| 56 | + header.push(`sentry_timestamp=${new Date().getTime()}`); |
| 57 | + header.push(`sentry_client=${clientName}/${clientVersion}`); |
| 58 | + header.push(`sentry_key=${dsn.user}`); |
| 59 | + return { |
| 60 | + 'Content-Type': 'application/json', |
| 61 | + 'X-Sentry-Auth': header.join(', '), |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + /** Returns the url to the report dialog endpoint. */ |
| 66 | + public getReportDialogEndpoint( |
| 67 | + dialogOptions: { |
| 68 | + [key: string]: any; |
| 69 | + user?: { name?: string; email?: string }; |
| 70 | + } = {}, |
| 71 | + ): string { |
| 72 | + const dsn = this.dsnObject; |
| 73 | + const endpoint = `${this.getBaseUrl()}${dsn.path ? `/${dsn.path}` : ''}/api/embed/error-page/`; |
| 74 | + |
| 75 | + const encodedOptions = []; |
| 76 | + for (const key in dialogOptions) { |
| 77 | + if (key === 'user') { |
| 78 | + if (!dialogOptions.user) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + if (dialogOptions.user.name) { |
| 82 | + encodedOptions.push(`name=${encodeURIComponent(dialogOptions.user.name)}`); |
| 83 | + } |
| 84 | + if (dialogOptions.user.email) { |
| 85 | + encodedOptions.push(`email=${encodeURIComponent(dialogOptions.user.email)}`); |
| 86 | + } |
| 87 | + } else { |
| 88 | + encodedOptions.push(`${encodeURIComponent(key)}=${encodeURIComponent(dialogOptions[key] as string)}`); |
| 89 | + } |
| 90 | + } |
| 91 | + if (encodedOptions.length) { |
| 92 | + return `${endpoint}?${encodedOptions.join('&')}`; |
| 93 | + } |
| 94 | + |
| 95 | + return endpoint; |
| 96 | + } |
| 97 | +} |
0 commit comments