@syntropysoft/syntropyfront
Version:
🚀 Observability library with automatic capture - Just 1 line of code! Automatically captures clicks, errors, HTTP calls, and console logs with flexible error handling.
3 lines (2 loc) • 4.88 kB
JavaScript
var SyntropyFront=function(){"use strict";class r{constructor(){this.breadcrumbs=[]}add(r,e,t={}){const s={category:r,message:e,data:t,timestamp:(new Date).toISOString()};return this.breadcrumbs.push(s),s}getAll(){return this.breadcrumbs}clear(){this.breadcrumbs=[]}getCount(){return this.breadcrumbs.length}}class e{constructor(){this.errors=[]}send(r,e={}){const t={message:r.message,stack:r.stack,context:e,timestamp:(new Date).toISOString()};return this.errors.push(t),t}getAll(){return this.errors}clear(){this.errors=[]}getCount(){return this.errors.length}}class t{constructor(){this.isSilent=!0}log(r,e=null){this.isSilent||(e?console.log(r,e):console.log(r))}error(r,e=null){e?console.error(r,e):console.error(r)}warn(r,e=null){e?console.warn(r,e):console.warn(r)}enableLogging(){this.isSilent=!1}disableLogging(){this.isSilent=!0}}return new class{constructor(){this.breadcrumbManager=new r,this.errorManager=new e,this.logger=new t,this.maxEvents=50,this.fetchConfig=null,this.onErrorCallback=null,this.isActive=!1,this.originalHandlers={},this.init()}init(){this.isActive=!0,this.setupAutomaticCapture(),console.log("🚀 SyntropyFront: Initialized with automatic capture")}configure(r={}){this.maxEvents=r.maxEvents||this.maxEvents,this.fetchConfig=r.fetch,this.onErrorCallback=r.onError,this.onErrorCallback?console.log(`✅ SyntropyFront: Configured - maxEvents: ${this.maxEvents}, custom error handler`):this.fetchConfig?console.log(`✅ SyntropyFront: Configured - maxEvents: ${this.maxEvents}, endpoint: ${this.fetchConfig.url}`):console.log(`✅ SyntropyFront: Configured - maxEvents: ${this.maxEvents}, console only`)}setupAutomaticCapture(){"undefined"!=typeof window&&(this.setupClickCapture(),this.setupErrorCapture(),this.setupHttpCapture(),this.setupConsoleCapture())}setupClickCapture(){document.addEventListener("click",r=>{const e=r.target;this.addBreadcrumb("user","click",{element:e.tagName,id:e.id,className:e.className,x:r.clientX,y:r.clientY})})}setupErrorCapture(){this.originalHandlers.onerror=window.onerror,this.originalHandlers.onunhandledrejection=window.onunhandledrejection,window.onerror=(r,e,t,s,n)=>{const o={type:"uncaught_exception",error:{message:r,source:e,lineno:t,colno:s,stack:n?.stack},breadcrumbs:this.getBreadcrumbs(),timestamp:(new Date).toISOString()};return this.handleError(o),!!this.originalHandlers.onerror&&this.originalHandlers.onerror(r,e,t,s,n)},window.onunhandledrejection=r=>{const e={type:"unhandled_rejection",error:{message:r.reason?.message||"Promise rejection without message",stack:r.reason?.stack},breadcrumbs:this.getBreadcrumbs(),timestamp:(new Date).toISOString()};this.handleError(e),this.originalHandlers.onunhandledrejection&&this.originalHandlers.onunhandledrejection(r)}}setupHttpCapture(){const r=window.fetch;window.fetch=(...e)=>{const[t,s]=e;return this.addBreadcrumb("http","fetch",{url:t,method:s?.method||"GET"}),r(...e).then(r=>(this.addBreadcrumb("http","fetch_response",{url:t,status:r.status}),r)).catch(r=>{throw this.addBreadcrumb("http","fetch_error",{url:t,error:r.message}),r})}}setupConsoleCapture(){const r=console.log,e=console.error,t=console.warn;console.log=(...e)=>{this.addBreadcrumb("console","log",{message:e.join(" ")}),r.apply(console,e)},console.error=(...r)=>{this.addBreadcrumb("console","error",{message:r.join(" ")}),e.apply(console,r)},console.warn=(...r)=>{this.addBreadcrumb("console","warn",{message:r.join(" ")}),t.apply(console,r)}}handleError(r){if(this.logger.error("❌ Error:",r),this.onErrorCallback)try{this.onErrorCallback(r)}catch(r){console.warn("SyntropyFront: Error in user callback:",r)}else this.fetchConfig&&this.postToEndpoint(r)}postToEndpoint(r){const{url:e,options:t={}}=this.fetchConfig,s={method:"POST",headers:{"Content-Type":"application/json",...t.headers},body:JSON.stringify(r),...t};fetch(e,s).catch(r=>{console.warn("SyntropyFront: Error posting to endpoint:",r)})}addBreadcrumb(r,e,t={}){if(!this.isActive)return;const s=this.breadcrumbManager.add(r,e,t),n=this.breadcrumbManager.getAll();return n.length>this.maxEvents&&(this.breadcrumbManager.clear(),n.slice(-this.maxEvents).forEach(r=>this.breadcrumbManager.add(r.category,r.message,r.data))),s}getBreadcrumbs(){return this.breadcrumbManager.getAll()}clearBreadcrumbs(){this.breadcrumbManager.clear()}sendError(r,e={}){if(!this.isActive)return;const t=this.errorManager.send(r,e),s={...t,breadcrumbs:this.getBreadcrumbs(),timestamp:(new Date).toISOString()};return this.handleError(s),t}getErrors(){return this.errorManager.getAll()}clearErrors(){this.errorManager.clear()}getStats(){return{breadcrumbs:this.breadcrumbManager.getCount(),errors:this.errorManager.getCount(),isActive:this.isActive,maxEvents:this.maxEvents,hasFetchConfig:!!this.fetchConfig,hasErrorCallback:!!this.onErrorCallback,endpoint:this.fetchConfig?.url||"console"}}}}();
//# sourceMappingURL=index.min.js.map