@logsnag/remix
Version:
LogSnag client for Remix applications
3 lines • 2.96 kB
JavaScript
"use client";
var p=Object.defineProperty;var f=(s,t,e)=>t in s?p(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var a=(s,t,e)=>(f(s,typeof t!="symbol"?t+"":t,e),e);var h="https://api.logsnag.com",u="/v1",c={LOG:h+u+"/log",IDENTIFY:h+u+"/identify",INSIGHT:h+u+"/insight",GROUP:h+"/v1/group",PAGE:h+u+"/page"};var n=class extends Error{constructor(e,i,o){super(`HTTP Error Response: ${e} ${i}`);a(this,"message");this.message=this.createReadableString(o)}createReadableString(e){let i="[LogSnag] Failed to publish: ";return e&&e.validation&&Array.isArray(e.validation.body)?i+=e.validation.body.map(o=>o.message).join(", "):i+=": Please check our docs at https://docs.logsnag.com",i}toString(){return this.message}toJSON(){return{message:this.message}}};function l(s){return Math.abs(Date.now()-s)<Math.abs(Date.now()-s*1e3)}function g(s){if(s)return s instanceof Date&&(s=s.getTime()),l(s)&&(s=Math.floor(s/1e3)),s}var d=class{constructor({token:t,project:e,disableTracking:i=!1}){a(this,"token");a(this,"project");a(this,"disabled",!1);this.token=t,this.project=e,this.disabled=i||!1}disableTracking(){this.disabled=!0}enableTracking(){this.disabled=!1}isTrackingDisabled(){return this.disabled}getProject(){return this.project}createAuthorizationHeader(){return`Bearer ${this.token}`}createHeaders(){return{"Content-Type":"application/json",Authorization:this.createAuthorizationHeader()}}get insight(){return{track:this.insightTrack.bind(this),increment:this.insightIncrement.bind(this)}}async track(t){if(this.isTrackingDisabled())return!0;let e=this.createHeaders(),i="POST";t.timestamp=g(t.timestamp);let o=JSON.stringify({...t,project:this.getProject()}),r=await fetch(c.LOG,{method:i,body:o,headers:e});if(!r.ok)throw new n(r.status,r.statusText,await r.json());return!0}async identify(t){if(this.isTrackingDisabled())return!0;let e=this.createHeaders(),i="POST",o=JSON.stringify({...t,project:this.getProject()}),r=await fetch(c.IDENTIFY,{method:i,body:o,headers:e});if(!r.ok)throw new n(r.status,r.statusText,await r.json());return!0}async group(t){if(this.isTrackingDisabled())return!0;let e=this.createHeaders(),i="POST",o=JSON.stringify({...t,project:this.getProject()}),r=await fetch(c.GROUP,{method:i,body:o,headers:e});if(!r.ok)throw new n(r.status,r.statusText,await r.json());return!0}async insightTrack(t){if(this.isTrackingDisabled())return!0;let e=this.createHeaders(),i="POST",o=JSON.stringify({...t,project:this.getProject()}),r=await fetch(c.INSIGHT,{method:i,body:o,headers:e});if(!r.ok)throw new n(r.status,r.statusText,await r.json());return!0}async insightIncrement(t){if(this.isTrackingDisabled())return!0;let e=this.createHeaders(),i="PATCH",o=JSON.stringify({project:this.getProject(),icon:t.icon,title:t.title,value:{$inc:t.value}}),r=await fetch(c.INSIGHT,{method:i,body:o,headers:e});if(!r.ok)throw new n(r.status,r.statusText,await r.json());return!0}};export{d as LogSnag};
//# sourceMappingURL=index.mjs.map