app-tracker 1.6.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +1 -0
  2. package/index.js +1 -1
  3. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -27,6 +27,7 @@ export declare class AppLogger {
27
27
  private static sendLogsToServer;
28
28
  private static checkLogType;
29
29
  private static sendLogsToServerCore;
30
+ private static setUserAgentFromOs;
30
31
  }
31
32
  export declare class AppLoggerConfig {
32
33
  sendLogsOnLocal?: boolean | undefined;
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))((function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator.throw(value))}catch(e){reject(e)}}function step(result){var value;result.done?resolve(result.value):(value=result.value,value instanceof P?value:new P((function(resolve){resolve(value)}))).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0});const log_1=require("./log"),cross_fetch_1=require("cross-fetch");class AppLogger{static init(apiKey,config){var _a,_b;this.apiKey=apiKey,config&&(config.endpointUrl&&(this.endpointUrl=config.endpointUrl),config.maxLogsCount&&(this.maxLogsCount=config.maxLogsCount),config.maxSendTries&&(this.maxSendTries=config.maxSendTries),config.sendLogsOnLocal&&(this.sendLogsOnLocal=config.sendLogsOnLocal),config.consoleLogsOnLocal&&(this.consoleLogsOnLocal=config.consoleLogsOnLocal),config.logLevel&&(this.logLevel=config.logLevel),config.sendLogsByTypesImmediately&&(this.sendLogsByTypesImmediately=config.sendLogsByTypesImmediately)),this.isNode="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0),this.isNode?(this.os=null===(_b=config)||void 0===_b?void 0:_b.userAgentOrOs,this.userAgent=JSON.stringify(this.os)):this.userAgent=null===(_a=config)||void 0===_a?void 0:_a.userAgentOrOs,this.isRunningLocal=this.isNode?this.os.hostname().indexOf("local")>-1:window.location.href.indexOf("localhost")>-1,""==apiKey||this.isNode||window.addEventListener("beforeunload",(e=>{if(e.preventDefault(),e.returnValue="",!this.isSending){const logsToSend=this.logs;this.isSending=!0,this.logs=[],AppLogger.sendLogsToServerCore(logsToSend)}}))}static debug(message,...optionalParams){AppLogger.addLog(log_1.LogType.debug,message,optionalParams)}static info(message,...optionalParams){AppLogger.addLog(log_1.LogType.info,message,optionalParams)}static log(message,...optionalParams){AppLogger.addLog(log_1.LogType.log,message,optionalParams)}static warn(message,...optionalParams){AppLogger.addLog(log_1.LogType.warn,message,optionalParams)}static error(message,...optionalParams){AppLogger.addLog(log_1.LogType.error,message,optionalParams)}static critical(message,...optionalParams){AppLogger.addLog(log_1.LogType.critical,message,optionalParams)}static addLog(type,message,optionalParams){this.checkLogType(type)?this.isRunningLocal&&!this.sendLogsOnLocal?AppLogger.logToConsole(type,message,optionalParams):AppLogger.sendLogsToServer(type,message,optionalParams):AppLogger.logToConsole(type,message,optionalParams)}static logToConsole(type,message,optionalParams){if(this.consoleLogsOnLocal)switch(type){case log_1.LogType.debug:console.debug(message,optionalParams);break;case log_1.LogType.critical:case log_1.LogType.error:console.error(message,optionalParams);break;case log_1.LogType.info:console.info(message,optionalParams);break;case log_1.LogType.log:console.log(message,optionalParams);break;case log_1.LogType.warn:console.warn(message,optionalParams);break;default:console.warn("Unkown LogType: ",type)}}static sendLogsToServer(type,message,...optionalParams){var _a;const log={logType:type,message:message,optionalParams:[],createdAt:new Date};if(optionalParams.forEach((op=>op.forEach((o=>log.optionalParams.push(o))))),this.logs.push(log),this.logs.length>=this.maxLogsCount&&!this.isSending||null!=((null===(_a=this.sendLogsByTypesImmediately)||void 0===_a?void 0:_a.find((l=>l==type)))||type==log_1.LogType.critical)&&!this.isSending){const logsToSend=this.logs;this.logs=[],this.isSending=!0,AppLogger.sendLogsToServerCore(logsToSend)}}static checkLogType(type){return null!=this.logLevel.find((l=>l===type))||type===log_1.LogType.critical}static sendLogsToServerCore(logsToSend){return __awaiter(this,void 0,void 0,(function*(){if(this.sendTries<this.maxSendTries)try{const last=this.endpointUrl.charAt(this.endpointUrl.length-1);yield cross_fetch_1.default(`${this.endpointUrl}${"/"===last?"logs/createLog":"/logs/createLog"}`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Auth-Key":this.apiKey},body:JSON.stringify({logs:logsToSend,userAgent:this.userAgent},((key,value)=>{if(value instanceof Error){let error={};return Object.getOwnPropertyNames(value).forEach((function(key){error[key]=value[key]})),error}return value}))});logsToSend=[],this.isSending=!1}catch(error){console.error(`AppLogger: Could not send Logs to Server try ${this.sendTries}`,error),this.sendTries++,setTimeout((()=>{AppLogger.sendLogsToServerCore(logsToSend)}),1e4)}else this.sendTries=0,this.isSending=!1,console.error("AppLogger: Could not send Logs to Server finally")}))}}exports.AppLogger=AppLogger,AppLogger.sendLogsOnLocal=!1,AppLogger.consoleLogsOnLocal=!1,AppLogger.endpointUrl="https://api-app-tracker.azurewebsites.net/",AppLogger.apiKey="",AppLogger.logs=[],AppLogger.sendTries=0,AppLogger.maxSendTries=5,AppLogger.isSending=!1,AppLogger.maxLogsCount=10,AppLogger.logLevel=[log_1.LogType.error],AppLogger.sendLogsByTypesImmediately=[log_1.LogType.error],AppLogger.isNode=!1;class AppLoggerConfig{constructor(){this.maxLogsCount=5,this.sendLogsByTypesImmediately=[log_1.LogType.error]}}exports.AppLoggerConfig=AppLoggerConfig;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))((function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator.throw(value))}catch(e){reject(e)}}function step(result){var value;result.done?resolve(result.value):(value=result.value,value instanceof P?value:new P((function(resolve){resolve(value)}))).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0});const log_1=require("./log"),cross_fetch_1=require("cross-fetch");class AppLogger{static init(apiKey,config){var _a,_b;this.apiKey=apiKey,config&&(config.endpointUrl&&(this.endpointUrl=config.endpointUrl),config.maxLogsCount&&(this.maxLogsCount=config.maxLogsCount),config.maxSendTries&&(this.maxSendTries=config.maxSendTries),config.sendLogsOnLocal&&(this.sendLogsOnLocal=config.sendLogsOnLocal),config.consoleLogsOnLocal&&(this.consoleLogsOnLocal=config.consoleLogsOnLocal),config.logLevel&&(this.logLevel=config.logLevel),config.sendLogsByTypesImmediately&&(this.sendLogsByTypesImmediately=config.sendLogsByTypesImmediately)),this.isNode="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0),this.isNode?(this.os=null===(_b=config)||void 0===_b?void 0:_b.userAgentOrOs,this.userAgent=AppLogger.setUserAgentFromOs()):this.userAgent=null===(_a=config)||void 0===_a?void 0:_a.userAgentOrOs,this.isRunningLocal=this.isNode?this.os.hostname().indexOf("local")>-1:window.location.href.indexOf("localhost")>-1,""==apiKey||this.isNode||window.addEventListener("beforeunload",(e=>{if(e.preventDefault(),e.returnValue="",!this.isSending){const logsToSend=this.logs;this.isSending=!0,this.logs=[],AppLogger.sendLogsToServerCore(logsToSend)}}))}static debug(message,...optionalParams){AppLogger.addLog(log_1.LogType.debug,message,optionalParams)}static info(message,...optionalParams){AppLogger.addLog(log_1.LogType.info,message,optionalParams)}static log(message,...optionalParams){AppLogger.addLog(log_1.LogType.log,message,optionalParams)}static warn(message,...optionalParams){AppLogger.addLog(log_1.LogType.warn,message,optionalParams)}static error(message,...optionalParams){AppLogger.addLog(log_1.LogType.error,message,optionalParams)}static critical(message,...optionalParams){AppLogger.addLog(log_1.LogType.critical,message,optionalParams)}static addLog(type,message,optionalParams){this.checkLogType(type)?this.isRunningLocal&&!this.sendLogsOnLocal?AppLogger.logToConsole(type,message,optionalParams):AppLogger.sendLogsToServer(type,message,optionalParams):AppLogger.logToConsole(type,message,optionalParams)}static logToConsole(type,message,optionalParams){if(this.consoleLogsOnLocal)switch(type){case log_1.LogType.debug:console.debug(message,optionalParams);break;case log_1.LogType.critical:case log_1.LogType.error:console.error(message,optionalParams);break;case log_1.LogType.info:console.info(message,optionalParams);break;case log_1.LogType.log:console.log(message,optionalParams);break;case log_1.LogType.warn:console.warn(message,optionalParams);break;default:console.warn("Unkown LogType: ",type)}}static sendLogsToServer(type,message,...optionalParams){var _a;const log={logType:type,message:message,optionalParams:[],createdAt:new Date};if(optionalParams.forEach((op=>op.forEach((o=>log.optionalParams.push(o))))),this.logs.push(log),this.logs.length>=this.maxLogsCount&&!this.isSending||null!=((null===(_a=this.sendLogsByTypesImmediately)||void 0===_a?void 0:_a.find((l=>l==type)))||type==log_1.LogType.critical)&&!this.isSending){const logsToSend=this.logs;this.logs=[],this.isSending=!0,AppLogger.sendLogsToServerCore(logsToSend)}}static checkLogType(type){return null!=this.logLevel.find((l=>l===type))||type===log_1.LogType.critical}static sendLogsToServerCore(logsToSend){return __awaiter(this,void 0,void 0,(function*(){if(this.sendTries<this.maxSendTries)try{const last=this.endpointUrl.charAt(this.endpointUrl.length-1);yield cross_fetch_1.default(`${this.endpointUrl}${"/"===last?"logs/createLog":"/logs/createLog"}`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Auth-Key":this.apiKey},body:JSON.stringify({logs:logsToSend,userAgent:this.userAgent},((key,value)=>{if(value instanceof Error){let error={};return Object.getOwnPropertyNames(value).forEach((function(key){error[key]=value[key]})),error}return value}))});logsToSend=[],this.isSending=!1}catch(error){console.error(`AppLogger: Could not send Logs to Server try ${this.sendTries}`,error),this.sendTries++,setTimeout((()=>{AppLogger.sendLogsToServerCore(logsToSend)}),1e4)}else this.sendTries=0,this.isSending=!1,console.error("AppLogger: Could not send Logs to Server finally")}))}static setUserAgentFromOs(){const osInfo={arch:this.os.arch(),cpus:this.os.cpus(),endianness:this.os.endianness(),freemem:this.os.freemem(),homedir:this.os.homedir(),hostname:this.os.hostname(),loadavg:this.os.loadavg(),networkInterfaces:this.os.networkInterfaces(),platform:this.os.platform(),release:this.os.release(),tmpdir:this.os.tmpdir(),totalmem:this.os.totalmem(),type:this.os.type(),uptime:this.os.uptime(),userInfo:this.os.userInfo(),version:this.os.version()};return JSON.stringify(osInfo)}}exports.AppLogger=AppLogger,AppLogger.sendLogsOnLocal=!1,AppLogger.consoleLogsOnLocal=!1,AppLogger.endpointUrl="https://api-app-tracker.azurewebsites.net/",AppLogger.apiKey="",AppLogger.logs=[],AppLogger.sendTries=0,AppLogger.maxSendTries=5,AppLogger.isSending=!1,AppLogger.maxLogsCount=10,AppLogger.logLevel=[log_1.LogType.error],AppLogger.sendLogsByTypesImmediately=[log_1.LogType.error],AppLogger.isNode=!1;class AppLoggerConfig{constructor(){this.maxLogsCount=5,this.sendLogsByTypesImmediately=[log_1.LogType.error]}}exports.AppLoggerConfig=AppLoggerConfig;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "app-tracker",
3
- "version": "1.6.0",
4
- "description": "App Tracker for Web-Applications",
3
+ "version": "1.6.1",
4
+ "description": "App Tracker Package for the App Tracker Web-Application",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "build": "tsc && npm run copyFile",
@@ -11,7 +11,7 @@
11
11
  "lint": "tslint -p tsconfig.json"
12
12
  },
13
13
  "keywords": [],
14
- "author": "Egor Kin",
14
+ "author": "Wigtertainment Ltd",
15
15
  "license": "ISC",
16
16
  "devDependencies": {
17
17
  "@types/node": "^17.0.10",