UNPKG

@logsnag/node

Version:

LogSnag client for Node.js applications

2 lines (1 loc) 3.22 kB
var u=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var m=(s,t)=>{for(var r in t)u(s,r,{get:t[r],enumerable:!0})},b=(s,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of f(t))!l.call(s,o)&&o!==r&&u(s,o,{get:()=>t[o],enumerable:!(i=g(t,o))||i.enumerable});return s};var T=s=>b(u({},"__esModule",{value:!0}),s);var k={};m(k,{HTTPResponseError:()=>n,LogSnag:()=>c});module.exports=T(k);var h="https://api.logsnag.com",d="/v1",a={LOG:h+d+"/log",IDENTIFY:h+d+"/identify",INSIGHT:h+d+"/insight",GROUP:h+"/v1/group",PAGE:h+d+"/page"};var n=class extends Error{message;constructor(t,r,i){super(`HTTP Error Response: ${t} ${r}`),this.message=this.createReadableString(i)}createReadableString(t){let r="[LogSnag] Failed to publish: ";return t&&t.validation&&Array.isArray(t.validation.body)?r+=t.validation.body.map(i=>i.message).join(", "):r+=": Please check our docs at https://docs.logsnag.com",r}toString(){return this.message}toJSON(){return{message:this.message}}};function O(s){return Math.abs(Date.now()-s)<Math.abs(Date.now()-s*1e3)}function p(s){if(s)return s instanceof Date&&(s=s.getTime()),O(s)&&(s=Math.floor(s/1e3)),s}var c=class{token;project;disabled=!1;constructor({token:t,project:r,disableTracking:i=!1}){this.token=t,this.project=r,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 r=this.createHeaders(),i="POST";t.timestamp=p(t.timestamp);let o=JSON.stringify({...t,project:this.getProject()}),e=await fetch(a.LOG,{method:i,body:o,headers:r});if(!e.ok)throw new n(e.status,e.statusText,await e.json());return!0}async identify(t){if(this.isTrackingDisabled())return!0;let r=this.createHeaders(),i="POST",o=JSON.stringify({...t,project:this.getProject()}),e=await fetch(a.IDENTIFY,{method:i,body:o,headers:r});if(!e.ok)throw new n(e.status,e.statusText,await e.json());return!0}async group(t){if(this.isTrackingDisabled())return!0;let r=this.createHeaders(),i="POST",o=JSON.stringify({...t,project:this.getProject()}),e=await fetch(a.GROUP,{method:i,body:o,headers:r});if(!e.ok)throw new n(e.status,e.statusText,await e.json());return!0}async insightTrack(t){if(this.isTrackingDisabled())return!0;let r=this.createHeaders(),i="POST",o=JSON.stringify({...t,project:this.getProject()}),e=await fetch(a.INSIGHT,{method:i,body:o,headers:r});if(!e.ok)throw new n(e.status,e.statusText,await e.json());return!0}async insightIncrement(t){if(this.isTrackingDisabled())return!0;let r=this.createHeaders(),i="PATCH",o=JSON.stringify({project:this.getProject(),icon:t.icon,title:t.title,value:{$inc:t.value}}),e=await fetch(a.INSIGHT,{method:i,body:o,headers:r});if(!e.ok)throw new n(e.status,e.statusText,await e.json());return!0}};0&&(module.exports={HTTPResponseError,LogSnag});