152 lines
3.3 KiB
TypeScript
152 lines
3.3 KiB
TypeScript
import Client from './core/Client';
|
|
import ErrorPlugin from './plugins/ErrorPlugin';
|
|
import PerformancePlugin from './plugins/PerformancePlugin';
|
|
import NetworkPlugin from './plugins/NetworkPlugin';
|
|
import BehaviorPlugin from './plugins/BehaviorPlugin';
|
|
import OfflinePlugin from './plugins/OfflinePlugin';
|
|
import SamplingPlugin from './plugins/SamplingPlugin';
|
|
import type { LightConfig, LightClient, SentryEvent, EventLevel, UserInfo, Breadcrumb, LightPlugin, ErrorEvent, PerformanceEvent, NetworkEvent, BehaviorEvent, DSNInfo } from './types';
|
|
|
|
let globalClient: LightClient | null = null;
|
|
|
|
function init(config: LightConfig): LightClient {
|
|
const client = new Client(config);
|
|
|
|
const defaultPlugins: LightPlugin[] = [
|
|
new OfflinePlugin(),
|
|
new SamplingPlugin(),
|
|
new ErrorPlugin(),
|
|
];
|
|
|
|
const pluginNames = config.plugins?.filter(p => typeof p === 'string') as string[] || [];
|
|
|
|
if (pluginNames.includes('performance') || pluginNames.includes('all')) {
|
|
defaultPlugins.push(new PerformancePlugin());
|
|
}
|
|
if (pluginNames.includes('network') || pluginNames.includes('all')) {
|
|
defaultPlugins.push(new NetworkPlugin());
|
|
}
|
|
if (pluginNames.includes('behavior') || pluginNames.includes('all')) {
|
|
defaultPlugins.push(new BehaviorPlugin());
|
|
}
|
|
|
|
for (const plugin of defaultPlugins) {
|
|
client.use(plugin);
|
|
}
|
|
|
|
if (config.plugins) {
|
|
for (const plugin of config.plugins) {
|
|
if (typeof plugin === 'object' && plugin !== null && 'name' in plugin && 'setup' in plugin) {
|
|
client.use(plugin as LightPlugin);
|
|
}
|
|
}
|
|
}
|
|
|
|
client.init();
|
|
|
|
globalClient = client;
|
|
return client;
|
|
}
|
|
|
|
function getClient(): LightClient | null {
|
|
return globalClient;
|
|
}
|
|
|
|
function captureException(error: Error | unknown): void {
|
|
globalClient?.captureException(error);
|
|
}
|
|
|
|
function captureMessage(message: string, level?: EventLevel): void {
|
|
globalClient?.captureMessage(message, level);
|
|
}
|
|
|
|
function captureEvent(event: Partial<SentryEvent> & { type: string }): void {
|
|
globalClient?.captureEvent(event);
|
|
}
|
|
|
|
function setUser(user: UserInfo | null): void {
|
|
globalClient?.setUser(user);
|
|
}
|
|
|
|
function setTag(key: string, value: string): void {
|
|
globalClient?.setTag(key, value);
|
|
}
|
|
|
|
function setTags(tags: Record<string, string>): void {
|
|
globalClient?.setTags(tags);
|
|
}
|
|
|
|
function setExtra(key: string, value: unknown): void {
|
|
globalClient?.setExtra(key, value);
|
|
}
|
|
|
|
function addBreadcrumb(breadcrumb: Omit<Breadcrumb, 'timestamp'>): void {
|
|
globalClient?.addBreadcrumb(breadcrumb);
|
|
}
|
|
|
|
async function flush(): Promise<void> {
|
|
await globalClient?.flush();
|
|
}
|
|
|
|
function disable(): void {
|
|
globalClient?.disable();
|
|
}
|
|
|
|
function enable(): void {
|
|
globalClient?.enable();
|
|
}
|
|
|
|
export {
|
|
init,
|
|
getClient,
|
|
captureException,
|
|
captureMessage,
|
|
captureEvent,
|
|
setUser,
|
|
setTag,
|
|
setTags,
|
|
setExtra,
|
|
addBreadcrumb,
|
|
flush,
|
|
disable,
|
|
enable,
|
|
Client,
|
|
ErrorPlugin,
|
|
PerformancePlugin,
|
|
NetworkPlugin,
|
|
BehaviorPlugin,
|
|
OfflinePlugin,
|
|
SamplingPlugin,
|
|
};
|
|
|
|
export type {
|
|
LightConfig,
|
|
LightClient,
|
|
LightPlugin,
|
|
SentryEvent,
|
|
ErrorEvent,
|
|
PerformanceEvent,
|
|
NetworkEvent,
|
|
BehaviorEvent,
|
|
EventLevel,
|
|
UserInfo,
|
|
Breadcrumb,
|
|
DSNInfo,
|
|
};
|
|
|
|
export default {
|
|
init,
|
|
getClient,
|
|
captureException,
|
|
captureMessage,
|
|
captureEvent,
|
|
setUser,
|
|
setTag,
|
|
setTags,
|
|
setExtra,
|
|
addBreadcrumb,
|
|
flush,
|
|
disable,
|
|
enable,
|
|
};
|