-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtunnelRoute.ts
More file actions
61 lines (54 loc) · 1.62 KB
/
tunnelRoute.ts
File metadata and controls
61 lines (54 loc) · 1.62 KB
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
import { dsnToString, getClient, handleTunnelRequest } from '@sentry/core';
export interface CreateSentryTunnelRouteOptions {
allowedDsns?: string[];
}
type SentryTunnelRouteHandlerContext = {
request: Request;
};
type SentryTunnelRoute = {
handlers: {
POST: (context: SentryTunnelRouteHandlerContext) => Promise<Response>;
};
};
/**
* Creates a TanStack Start server route configuration for tunneling Sentry envelopes.
*
* @example
* ```ts
* import { createFileRoute } from '@tanstack/react-router';
* import * as Sentry from '@sentry/tanstackstart-react';
*
* export const Route = createFileRoute('/monitoring')({
* server: Sentry.createSentryTunnelRoute({
* allowedDsns: ['https://public@o0.ingest.sentry.io/0'],
* }),
* });
* ```
*/
export function createSentryTunnelRoute(options: CreateSentryTunnelRouteOptions): SentryTunnelRoute {
return {
handlers: {
POST: async ({ request }) => {
const allowedDsnsFromOptions =
options.allowedDsns && options.allowedDsns.length > 0 ? options.allowedDsns : undefined;
const allowedDsns =
allowedDsnsFromOptions ??
(() => {
const client = getClient();
const dsn = client?.getDsn();
return dsn ? [dsnToString(dsn)] : undefined;
})();
if (!allowedDsns) {
return new Response(
'Tunnel route requires Sentry server SDK initialized with a DSN, or pass allowedDsns explicitly.',
{ status: 500 },
);
}
return handleTunnelRequest({
request,
allowedDsns,
});
},
},
};
}