Unignored dist/ so yarn/npm can install from git URL without needing to build locally. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
223 lines
6.0 KiB
JavaScript
223 lines
6.0 KiB
JavaScript
import { TelemetryClient } from './client';
|
|
// Export classes
|
|
export { TelemetryClient } from './client';
|
|
export { Journey, JourneyScope, Step, StepScope } from './journey';
|
|
// Global client instance
|
|
let globalClient = null;
|
|
/**
|
|
* Initialize the global IronTelemetry client
|
|
*/
|
|
export function init(optionsOrDsn) {
|
|
const options = typeof optionsOrDsn === 'string' ? { dsn: optionsOrDsn } : optionsOrDsn;
|
|
globalClient = new TelemetryClient(options);
|
|
return globalClient;
|
|
}
|
|
/**
|
|
* Get the global client instance
|
|
*/
|
|
export function getClient() {
|
|
return globalClient;
|
|
}
|
|
/**
|
|
* Capture an exception using the global client
|
|
*/
|
|
export async function captureException(error, extra) {
|
|
if (!globalClient) {
|
|
console.warn('[IronTelemetry] Client not initialized. Call init() first.');
|
|
return { success: false, error: 'Client not initialized' };
|
|
}
|
|
return globalClient.captureException(error, extra);
|
|
}
|
|
/**
|
|
* Capture a message using the global client
|
|
*/
|
|
export async function captureMessage(message, level = 'info') {
|
|
if (!globalClient) {
|
|
console.warn('[IronTelemetry] Client not initialized. Call init() first.');
|
|
return { success: false, error: 'Client not initialized' };
|
|
}
|
|
return globalClient.captureMessage(message, level);
|
|
}
|
|
export function addBreadcrumb(messageOrBreadcrumb, category, level, data) {
|
|
if (!globalClient) {
|
|
console.warn('[IronTelemetry] Client not initialized. Call init() first.');
|
|
return;
|
|
}
|
|
if (typeof messageOrBreadcrumb === 'string') {
|
|
globalClient.addBreadcrumb(messageOrBreadcrumb, category, level, data);
|
|
}
|
|
else {
|
|
globalClient.addBreadcrumb(messageOrBreadcrumb);
|
|
}
|
|
}
|
|
/**
|
|
* Set user context using the global client
|
|
*/
|
|
export function setUser(id, email, data) {
|
|
if (!globalClient) {
|
|
console.warn('[IronTelemetry] Client not initialized. Call init() first.');
|
|
return;
|
|
}
|
|
globalClient.setUser(id, email, data);
|
|
}
|
|
/**
|
|
* Clear user context using the global client
|
|
*/
|
|
export function clearUser() {
|
|
if (!globalClient) {
|
|
return;
|
|
}
|
|
globalClient.clearUser();
|
|
}
|
|
/**
|
|
* Set a tag using the global client
|
|
*/
|
|
export function setTag(key, value) {
|
|
if (!globalClient) {
|
|
console.warn('[IronTelemetry] Client not initialized. Call init() first.');
|
|
return;
|
|
}
|
|
globalClient.setTag(key, value);
|
|
}
|
|
/**
|
|
* Set extra context using the global client
|
|
*/
|
|
export function setExtra(key, value) {
|
|
if (!globalClient) {
|
|
console.warn('[IronTelemetry] Client not initialized. Call init() first.');
|
|
return;
|
|
}
|
|
globalClient.setExtra(key, value);
|
|
}
|
|
/**
|
|
* Start a journey using the global client
|
|
*/
|
|
export function startJourney(name) {
|
|
if (!globalClient) {
|
|
throw new Error('[IronTelemetry] Client not initialized. Call init() first.');
|
|
}
|
|
return globalClient.startJourney(name);
|
|
}
|
|
/**
|
|
* Start a step in the current journey using the global client
|
|
*/
|
|
export function startStep(name, category) {
|
|
if (!globalClient) {
|
|
throw new Error('[IronTelemetry] Client not initialized. Call init() first.');
|
|
}
|
|
return globalClient.startStep(name, category);
|
|
}
|
|
/**
|
|
* Flush pending events using the global client
|
|
*/
|
|
export async function flush() {
|
|
if (!globalClient) {
|
|
return;
|
|
}
|
|
await globalClient.flush();
|
|
}
|
|
/**
|
|
* Close the global client
|
|
*/
|
|
export function close() {
|
|
if (globalClient) {
|
|
globalClient.close();
|
|
globalClient = null;
|
|
}
|
|
}
|
|
// Default export for convenience
|
|
const IronTelemetry = {
|
|
init,
|
|
getClient,
|
|
captureException,
|
|
captureMessage,
|
|
addBreadcrumb,
|
|
setUser,
|
|
clearUser,
|
|
setTag,
|
|
setExtra,
|
|
startJourney,
|
|
startStep,
|
|
flush,
|
|
close,
|
|
};
|
|
export default IronTelemetry;
|
|
/**
|
|
* Enable the Error.prototype.capture() extension.
|
|
* This is opt-in to avoid TypeScript conflicts in projects that create
|
|
* Error-like objects without the capture method.
|
|
*
|
|
* After calling this, you can use: throw new Error('msg').capture()
|
|
*/
|
|
export function enableErrorCapture() {
|
|
if (typeof Error.prototype.capture === 'function') {
|
|
return; // Already enabled
|
|
}
|
|
Error.prototype.capture = function () {
|
|
captureException(this);
|
|
return this;
|
|
};
|
|
}
|
|
// Type augmentation for projects that call enableErrorCapture()
|
|
// Users can add this to their project if they want type support:
|
|
// declare global { interface Error { capture(): Error; } }
|
|
/**
|
|
* Set up global unhandled exception handler
|
|
*/
|
|
export function useUnhandledExceptionHandler() {
|
|
if (typeof window !== 'undefined') {
|
|
// Browser
|
|
window.addEventListener('error', (event) => {
|
|
captureException(event.error ?? new Error(event.message));
|
|
});
|
|
window.addEventListener('unhandledrejection', (event) => {
|
|
captureException(event.reason ?? new Error('Unhandled Promise rejection'));
|
|
});
|
|
}
|
|
else if (typeof process !== 'undefined') {
|
|
// Node.js
|
|
process.on('uncaughtException', (error) => {
|
|
captureException(error);
|
|
});
|
|
process.on('unhandledRejection', (reason) => {
|
|
captureException(reason ?? new Error('Unhandled Promise rejection'));
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* Track a step with automatic error handling
|
|
*/
|
|
export function trackStep(name, fn, category) {
|
|
if (!globalClient) {
|
|
return fn();
|
|
}
|
|
const step = startStep(name, category);
|
|
try {
|
|
const result = fn();
|
|
step[Symbol.dispose]();
|
|
return result;
|
|
}
|
|
catch (error) {
|
|
step.getStep().fail();
|
|
throw error;
|
|
}
|
|
}
|
|
/**
|
|
* Track an async step with automatic error handling
|
|
*/
|
|
export async function trackStepAsync(name, fn, category) {
|
|
if (!globalClient) {
|
|
return fn();
|
|
}
|
|
const step = startStep(name, category);
|
|
try {
|
|
const result = await fn();
|
|
step[Symbol.dispose]();
|
|
return result;
|
|
}
|
|
catch (error) {
|
|
step.getStep().fail();
|
|
throw error;
|
|
}
|
|
}
|
|
//# sourceMappingURL=index.js.map
|