app-tracker 1.3.1 → 1.4.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.
package/.prettierrc ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "printWidth": 120,
3
+ "trailingComma": "all",
4
+ "singleQuote": true
5
+ }
package/lib/README.md ADDED
@@ -0,0 +1 @@
1
+ # App Tracker for Web Applications
package/lib/index.js ADDED
@@ -0,0 +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"),os=require("os");class AppLogger{static init(apiKey,config){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.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.userAgent=JSON.stringify(os):this.userAgent=navigator.userAgent,""==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 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){switch(type){case log_1.LogType.debug:console.debug(message,optionalParams);break;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 isRunningLocal(){return this.isNode?os.hostname().indexOf("local")>-1:window.location.href.indexOf("localhost")>-1}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 fetch(`${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){this.sendTries++,setTimeout((()=>{AppLogger.sendLogsToServerCore(logsToSend)}),1e4)}else this.sendTries=0,this.isSending=!1,console.error("AppLogger: Could not send Logs to Server")}))}}exports.AppLogger=AppLogger,AppLogger.sendLogsOnLocal=!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;
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "app-tracker",
3
+ "version": "1.4.1",
4
+ "description": "App Tracker for Web-Applications",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "build": "tsc && npm run copyFile",
8
+ "copyFile": "xcopy package.json lib && xcopy README.md lib && npm run terser",
9
+ "terser": "terser lib/index.js --compress -o lib/index.js",
10
+ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
11
+ "lint": "tslint -p tsconfig.json"
12
+ },
13
+ "keywords": [],
14
+ "author": "Egor Kin",
15
+ "license": "ISC",
16
+ "devDependencies": {
17
+ "@types/node": "^17.0.10",
18
+ "prettier": "^1.19.1",
19
+ "tslint": "^5.20.1",
20
+ "tslint-config-prettier": "^1.18.0",
21
+ "typescript": "^3.7.3"
22
+ }
23
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "app-tracker",
3
- "version": "1.3.1",
3
+ "version": "1.4.1",
4
4
  "description": "App Tracker for Web-Applications",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,7 @@
14
14
  "author": "Egor Kin",
15
15
  "license": "ISC",
