-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovide.ts
More file actions
153 lines (144 loc) · 4.13 KB
/
provide.ts
File metadata and controls
153 lines (144 loc) · 4.13 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
ErrorHandler,
type EnvironmentProviders,
type Provider,
makeEnvironmentProviders,
APP_INITIALIZER,
} from '@angular/core';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import type { Integration, Transport } from '@logtide/types';
import { hub, GlobalErrorIntegration, resolveDSN } from '@logtide/core';
import {
getSessionId,
WebVitalsIntegration,
ClickBreadcrumbIntegration,
NetworkBreadcrumbIntegration,
OfflineTransport,
type BrowserClientOptions,
} from '@logtide/browser';
import { LogtideErrorHandler } from './error-handler';
import { LogtideHttpInterceptor } from './http-interceptor';
function buildBrowserIntegrations(options: BrowserClientOptions): Integration[] {
const browserOpts = options.browser ?? {};
const integrations: Integration[] = [];
const apiUrl = resolveDSN(options).apiUrl;
if (browserOpts.webVitals) {
integrations.push(
new WebVitalsIntegration({
sampleRate: browserOpts.webVitalsSampleRate,
}),
);
}
if (browserOpts.clickBreadcrumbs !== false) {
const clickOpts = typeof browserOpts.clickBreadcrumbs === 'object'
? browserOpts.clickBreadcrumbs
: undefined;
integrations.push(new ClickBreadcrumbIntegration(clickOpts));
}
if (browserOpts.networkBreadcrumbs !== false) {
const netOpts = typeof browserOpts.networkBreadcrumbs === 'object'
? browserOpts.networkBreadcrumbs
: {};
integrations.push(
new NetworkBreadcrumbIntegration({ ...netOpts, apiUrl }),
);
}
return integrations;
}
function buildTransportWrapper(options: BrowserClientOptions): ((inner: Transport) => Transport) | undefined {
const browserOpts = options.browser ?? {};
if (browserOpts.offlineResilience === false) return undefined;
const dsn = resolveDSN(options);
return (inner: Transport) => new OfflineTransport({
inner,
beaconUrl: `${dsn.apiUrl}/api/v1/ingest`,
apiKey: dsn.apiKey,
debug: options.debug,
});
}
/**
* Provide LogTide in a standalone Angular app (Angular 17+).
*
* @example
* ```ts
* // app.config.ts
* import { provideLogtide } from '@logtide/angular';
*
* export const appConfig: ApplicationConfig = {
* providers: [
* provideLogtide({ dsn: '...', service: 'my-angular-app' }),
* ],
* };
* ```
*/
export function provideLogtide(options: BrowserClientOptions): EnvironmentProviders {
return makeEnvironmentProviders([
{
provide: APP_INITIALIZER,
useFactory: () => {
return () => {
hub.init({
service: 'angular',
...options,
transportWrapper: buildTransportWrapper(options) ?? options.transportWrapper,
integrations: [
new GlobalErrorIntegration(),
...buildBrowserIntegrations(options),
...(options.integrations ?? []),
],
});
hub.getScope().setSessionId(getSessionId());
};
},
multi: true,
},
{ provide: ErrorHandler, useClass: LogtideErrorHandler },
{
provide: HTTP_INTERCEPTORS,
useClass: LogtideHttpInterceptor,
multi: true,
},
]);
}
/**
* Get the providers array for use in NgModule-based apps.
*
* @example
* ```ts
* @NgModule({
* providers: [
* ...getLogtideProviders({ dsn: '...', service: 'my-app' }),
* ],
* })
* export class AppModule {}
* ```
*/
export function getLogtideProviders(options: BrowserClientOptions): Provider[] {
return [
{
provide: APP_INITIALIZER,
useFactory: () => {
return () => {
hub.init({
service: 'angular',
...options,
transportWrapper: buildTransportWrapper(options) ?? options.transportWrapper,
integrations: [
new GlobalErrorIntegration(),
...buildBrowserIntegrations(options),
...(options.integrations ?? []),
],
});
hub.getScope().setSessionId(getSessionId());
};
},
multi: true,
},
{ provide: ErrorHandler, useClass: LogtideErrorHandler },
{
provide: HTTP_INTERCEPTORS,
useClass: LogtideHttpInterceptor,
multi: true,
},
];
}