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