16
16
  "devDependencies": {
17
+ "@types/node": "^17.0.10",
17
18
  "prettier": "^1.19.1",
18
19
  "tslint": "^5.20.1",
19
20
  "tslint-config-prettier": "^1.18.0",
package/src/index.ts ADDED
@@ -0,0 +1,179 @@
1
+ import { Log, LogType } from './log';
2
+ const os = require('os');
3
+
4
+ export class AppLogger {
5
+
6
+ public static sendLogsOnLocal: boolean = false;
7
+ private static endpointUrl: string = 'https://api-app-tracker.azurewebsites.net/';
8
+ private static apiKey: string = '';
9
+ private static logs: Log[] = [];
10
+ private static sendTries: number = 0;
11
+ private static maxSendTries: number = 5;
12
+ private static isSending: boolean = false;
13
+ private static maxLogsCount: number = 10;
14
+ private static userAgent: string;
15
+ private static logLevel: LogType[] = [LogType.error];
16
+ private static sendLogsByTypesImmediately: LogType[] | undefined = [LogType.error];
17
+ private static isNode = false;
18
+
19
+ public static init(apiKey: string, config?: AppLoggerConfig) {
20
+ this.apiKey = apiKey;
21
+ if (config) {
22
+ if (config.endpointUrl) this.endpointUrl = config.endpointUrl;
23
+ if (config.maxLogsCount) this.maxLogsCount = config.maxLogsCount;
24
+ if (config.maxSendTries) this.maxSendTries = config.maxSendTries;
25
+ if (config.sendLogsOnLocal) this.sendLogsOnLocal = config.sendLogsOnLocal;
26
+ if (config.logLevel) this.logLevel = config.logLevel;
27
+ if (config.sendLogsByTypesImmediately) this.sendLogsByTypesImmediately = config.sendLogsByTypesImmediately;
28
+ }
29
+ this.isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
30
+ if (!this.isNode)
31
+ this.userAgent = navigator.userAgent;
32
+ else {
33
+ this.userAgent = JSON.stringify(os);
34
+ }
35
+ if (apiKey != '' && !this.isNode) {
36
+ window.addEventListener('beforeunload', (e) => {
37
+ e.preventDefault();
38
+ e.returnValue = '';
39
+ if (!this.isSending) {
40
+ const logsToSend = this.logs;
41
+ this.isSending = true;
42
+ this.logs = [];
43
+ AppLogger.sendLogsToServerCore(logsToSend);
44
+ }
45
+ });
46
+ }
47
+ }
48
+
49
+ public static debug(message?: string, ...optionalParams: any[]) {
50
+ AppLogger.addLog(LogType.debug, message, optionalParams);
51
+ }
52
+
53
+ public static info(message?: string, ...optionalParams: any[]) {
54
+ AppLogger.addLog(LogType.info, message, optionalParams);
55
+ }
56
+
57
+ public static log(message?: string, ...optionalParams: any[]) {
58
+ AppLogger.addLog(LogType.log, message, optionalParams);
59
+ }
60
+
61
+ public static warn(message?: string, ...optionalParams: any[]) {
62
+ AppLogger.addLog(LogType.warn, message, optionalParams);
63
+ }
64
+
65
+ public static error(message?: string, ...optionalParams: any[]) {
66
+ AppLogger.addLog(LogType.error, message, optionalParams);
67
+ }
68
+
69
+ private static addLog(type: LogType, message: string | undefined, optionalParams: any[]) {
70
+ if (this.checkLogType(type)) {
71
+ if (this.isRunningLocal() && !this.sendLogsOnLocal) {
72
+ AppLogger.logToConsole(type, message, optionalParams);
73
+ } else {
74
+ AppLogger.sendLogsToServer(type, message, optionalParams);
75
+ }
76
+ } else {
77
+ AppLogger.logToConsole(type, message, optionalParams);
78
+ }
79
+ }
80
+
81
+ private static logToConsole(type: LogType, message: string | undefined, optionalParams: any[]) {
82
+ switch (type) {
83
+ case LogType.debug:
84
+ console.debug(message, optionalParams);
85
+ break;
86
+ case LogType.error:
87
+ console.error(message, optionalParams);
88
+ break;
89
+ case LogType.info:
90
+ console.info(message, optionalParams);
91
+ break;
92
+ case LogType.log:
93
+ console.log(message, optionalParams);
94
+ break;
95
+ case LogType.warn:
96
+ console.warn(message, optionalParams);
97
+ break;
98
+ default:
99
+ console.warn("Unkown LogType: ", type);
100
+ break;
101
+ }
102
+ }
103
+
104
+ private static isRunningLocal(): boolean {
105
+ return this.isNode ? os.hostname().indexOf("local") > -1 : window.location.href.indexOf('localhost') > -1;
106
+ }
107
+
108
+ private static sendLogsToServer(type: LogType, message?: string, ...optionalParams: any[][]) {
109
+ const log = { logType: type, message: message, optionalParams: [], createdAt: new Date() } as Log;
110
+ optionalParams.forEach(op => op.forEach(o => log.optionalParams.push(o)));
111
+ this.logs.push(log);
112
+ if ((this.logs.length >= this.maxLogsCount && !this.isSending) ||
113
+ ((this.sendLogsByTypesImmediately?.find(l => l == type) || type == LogType.critical) != null && !this.isSending)) {
114
+ const logsToSend = this.logs;
115
+ this.logs = [];
116
+ this.isSending = true;
117
+ AppLogger.sendLogsToServerCore(logsToSend);
118
+ }
119
+ }
120
+
121
+ private static checkLogType(type: LogType): boolean {
122
+ if (this.logLevel.find(l => l === type) != null || type === LogType.critical) {
123
+ return true;
124
+ }
125
+ else {
126
+ return false;
127
+ }
128
+ }
129
+
130
+ private static async sendLogsToServerCore(logsToSend: Log[]) {
131
+ if (this.sendTries < this.maxSendTries) {
132
+ try {
133
+ const last = this.endpointUrl.charAt(this.endpointUrl.length - 1);
134
+ const response = await fetch(`${this.endpointUrl}${last === '/' ? 'logs/createLog' : '/logs/createLog'}`,
135
+ {
136
+ method: 'POST',
137
+ headers: {
138
+ 'Accept': 'application/json',
139
+ 'Content-Type': 'application/json',
140
+ 'X-Auth-Key': this.apiKey
141
+ },
142
+ body: JSON.stringify({ logs: logsToSend, userAgent: this.userAgent }, (key: string, value: any) => {
143
+ if (value instanceof Error) {
144
+ let error: any = <any>{};
145
+
146
+ Object.getOwnPropertyNames(value).forEach(function (key) {
147
+ error[key] = (<any>value)[key];
148
+ });
149
+ return error;
150
+ }
151
+ return value;
152
+ })
153
+ });
154
+
155
+ logsToSend = [];
156
+ this.isSending = false;
157
+ } catch (error) {
158
+ this.sendTries++;
159
+ setTimeout(() => {
160
+ AppLogger.sendLogsToServerCore(logsToSend);
161
+ }, 10000);
162
+ }
163
+ } else {
164
+ this.sendTries = 0;
165
+ this.isSending = false;
166
+ console.error('AppLogger: Could not send Logs to Server');
167
+ }
168
+ }
169
+ }
170
+
171
+ export class AppLoggerConfig {
172
+ public sendLogsOnLocal?: boolean | undefined;
173
+ public endpointUrl?: string | undefined;
174
+ public maxSendTries?: number | undefined;
175
+ public maxLogsCount?: number | undefined = 5;
176
+ /** Defines which log types should be sent to the server */
177
+ public logLevel?: LogType[] | undefined;
178
+ public sendLogsByTypesImmediately?: LogType[] | undefined = [LogType.error];
179
+ }
package/src/log.ts ADDED
@@ -0,0 +1,8 @@
1
+ export class Log {
2
+ public logType: LogType = LogType.log;
3
+ public message: any;
4
+ public optionalParams: any[] = [];
5
+ public createdAt: Date = new Date();
6
+ }
7
+
8
+ export enum LogType { debug, info, log, warn, error, critical }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es6",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./lib",
7
+ "strict": true
8
+ },
9
+ "include": [
10
+ "src"
11
+ ],
12
+ "exclude": [
13
+ "node_modules",
14
+ "**/__tests__/*"
15
+ ]
16
+ }
package/tslint.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": [
3
+ "tslint:recommended",
4
+ "tslint-config-prettier"
5
+ ],
6
+ "rules": {
7
+ "no-console": false
8
+ }
9
+ }
package/index.d.ts DELETED
@@ -1,35 +0,0 @@
1
- import { LogType } from './log';
2
- export declare class AppLogger {
3
- static sendLogsOnLocal: boolean;
4
- static init(apiKey: string, config?: AppLoggerConfig): void;
5
- static debug(message?: string, ...optionalParams: any[]): void;
6
- static info(message?: string, ...optionalParams: any[]): void;
7
- static log(message?: string, ...optionalParams: any[]): void;
8
- static warn(message?: string, ...optionalParams: any[]): void;
9
- static error(message?: string, ...optionalParams: any[]): void;
10
- private static addLog;
11
- private static logToConsole;
12
- private static endpointUrl;
13
- private static apiKey;
14
- private static logs;
15
- private static sendTries;
16
- private static maxSendTries;
17
- private static isSending;
18
- private static maxLogsCount;
19
- private static userAgent;
20
- private static logLevel;
21
- private static sendLogsByTypesImmediately;
22
- private static isRunningLocal;
23
- private static sendLogsToServer;
24
- private static checkLogType;
25
- private static sendLogsToServerCore;
26
- }
27
- export declare class AppLoggerConfig {
28
- sendLogsOnLocal?: boolean | undefined;
29
- endpointUrl?: string | undefined;
30
- maxSendTries?: number | undefined;
31
- maxLogsCount?: number | undefined;
32
- /** Defines which log types should be sent to the server */
33
- logLevel?: LogType[] | undefined;
34
- sendLogsByTypesImmediately?: LogType[] | undefined;
35
- }
package/index.js DELETED
@@ -1 +0,0 @@
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");class AppLogger{static init(apiKey,config){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.logLevel&&(this.logLevel=config.logLevel),config.sendLogsByTypesImmediately&&(this.sendLogsByTypesImmediately=config.sendLogsByTypesImmediately)),navigator?this.userAgent=navigator.userAgent:this.userAgent=JSON.stringify({client:{type:"Backend Server"}}),""!=apiKey&&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 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){switch(type){case log_1.LogType.debug:console.debug(message,optionalParams);break;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 isRunningLocal(){return window.location.href.indexOf("localhost")>0}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)))&&!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))}static sendLogsToServerCore(logsToSend){return __awaiter(this,void 0,void 0,(function*(){if(this.sendTries<this.maxSendTries)try{yield fetch(`${this.endpointUrl}`,{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){this.sendTries++,setTimeout((()=>{AppLogger.sendLogsToServerCore(logsToSend)}),1e4)}else this.sendTries=0,this.isSending=!1,console.error("AppLogger: Could not send Logs to Server")}))}}exports.AppLogger=AppLogger,AppLogger.sendLogsOnLocal=!1,AppLogger.endpointUrl="",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];class AppLoggerConfig{constructor(){this.maxLogsCount=5,this.sendLogsByTypesImmediately=[log_1.LogType.error]}}exports.AppLoggerConfig=AppLoggerConfig;
package/log.d.ts DELETED
@@ -1,13 +0,0 @@
1
- export declare class Log {
2
- logType: LogType;
3
- message: any;
4
- optionalParams: any[];
5
- createdAt: Date;
6
- }
7
- export declare enum LogType {
8
- debug = 0,
9
- info = 1,
10
- log = 2,
11
- warn = 3,
12
- error = 4
13
- }
package/log.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class Log {
4
- constructor() {
5
- this.logType = LogType.log;
6
- this.optionalParams = [];
7
- this.createdAt = new Date();
8
- }
9
- }
10
- exports.Log = Log;
11
- var LogType;
12
- (function (LogType) {
13
- LogType[LogType["debug"] = 0] = "debug";
14
- LogType[LogType["info"] = 1] = "info";
15
- LogType[LogType["log"] = 2] = "log";
16
- LogType[LogType["warn"] = 3] = "warn";
17
- LogType[LogType["error"] = 4] = "error";
18
- })(LogType = exports.LogType || (exports.LogType = {}));