corexxx 1.0.133 → 1.0.135
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/.vscode/settings.json +3 -0
- package/dist/danalytics.d.ts +36 -0
- package/dist/danalytics.js +113 -0
- package/dist/danalytics.js.map +1 -0
- package/dist/dlog.d.ts +2 -12
- package/dist/dlog.js +14 -36
- package/dist/dlog.js.map +1 -1
- package/dist/dnetwork.d.ts +34 -3
- package/dist/dnetwork.js +64 -39
- package/dist/dnetwork.js.map +1 -1
- package/package.json +1 -1
- package/src/danalytics.ts +112 -0
- package/src/dlog.ts +16 -45
- package/src/dnetwork.ts +108 -47
- package/test/danalytics.test.ts +29 -0
- package/test/dlog.test.ts +12 -7
- package/test/dnetwork.test.ts +140 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DAnalytics - Simple Analytics/Event Tracking Utility
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* const analytics = new DAnalytics({ appName: "myApp", debug: true });
|
|
6
|
+
* analytics.trackEvent("user_login", { userId: 123 });
|
|
7
|
+
* analytics.trackPageView("/home");
|
|
8
|
+
*/
|
|
9
|
+
export interface TConfig {
|
|
10
|
+
server: string;
|
|
11
|
+
app_id: string;
|
|
12
|
+
app_version?: string;
|
|
13
|
+
device_os?: string;
|
|
14
|
+
device_id?: string;
|
|
15
|
+
device_api?: string;
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare class DAnalytics {
|
|
19
|
+
private config;
|
|
20
|
+
private sessionKey;
|
|
21
|
+
private eventQueue;
|
|
22
|
+
constructor(config: TConfig);
|
|
23
|
+
/**
|
|
24
|
+
* Initialize analytics session by calling the launch API.
|
|
25
|
+
*/
|
|
26
|
+
private initSession;
|
|
27
|
+
/**
|
|
28
|
+
* Track a specific user action by calling the /api/analytics/action endpoint.
|
|
29
|
+
* @param type The type of action (e.g., "click")
|
|
30
|
+
* @param target_id The target element's ID (e.g., "btn1")
|
|
31
|
+
* @param tag A tag describing the action (e.g., "btn_click")
|
|
32
|
+
* @returns The response from the server
|
|
33
|
+
*/
|
|
34
|
+
trackAction(type: string, target_id: string, tag: string): Promise<any>;
|
|
35
|
+
trackConsoleLog(): Promise<any>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* DAnalytics - Simple Analytics/Event Tracking Utility
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* const analytics = new DAnalytics({ appName: "myApp", debug: true });
|
|
7
|
+
* analytics.trackEvent("user_login", { userId: 123 });
|
|
8
|
+
* analytics.trackPageView("/home");
|
|
9
|
+
*/
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.DAnalytics = void 0;
|
|
21
|
+
const dlog_1 = require("./dlog");
|
|
22
|
+
const dnetwork_1 = require("./dnetwork");
|
|
23
|
+
class DAnalytics {
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.sessionKey = null;
|
|
26
|
+
this.eventQueue = [];
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.initSession();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Initialize analytics session by calling the launch API.
|
|
32
|
+
*/
|
|
33
|
+
initSession() {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
const url = `http://${this.config.server}/api/analytics/launch`;
|
|
36
|
+
const payload = {
|
|
37
|
+
app_id: this.config.app_id,
|
|
38
|
+
app_version: this.config.app_version || "0.0",
|
|
39
|
+
device_os: this.config.device_os || "unknown",
|
|
40
|
+
device_id: this.config.device_id || "unknown",
|
|
41
|
+
device_api: this.config.device_api || "unknown",
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
const resp = yield dnetwork_1.dnetwork.postSimpleStore(url, payload);
|
|
45
|
+
this.sessionKey = resp.out.session;
|
|
46
|
+
if (this.config.debug) {
|
|
47
|
+
dlog_1.dlog.d("[DAnalytics] Session initialized:", this.sessionKey);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (this.config.debug) {
|
|
52
|
+
// eslint-disable-next-line no-console
|
|
53
|
+
dlog_1.dlog.d("[DAnalytics] Failed to initialize session", err);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Track a specific user action by calling the /api/analytics/action endpoint.
|
|
60
|
+
* @param type The type of action (e.g., "click")
|
|
61
|
+
* @param target_id The target element's ID (e.g., "btn1")
|
|
62
|
+
* @param tag A tag describing the action (e.g., "btn_click")
|
|
63
|
+
* @returns The response from the server
|
|
64
|
+
*/
|
|
65
|
+
trackAction(type, target_id, tag) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
if (!this.sessionKey) {
|
|
68
|
+
if (this.config.debug) {
|
|
69
|
+
// eslint-disable-next-line no-console
|
|
70
|
+
console.warn("[DAnalytics] Session not initialized, cannot track action.");
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const url = `http://${this.config.server}/api/analytics/action`;
|
|
75
|
+
const payload = {
|
|
76
|
+
app_id: this.config.app_id,
|
|
77
|
+
session: this.sessionKey,
|
|
78
|
+
type,
|
|
79
|
+
action_id: target_id,
|
|
80
|
+
action_tag: tag,
|
|
81
|
+
};
|
|
82
|
+
try {
|
|
83
|
+
const data = yield dnetwork_1.dnetwork.postSimpleStore(url, payload);
|
|
84
|
+
if (this.config.debug) {
|
|
85
|
+
// eslint-disable-next-line no-console
|
|
86
|
+
console.log("[DAnalytics] Tracked action:", data);
|
|
87
|
+
}
|
|
88
|
+
return data;
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
if (this.config.debug) {
|
|
92
|
+
// eslint-disable-next-line no-console
|
|
93
|
+
console.error("[DAnalytics] Failed to track action", err);
|
|
94
|
+
}
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
// This will log anything in the console.
|
|
100
|
+
// This will flash all log history
|
|
101
|
+
trackConsoleLog() {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const url = `http://${this.config.server}/api/analytics/adhoc`;
|
|
104
|
+
return yield dnetwork_1.dnetwork.postSimpleStore(url, {
|
|
105
|
+
app_id: this.config.app_id,
|
|
106
|
+
session: this.sessionKey,
|
|
107
|
+
console_log: dlog_1.dlog.getLogHistoryAndFlash(),
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.DAnalytics = DAnalytics;
|
|
113
|
+
//# sourceMappingURL=danalytics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"danalytics.js","sourceRoot":"","sources":["../src/danalytics.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;AAEH,iCAA8B;AAC9B,yCAAsC;AAYtC,MAAa,UAAU;IAKrB,YAAY,MAAe;QAHnB,eAAU,GAAkB,IAAI,CAAC;QACjC,eAAU,GAAmD,EAAE,CAAC;QAGtE,IAAI,CAAC,MAAM,GAAG,MAAM,CA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
@@ -49,18 +49,8 @@ export declare namespace dlog {
|
|
|
49
49
|
*/
|
|
50
50
|
export function ex(error: Error, ignore?: boolean, msg?: string): void;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
* Redacts sensitive keys.
|
|
52
|
+
* Returns the log history and clears it.
|
|
54
53
|
*/
|
|
55
|
-
export function
|
|
56
|
-
/**
|
|
57
|
-
* Logs all entries of a Map, redacting sensitive keys.
|
|
58
|
-
*/
|
|
59
|
-
export function map(map: Map<string, unknown>): void;
|
|
60
|
-
/**
|
|
61
|
-
* Placeholder for trace logging.
|
|
62
|
-
* To be implemented if needed.
|
|
63
|
-
*/
|
|
64
|
-
export function trace(): void;
|
|
54
|
+
export function getLogHistoryAndFlash(): string[];
|
|
65
55
|
export {};
|
|
66
56
|
}
|
package/dist/dlog.js
CHANGED
|
@@ -22,6 +22,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
22
22
|
exports.dlog = void 0;
|
|
23
23
|
var dlog;
|
|
24
24
|
(function (dlog) {
|
|
25
|
+
// This will store any data other than debug
|
|
26
|
+
const logs_history = [];
|
|
25
27
|
let config = { isDev: true, enablePII: false };
|
|
26
28
|
// Unicode icons for log levels
|
|
27
29
|
const ICONS = {
|
|
@@ -124,6 +126,9 @@ var dlog;
|
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
128
|
const message = `[${icon}${level}] [${now}][${caller}] ${convertMsg(msg)}${msg2Str}`;
|
|
129
|
+
if (level != "DEBUG") {
|
|
130
|
+
logs_history.push(message);
|
|
131
|
+
}
|
|
127
132
|
console.log(message);
|
|
128
133
|
}
|
|
129
134
|
/**
|
|
@@ -165,46 +170,19 @@ var dlog;
|
|
|
165
170
|
const caller = getCallerFunctionName();
|
|
166
171
|
const icon = ICONS.EXCEPTION;
|
|
167
172
|
const stack = !ignore && error.stack ? `\n${error.stack}` : "";
|
|
168
|
-
|
|
173
|
+
let message = `[${now}][${icon} EXCEPTION][${caller}] ${msg !== null && msg !== void 0 ? msg : ""} ${error.message}${stack}`;
|
|
174
|
+
console.log(message);
|
|
175
|
+
logs_history.push(message);
|
|
169
176
|
}
|
|
170
177
|
dlog.ex = ex;
|
|
171
178
|
/**
|
|
172
|
-
*
|
|
173
|
-
* Redacts sensitive keys.
|
|
174
|
-
*/
|
|
175
|
-
function obj(obj, msg = "DEBUG OBJECT") {
|
|
176
|
-
if (!config.isDev)
|
|
177
|
-
return;
|
|
178
|
-
const now = new Date().toLocaleString();
|
|
179
|
-
const caller = getCallerFunctionName();
|
|
180
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} START =====`);
|
|
181
|
-
console.log(convertMsg(obj));
|
|
182
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} END =====`);
|
|
183
|
-
}
|
|
184
|
-
dlog.obj = obj;
|
|
185
|
-
/**
|
|
186
|
-
* Logs all entries of a Map, redacting sensitive keys.
|
|
187
|
-
*/
|
|
188
|
-
function map(map) {
|
|
189
|
-
if (!config.isDev)
|
|
190
|
-
return;
|
|
191
|
-
const now = new Date().toLocaleString();
|
|
192
|
-
const caller = getCallerFunctionName();
|
|
193
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP START =====`);
|
|
194
|
-
for (const [key, value] of map.entries()) {
|
|
195
|
-
const displayValue = convertMsg({ [key]: value });
|
|
196
|
-
console.log(`${JSON.stringify(key)} ==> ${displayValue}`);
|
|
197
|
-
}
|
|
198
|
-
console.log(`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP END =====`);
|
|
199
|
-
}
|
|
200
|
-
dlog.map = map;
|
|
201
|
-
/**
|
|
202
|
-
* Placeholder for trace logging.
|
|
203
|
-
* To be implemented if needed.
|
|
179
|
+
* Returns the log history and clears it.
|
|
204
180
|
*/
|
|
205
|
-
function
|
|
206
|
-
|
|
181
|
+
function getLogHistoryAndFlash() {
|
|
182
|
+
const history = [...logs_history];
|
|
183
|
+
logs_history.length = 0;
|
|
184
|
+
return history;
|
|
207
185
|
}
|
|
208
|
-
dlog.
|
|
186
|
+
dlog.getLogHistoryAndFlash = getLogHistoryAndFlash;
|
|
209
187
|
})(dlog || (exports.dlog = dlog = {}));
|
|
210
188
|
//# sourceMappingURL=dlog.js.map
|
package/dist/dlog.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dlog.js","sourceRoot":"","sources":["../src/dlog.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;AAEH,IAAiB,IAAI,
|
|
1
|
+
{"version":3,"file":"dlog.js","sourceRoot":"","sources":["../src/dlog.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;AAEH,IAAiB,IAAI,CA2KpB;AA3KD,WAAiB,IAAI;IAMnB,4CAA4C;IAC5C,MAAM,YAAY,GAAa,EAAE,CAAC;IAElC,IAAI,MAAM,GAAe,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAE3D,+BAA+B;IAC/B,MAAM,KAAK,GAAG;QACZ,KAAK,EAAE,IAAI,EAAE,cAAc;QAC3B,IAAI,EAAE,IAAI;QACV,OAAO,EAAE,GAAG;QACZ,KAAK,EAAE,IAAI;QACX,SAAS,EAAE,IAAI;KAChB,CAAC;IAEF;;;OAGG;IACH,SAAgB,SAAS,CAAC,GAAe;QACvC,MAAM,mCAAQ,MAAM,GAAK,GAAG,CAAE,CAAC;IACjC,CAAC;IAFe,cAAS,YAExB,CAAA;IAED;;;OAGG;IACH,SAAS,UAAU,CAAC,KAAc,EAAE,KAAK,GAAG,IAAI;QAC9C,SAAS,MAAM,CAAC,GAAQ;YACtB,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS;gBAAE,OAAO,GAAG,CAAC;YAClD,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;gBAAE,OAAO,GAAG,CAAC,GAAG,CAAC,M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,cAAc,EAAE,CAAC;QACxC,MAAM,MAAM,GAAG,qBAAqB,EAAE,CAAC;QACvC,MAAM,IAAI,GAAG,KAAK,CAAC,SAAS,CAAC;QAC7B,MAAM,KAAK,GAAG,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/D,IAAI,OAAO,GAAG,IAAI,GAAG,KAAK,IAAI,eAAe,MAAM,KAAK,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,EAAE,IAC/D,KAAK,CAAC,OACR,GAAG,KAAK,EAAE,CAAC;QACX,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACrB,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAXe,OAAE,KAWjB,CAAA;IAED;;OAEG;IACH,SAAgB,qBAAqB;QACnC,MAAM,OAAO,GAAG,CAAC,GAAG,YAAY,CAAC,CAAC;QAClC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC;QACxB,OAAO,OAAO,CAAC;IACjB,CAAC;IAJe,0BAAqB,wBAIpC,CAAA;AACH,CAAC,EA3KgB,IAAI,oBAAJ,IAAI,QA2KpB"}
|
package/dist/dnetwork.d.ts
CHANGED
|
@@ -1,11 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dnetwork.ts - Network Utility Functions for HTTP Requests
|
|
3
|
+
*
|
|
4
|
+
* Provides a set of async functions for GET/POST requests using axios,
|
|
5
|
+
* with integrated logging, assertion, and error handling.
|
|
6
|
+
* - All requests log activity via dlog
|
|
7
|
+
* - All responses are checked for HTTP success
|
|
8
|
+
* - JSON responses are parsed and returned as objects
|
|
9
|
+
* - SimpleStore helpers for common API patterns
|
|
10
|
+
*/
|
|
1
11
|
import { AxiosRequestConfig } from "axios";
|
|
2
12
|
import { TObject } from "./dtypes";
|
|
3
13
|
export declare namespace dnetwork {
|
|
14
|
+
/**
|
|
15
|
+
* Perform a GET request and return the response data.
|
|
16
|
+
*/
|
|
4
17
|
function get(url: string, config?: AxiosRequestConfig): Promise<any>;
|
|
5
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Perform a GET request and return the response as a JSON object.
|
|
20
|
+
*/
|
|
21
|
+
function getJson(url: string, config?: AxiosRequestConfig): Promise<object>;
|
|
22
|
+
/**
|
|
23
|
+
* Perform a POST request and return the response data.
|
|
24
|
+
*/
|
|
6
25
|
function post(url: string, data: any, config?: AxiosRequestConfig): Promise<any>;
|
|
7
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Perform a POST request and return the response as a JSON object.
|
|
28
|
+
*/
|
|
29
|
+
function postJson(url: string, data: TObject, config?: AxiosRequestConfig): Promise<object>;
|
|
30
|
+
/**
|
|
31
|
+
* GET request to a SimpleStore endpoint, expects {status: "success", ...}
|
|
32
|
+
*/
|
|
8
33
|
function getSimpleStore(url: string, config?: AxiosRequestConfig): Promise<any>;
|
|
34
|
+
/**
|
|
35
|
+
* POST request to a SimpleStore endpoint, expects {status: "success", ...}
|
|
36
|
+
*/
|
|
9
37
|
function postSimpleStore(url: string, data: TObject, config?: AxiosRequestConfig): Promise<any>;
|
|
10
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Perform a GET request with a browser-like user-agent header.
|
|
40
|
+
*/
|
|
41
|
+
function getAsFakeBrowser(url: string, config?: AxiosRequestConfig): Promise<any>;
|
|
11
42
|
}
|
package/dist/dnetwork.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* dnetwork.ts - Network Utility Functions for HTTP Requests
|
|
4
|
+
*
|
|
5
|
+
* Provides a set of async functions for GET/POST requests using axios,
|
|
6
|
+
* with integrated logging, assertion, and error handling.
|
|
7
|
+
* - All requests log activity via dlog
|
|
8
|
+
* - All responses are checked for HTTP success
|
|
9
|
+
* - JSON responses are parsed and returned as objects
|
|
10
|
+
* - SimpleStore helpers for common API patterns
|
|
11
|
+
*/
|
|
2
12
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
13
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
14
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -14,103 +24,118 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
25
|
exports.dnetwork = void 0;
|
|
16
26
|
const axios_1 = __importDefault(require("axios"));
|
|
17
|
-
const underscore_1 = __importDefault(require("underscore"));
|
|
18
27
|
const dassert_1 = require("./dassert");
|
|
19
28
|
const dlog_1 = require("./dlog");
|
|
20
29
|
var dnetwork;
|
|
21
30
|
(function (dnetwork) {
|
|
22
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Perform a GET request and return the response data.
|
|
33
|
+
*/
|
|
23
34
|
function get(url, config) {
|
|
24
35
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
dlog_1.dlog.d(`
|
|
26
|
-
|
|
27
|
-
dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `
|
|
36
|
+
dlog_1.dlog.d(`GET ${url}`);
|
|
37
|
+
const result = yield axios_1.default.get(url, config);
|
|
38
|
+
dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `GET request to URL ${url} failed with status ${result.status}`);
|
|
28
39
|
return result.data;
|
|
29
40
|
});
|
|
30
41
|
}
|
|
31
42
|
dnetwork.get = get;
|
|
32
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Perform a GET request and return the response as a JSON object.
|
|
45
|
+
*/
|
|
33
46
|
function getJson(url, config) {
|
|
34
47
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
|
|
36
|
-
if (
|
|
48
|
+
const res = yield get(url, config);
|
|
49
|
+
if (typeof res === "object" && res !== null) {
|
|
37
50
|
return res;
|
|
38
51
|
}
|
|
39
|
-
|
|
52
|
+
try {
|
|
53
|
+
return JSON.parse(res);
|
|
54
|
+
}
|
|
55
|
+
catch (_a) {
|
|
56
|
+
throw new Error("Response is not valid JSON");
|
|
57
|
+
}
|
|
40
58
|
});
|
|
41
59
|
}
|
|
42
60
|
dnetwork.getJson = getJson;
|
|
43
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Perform a POST request and return the response data.
|
|
63
|
+
*/
|
|
44
64
|
function post(url, data, config) {
|
|
45
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
dlog_1.dlog.d(`
|
|
47
|
-
|
|
48
|
-
dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `
|
|
66
|
+
dlog_1.dlog.d(`POST ${url}`, data);
|
|
67
|
+
const result = yield axios_1.default.post(url, data, config);
|
|
68
|
+
dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `POST request to URL ${url} failed with status ${result.status}`);
|
|
49
69
|
return result.data;
|
|
50
70
|
});
|
|
51
71
|
}
|
|
52
72
|
dnetwork.post = post;
|
|
53
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Perform a POST request and return the response as a JSON object.
|
|
75
|
+
*/
|
|
54
76
|
function postJson(url, data, config) {
|
|
55
77
|
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
|
|
57
|
-
if (
|
|
78
|
+
const res = yield post(url, data, config);
|
|
79
|
+
if (typeof res === "object" && res !== null) {
|
|
58
80
|
return res;
|
|
59
81
|
}
|
|
60
|
-
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(res);
|
|
84
|
+
}
|
|
85
|
+
catch (_a) {
|
|
86
|
+
throw new Error("Response is not valid JSON");
|
|
87
|
+
}
|
|
61
88
|
});
|
|
62
89
|
}
|
|
63
90
|
dnetwork.postJson = postJson;
|
|
64
|
-
|
|
91
|
+
/**
|
|
92
|
+
* GET request to a SimpleStore endpoint, expects {status: "success", ...}
|
|
93
|
+
*/
|
|
65
94
|
function getSimpleStore(url, config) {
|
|
66
95
|
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
dlog_1.dlog.d(
|
|
68
|
-
|
|
96
|
+
dlog_1.dlog.d(`GET ${url}`);
|
|
97
|
+
const response = yield axios_1.default.get(url, config);
|
|
69
98
|
const jsondata = response.data;
|
|
70
99
|
if (jsondata.status === "success") {
|
|
71
|
-
dlog_1.dlog.d("fetch success
|
|
100
|
+
dlog_1.dlog.d("fetch success");
|
|
72
101
|
return jsondata;
|
|
73
102
|
}
|
|
74
103
|
else {
|
|
75
|
-
dlog_1.dlog.
|
|
104
|
+
dlog_1.dlog.e(`GET failed: ${url}`, jsondata);
|
|
76
105
|
throw new Error(jsondata.msg);
|
|
77
106
|
}
|
|
78
107
|
});
|
|
79
108
|
}
|
|
80
109
|
dnetwork.getSimpleStore = getSimpleStore;
|
|
81
|
-
|
|
110
|
+
/**
|
|
111
|
+
* POST request to a SimpleStore endpoint, expects {status: "success", ...}
|
|
112
|
+
*/
|
|
82
113
|
function postSimpleStore(url, data, config) {
|
|
83
114
|
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
-
dlog_1.dlog.d(`
|
|
85
|
-
|
|
115
|
+
dlog_1.dlog.d(`POST ${url}`, data);
|
|
116
|
+
const response = yield axios_1.default.post(url, data, config);
|
|
86
117
|
const jsondata = response.data;
|
|
87
118
|
if (jsondata.status === "success") {
|
|
88
|
-
dlog_1.dlog.d("
|
|
119
|
+
dlog_1.dlog.d("POST success");
|
|
89
120
|
return jsondata;
|
|
90
121
|
}
|
|
91
122
|
else {
|
|
92
|
-
dlog_1.dlog.
|
|
123
|
+
dlog_1.dlog.e(`POST failed: ${url}`, jsondata);
|
|
93
124
|
throw new Error(jsondata.msg);
|
|
94
125
|
}
|
|
95
126
|
});
|
|
96
127
|
}
|
|
97
128
|
dnetwork.postSimpleStore = postSimpleStore;
|
|
98
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Perform a GET request with a browser-like user-agent header.
|
|
131
|
+
*/
|
|
132
|
+
function getAsFakeBrowser(url, config) {
|
|
99
133
|
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36",
|
|
103
|
-
},
|
|
104
|
-
});
|
|
134
|
+
const headers = Object.assign({ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36" }, ((config === null || config === void 0 ? void 0 : config.headers) || {}));
|
|
135
|
+
const resp = yield axios_1.default.get(url, Object.assign(Object.assign({}, config), { headers }));
|
|
105
136
|
return resp.data;
|
|
106
137
|
});
|
|
107
138
|
}
|
|
108
139
|
dnetwork.getAsFakeBrowser = getAsFakeBrowser;
|
|
109
140
|
})(dnetwork || (exports.dnetwork = dnetwork = {}));
|
|
110
|
-
// test
|
|
111
|
-
(function test() {
|
|
112
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
-
console.log(yield dnetwork.postJson("https://simplestore.dipankar.co.in/api/test/insert", { name: 1 }));
|
|
114
|
-
});
|
|
115
|
-
}); //();
|
|
116
141
|
//# sourceMappingURL=dnetwork.js.map
|
package/dist/dnetwork.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dnetwork.js","sourceRoot":"","sources":["../src/dnetwork.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"dnetwork.js","sourceRoot":"","sources":["../src/dnetwork.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;;;;;;;;;;;;AAEH,kDAAkD;AAClD,uCAAoC;AACpC,iCAA8B;AAG9B,IAAiB,QAAQ,CA6HxB;AA7HD,WAAiB,QAAQ;IACvB;;OAEG;IACH,SAAsB,GAAG,CACvB,GAAW,EACX,MAA2B;;YAE3B,WAAI,CAAC,CA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CAAC,CAAC,QAAQ,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC;YAC5B,MAAM,QAAQ,GAAG,MAAM,eAAK,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;YACrD,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC;YAC/B,IAAI,QAAQ,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;gBAClC,WAAI,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC;gBACvB,OAAO,QAAQ,CAAC;YAClB,CAAC;iBAAM,CAAC;gBACN,WAAI,CAAC,CAAC,CAAC,gBAAgB,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;gBACxC,MAAM,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;KAAA;IAfqB,wBAAe,kBAepC,CAAA;IAED;;OAEG;IACH,SAAsB,gBAAgB,CACpC,GAAW,EACX,MAA2B;;YAE3B,MAAM,OAAO,mBACX,YAAY,EACV,qHAAqH,IACpH,CAAC,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,KAAI,EAAE,CAAC,CAC3B,CAAC;YACF,MAAM,IAAI,GAAG,MAAM,eAAK,CAAC,GAAG,CAAC,GAAG,kCAAO,MAAM,KAAE,OAAO,IAAG,CAAC;YAC1D,OAAO,IAAI,CAAC,IAAI,CAAC;QACnB,CAAC;KAAA;IAXqB,yBAAgB,mBAWrC,CAAA;AACH,CAAC,EA7HgB,QAAQ,wBAAR,QAAQ,QA6HxB"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DAnalytics - Simple Analytics/Event Tracking Utility
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* const analytics = new DAnalytics({ appName: "myApp", debug: true });
|
|
6
|
+
* analytics.trackEvent("user_login", { userId: 123 });
|
|
7
|
+
* analytics.trackPageView("/home");
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { dlog } from "./dlog";
|
|
11
|
+
import { dnetwork } from "./dnetwork";
|
|
12
|
+
|
|
13
|
+
export interface TConfig {
|
|
14
|
+
server: string;
|
|
15
|
+
app_id: string;
|
|
16
|
+
app_version?: string;
|
|
17
|
+
device_os?: string;
|
|
18
|
+
device_id?: string;
|
|
19
|
+
device_api?: string;
|
|
20
|
+
debug?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class DAnalytics {
|
|
24
|
+
private config: TConfig;
|
|
25
|
+
private sessionKey: string | null = null;
|
|
26
|
+
private eventQueue: Array<{ type: string; data: any; ts: number }> = [];
|
|
27
|
+
|
|
28
|
+
constructor(config: TConfig) {
|
|
29
|
+
this.config = config;
|
|
30
|
+
this.initSession();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Initialize analytics session by calling the launch API.
|
|
35
|
+
*/
|
|
36
|
+
private async initSession() {
|
|
37
|
+
const url = `http://${this.config.server}/api/analytics/launch`;
|
|
38
|
+
const payload = {
|
|
39
|
+
app_id: this.config.app_id,
|
|
40
|
+
app_version: this.config.app_version || "0.0",
|
|
41
|
+
device_os: this.config.device_os || "unknown",
|
|
42
|
+
device_id: this.config.device_id || "unknown",
|
|
43
|
+
device_api: this.config.device_api || "unknown",
|
|
44
|
+
};
|
|
45
|
+
try {
|
|
46
|
+
const resp = await dnetwork.postSimpleStore(url, payload);
|
|
47
|
+
this.sessionKey = resp.out.session;
|
|
48
|
+
if (this.config.debug) {
|
|
49
|
+
dlog.d("[DAnalytics] Session initialized:", this.sessionKey);
|
|
50
|
+
}
|
|
51
|
+
} catch (err) {
|
|
52
|
+
if (this.config.debug) {
|
|
53
|
+
// eslint-disable-next-line no-console
|
|
54
|
+
dlog.d("[DAnalytics] Failed to initialize session", err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Track a specific user action by calling the /api/analytics/action endpoint.
|
|
60
|
+
* @param type The type of action (e.g., "click")
|
|
61
|
+
* @param target_id The target element's ID (e.g., "btn1")
|
|
62
|
+
* @param tag A tag describing the action (e.g., "btn_click")
|
|
63
|
+
* @returns The response from the server
|
|
64
|
+
*/
|
|
65
|
+
async trackAction(
|
|
66
|
+
type: string,
|
|
67
|
+
target_id: string,
|
|
68
|
+
tag: string
|
|
69
|
+
): Promise<any> {
|
|
70
|
+
if (!this.sessionKey) {
|
|
71
|
+
if (this.config.debug) {
|
|
72
|
+
// eslint-disable-next-line no-console
|
|
73
|
+
console.warn(
|
|
74
|
+
"[DAnalytics] Session not initialized, cannot track action."
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const url = `http://${this.config.server}/api/analytics/action`;
|
|
80
|
+
const payload = {
|
|
81
|
+
app_id: this.config.app_id,
|
|
82
|
+
session: this.sessionKey,
|
|
83
|
+
type,
|
|
84
|
+
action_id: target_id,
|
|
85
|
+
action_tag: tag,
|
|
86
|
+
};
|
|
87
|
+
try {
|
|
88
|
+
const data = await dnetwork.postSimpleStore(url, payload);
|
|
89
|
+
if (this.config.debug) {
|
|
90
|
+
// eslint-disable-next-line no-console
|
|
91
|
+
console.log("[DAnalytics] Tracked action:", data);
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (this.config.debug) {
|
|
96
|
+
// eslint-disable-next-line no-console
|
|
97
|
+
console.error("[DAnalytics] Failed to track action", err);
|
|
98
|
+
}
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// This will log anything in the console.
|
|
103
|
+
// This will flash all log history
|
|
104
|
+
async trackConsoleLog() {
|
|
105
|
+
const url = `http://${this.config.server}/api/analytics/adhoc`;
|
|
106
|
+
return await dnetwork.postSimpleStore(url, {
|
|
107
|
+
app_id: this.config.app_id,
|
|
108
|
+
session: this.sessionKey,
|
|
109
|
+
console_log: dlog.getLogHistoryAndFlash(),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/dlog.ts
CHANGED
|
@@ -24,6 +24,9 @@ export namespace dlog {
|
|
|
24
24
|
enablePII?: boolean;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
// This will store any data other than debug
|
|
28
|
+
const logs_history: string[] = [];
|
|
29
|
+
|
|
27
30
|
let config: DlogConfig = { isDev: true, enablePII: false };
|
|
28
31
|
|
|
29
32
|
// Unicode icons for log levels
|
|
@@ -127,6 +130,9 @@ export namespace dlog {
|
|
|
127
130
|
const message = `[${icon}${level}] [${now}][${caller}] ${convertMsg(
|
|
128
131
|
msg
|
|
129
132
|
)}${msg2Str}`;
|
|
133
|
+
if (level != "DEBUG") {
|
|
134
|
+
logs_history.push(message);
|
|
135
|
+
}
|
|
130
136
|
console.log(message);
|
|
131
137
|
}
|
|
132
138
|
|
|
@@ -168,54 +174,19 @@ export namespace dlog {
|
|
|
168
174
|
const caller = getCallerFunctionName();
|
|
169
175
|
const icon = ICONS.EXCEPTION;
|
|
170
176
|
const stack = !ignore && error.stack ? `\n${error.stack}` : "";
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Logs an object with a custom message.
|
|
180
|
-
* Redacts sensitive keys.
|
|
181
|
-
*/
|
|
182
|
-
export function obj(obj?: object, msg = "DEBUG OBJECT") {
|
|
183
|
-
if (!config.isDev) return;
|
|
184
|
-
const now = new Date().toLocaleString();
|
|
185
|
-
const caller = getCallerFunctionName();
|
|
186
|
-
console.log(
|
|
187
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} START =====`
|
|
188
|
-
);
|
|
189
|
-
console.log(convertMsg(obj));
|
|
190
|
-
console.log(
|
|
191
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== ${msg} END =====`
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Logs all entries of a Map, redacting sensitive keys.
|
|
197
|
-
*/
|
|
198
|
-
export function map(map: Map<string, unknown>) {
|
|
199
|
-
if (!config.isDev) return;
|
|
200
|
-
const now = new Date().toLocaleString();
|
|
201
|
-
const caller = getCallerFunctionName();
|
|
202
|
-
console.log(
|
|
203
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP START =====`
|
|
204
|
-
);
|
|
205
|
-
for (const [key, value] of map.entries()) {
|
|
206
|
-
const displayValue = convertMsg({ [key]: value });
|
|
207
|
-
console.log(`${JSON.stringify(key)} ==> ${displayValue}`);
|
|
208
|
-
}
|
|
209
|
-
console.log(
|
|
210
|
-
`[${now}][${ICONS.DEBUG} DEBUG][${caller}] ===== MAP END =====`
|
|
211
|
-
);
|
|
177
|
+
let message = `[${now}][${icon} EXCEPTION][${caller}] ${msg ?? ""} ${
|
|
178
|
+
error.message
|
|
179
|
+
}${stack}`;
|
|
180
|
+
console.log(message);
|
|
181
|
+
logs_history.push(message);
|
|
212
182
|
}
|
|
213
183
|
|
|
214
184
|
/**
|
|
215
|
-
*
|
|
216
|
-
* To be implemented if needed.
|
|
185
|
+
* Returns the log history and clears it.
|
|
217
186
|
*/
|
|
218
|
-
export function
|
|
219
|
-
|
|
187
|
+
export function getLogHistoryAndFlash(): string[] {
|
|
188
|
+
const history = [...logs_history];
|
|
189
|
+
logs_history.length = 0;
|
|
190
|
+
return history;
|
|
220
191
|
}
|
|
221
192
|
}
|
package/src/dnetwork.ts
CHANGED
|
@@ -1,81 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dnetwork.ts - Network Utility Functions for HTTP Requests
|
|
3
|
+
*
|
|
4
|
+
* Provides a set of async functions for GET/POST requests using axios,
|
|
5
|
+
* with integrated logging, assertion, and error handling.
|
|
6
|
+
* - All requests log activity via dlog
|
|
7
|
+
* - All responses are checked for HTTP success
|
|
8
|
+
* - JSON responses are parsed and returned as objects
|
|
9
|
+
* - SimpleStore helpers for common API patterns
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
import axios, { AxiosRequestConfig } from "axios";
|
|
2
|
-
import _ from "underscore";
|
|
3
13
|
import { dassert } from "./dassert";
|
|
4
14
|
import { dlog } from "./dlog";
|
|
5
15
|
import { TObject } from "./dtypes";
|
|
6
16
|
|
|
7
17
|
export namespace dnetwork {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Perform a GET request and return the response data.
|
|
20
|
+
*/
|
|
21
|
+
export async function get(
|
|
22
|
+
url: string,
|
|
23
|
+
config?: AxiosRequestConfig
|
|
24
|
+
): Promise<any> {
|
|
25
|
+
dlog.d(`GET ${url}`);
|
|
26
|
+
const result = await axios.get(url, config);
|
|
27
|
+
dassert.verifyOrThrow(
|
|
28
|
+
result.status >= 200 && result.status < 300,
|
|
29
|
+
`GET request to URL ${url} failed with status ${result.status}`
|
|
30
|
+
);
|
|
13
31
|
return result.data;
|
|
14
32
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Perform a GET request and return the response as a JSON object.
|
|
36
|
+
*/
|
|
37
|
+
export async function getJson(
|
|
38
|
+
url: string,
|
|
39
|
+
config?: AxiosRequestConfig
|
|
40
|
+
): Promise<object> {
|
|
41
|
+
const res = await get(url, config);
|
|
42
|
+
if (typeof res === "object" && res !== null) {
|
|
19
43
|
return res;
|
|
20
44
|
}
|
|
21
|
-
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(res);
|
|
47
|
+
} catch {
|
|
48
|
+
throw new Error("Response is not valid JSON");
|
|
49
|
+
}
|
|
22
50
|
}
|
|
23
51
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Perform a POST request and return the response data.
|
|
54
|
+
*/
|
|
55
|
+
export async function post(
|
|
56
|
+
url: string,
|
|
57
|
+
data: any,
|
|
58
|
+
config?: AxiosRequestConfig
|
|
59
|
+
): Promise<any> {
|
|
60
|
+
dlog.d(`POST ${url}`, data);
|
|
61
|
+
const result = await axios.post(url, data, config);
|
|
62
|
+
dassert.verifyOrThrow(
|
|
63
|
+
result.status >= 200 && result.status < 300,
|
|
64
|
+
`POST request to URL ${url} failed with status ${result.status}`
|
|
65
|
+
);
|
|
29
66
|
return result.data;
|
|
30
67
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Perform a POST request and return the response as a JSON object.
|
|
71
|
+
*/
|
|
72
|
+
export async function postJson(
|
|
73
|
+
url: string,
|
|
74
|
+
data: TObject,
|
|
75
|
+
config?: AxiosRequestConfig
|
|
76
|
+
): Promise<object> {
|
|
77
|
+
const res = await post(url, data, config);
|
|
78
|
+
if (typeof res === "object" && res !== null) {
|
|
35
79
|
return res;
|
|
36
80
|
}
|
|
37
|
-
|
|
81
|
+
try {
|
|
82
|
+
return JSON.parse(res);
|
|
83
|
+
} catch {
|
|
84
|
+
throw new Error("Response is not valid JSON");
|
|
85
|
+
}
|
|
38
86
|
}
|
|
39
87
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
88
|
+
/**
|
|
89
|
+
* GET request to a SimpleStore endpoint, expects {status: "success", ...}
|
|
90
|
+
*/
|
|
91
|
+
export async function getSimpleStore(
|
|
92
|
+
url: string,
|
|
93
|
+
config?: AxiosRequestConfig
|
|
94
|
+
): Promise<any> {
|
|
95
|
+
dlog.d(`GET ${url}`);
|
|
96
|
+
const response = await axios.get(url, config);
|
|
97
|
+
const jsondata = response.data;
|
|
45
98
|
if (jsondata.status === "success") {
|
|
46
|
-
dlog.d("fetch success
|
|
99
|
+
dlog.d("fetch success");
|
|
47
100
|
return jsondata;
|
|
48
101
|
} else {
|
|
49
|
-
dlog.
|
|
102
|
+
dlog.e(`GET failed: ${url}`, jsondata);
|
|
50
103
|
throw new Error(jsondata.msg);
|
|
51
104
|
}
|
|
52
105
|
}
|
|
53
106
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
107
|
+
/**
|
|
108
|
+
* POST request to a SimpleStore endpoint, expects {status: "success", ...}
|
|
109
|
+
*/
|
|
110
|
+
export async function postSimpleStore(
|
|
111
|
+
url: string,
|
|
112
|
+
data: TObject,
|
|
113
|
+
config?: AxiosRequestConfig
|
|
114
|
+
): Promise<any> {
|
|
115
|
+
dlog.d(`POST ${url}`, data);
|
|
116
|
+
const response = await axios.post(url, data, config);
|
|
117
|
+
const jsondata = response.data;
|
|
59
118
|
if (jsondata.status === "success") {
|
|
60
|
-
dlog.d("
|
|
119
|
+
dlog.d("POST success");
|
|
61
120
|
return jsondata;
|
|
62
121
|
} else {
|
|
63
|
-
dlog.
|
|
122
|
+
dlog.e(`POST failed: ${url}`, jsondata);
|
|
64
123
|
throw new Error(jsondata.msg);
|
|
65
124
|
}
|
|
66
125
|
}
|
|
67
126
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Perform a GET request with a browser-like user-agent header.
|
|
129
|
+
*/
|
|
130
|
+
export async function getAsFakeBrowser(
|
|
131
|
+
url: string,
|
|
132
|
+
config?: AxiosRequestConfig
|
|
133
|
+
): Promise<any> {
|
|
134
|
+
const headers = {
|
|
135
|
+
"user-agent":
|
|
136
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36",
|
|
137
|
+
...(config?.headers || {}),
|
|
138
|
+
};
|
|
139
|
+
const resp = await axios.get(url, { ...config, headers });
|
|
140
|
+
return resp.data;
|
|
75
141
|
}
|
|
76
142
|
}
|
|
77
|
-
|
|
78
|
-
// test
|
|
79
|
-
(async function test() {
|
|
80
|
-
console.log(await dnetwork.postJson("https://simplestore.dipankar.co.in/api/test/insert", { name: 1 }));
|
|
81
|
-
}); //();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DAnalytics, TConfig } from "../src/danalytics";
|
|
2
|
+
import { dlog } from "../src/dlog";
|
|
3
|
+
|
|
4
|
+
describe("DAnalytics initSession", () => {
|
|
5
|
+
beforeEach(() => {});
|
|
6
|
+
|
|
7
|
+
it("should call the launch API and store the session key", async () => {
|
|
8
|
+
const config: TConfig = {
|
|
9
|
+
server: "simplestore.dipankar.co.in",
|
|
10
|
+
app_id: "corexxx",
|
|
11
|
+
debug: false,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Create instance and wait for session to initialize
|
|
15
|
+
const analytics = new DAnalytics(config);
|
|
16
|
+
// Wait for the async initSession to complete
|
|
17
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
18
|
+
expect(analytics["sessionKey"]).toBeDefined();
|
|
19
|
+
|
|
20
|
+
// test action:
|
|
21
|
+
const result = await analytics.trackAction("click", "btn1", "btn_click");
|
|
22
|
+
expect(result).toMatchObject({ status: "success" });
|
|
23
|
+
|
|
24
|
+
// adhoc action
|
|
25
|
+
dlog.i("This is a sample test console log", { Hello: 100 });
|
|
26
|
+
const result1 = await analytics.trackConsoleLog();
|
|
27
|
+
expect(result1).toMatchObject({ status: "success" });
|
|
28
|
+
});
|
|
29
|
+
});
|
package/test/dlog.test.ts
CHANGED
|
@@ -25,12 +25,17 @@ describe("dlog utility", () => {
|
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
it("should
|
|
29
|
-
dlog.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
it("should return and clear the log history", () => {
|
|
29
|
+
dlog.d("Test debug log");
|
|
30
|
+
dlog.i("Test info log");
|
|
31
|
+
const history = dlog.getLogHistoryAndFlash();
|
|
32
|
+
expect(Array.isArray(history)).toBe(true);
|
|
33
|
+
expect(history.length).toBeGreaterThanOrEqual(2);
|
|
34
|
+
expect(history.some((line) => line.includes("Test debug log"))).toBe(true);
|
|
35
|
+
expect(history.some((line) => line.includes("Test info log"))).toBe(true);
|
|
36
|
+
|
|
37
|
+
// After flash, history should be empty
|
|
38
|
+
const afterFlash = dlog.getLogHistoryAndFlash();
|
|
39
|
+
expect(afterFlash.length).toBe(0);
|
|
35
40
|
});
|
|
36
41
|
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { dnetwork } from "../src/dnetwork";
|
|
3
|
+
|
|
4
|
+
// Mock axios
|
|
5
|
+
jest.mock("axios");
|
|
6
|
+
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
|
7
|
+
|
|
8
|
+
describe("dnetwork", () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
jest.clearAllMocks();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should perform a GET request and return data", async () => {
|
|
14
|
+
mockedAxios.get.mockResolvedValueOnce({
|
|
15
|
+
status: 200,
|
|
16
|
+
data: { foo: "bar" },
|
|
17
|
+
});
|
|
18
|
+
const data = await dnetwork.get("http://test.com");
|
|
19
|
+
expect(data).toEqual({ foo: "bar" });
|
|
20
|
+
expect(mockedAxios.get).toHaveBeenCalledWith("http://test.com", undefined);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should throw if GET request fails", async () => {
|
|
24
|
+
mockedAxios.get.mockResolvedValueOnce({ status: 404, data: {} });
|
|
25
|
+
await expect(dnetwork.get("http://fail.com")).rejects.toThrow(
|
|
26
|
+
"GET request to URL http://fail.com failed with status 404"
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should perform a GET request and parse JSON", async () => {
|
|
31
|
+
mockedAxios.get.mockResolvedValueOnce({
|
|
32
|
+
status: 200,
|
|
33
|
+
data: { hello: "world" },
|
|
34
|
+
});
|
|
35
|
+
const data = await dnetwork.getJson("http://json.com");
|
|
36
|
+
expect(data).toEqual({ hello: "world" });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should parse string JSON response in getJson", async () => {
|
|
40
|
+
mockedAxios.get.mockResolvedValueOnce({ status: 200, data: '{"a":1}' });
|
|
41
|
+
const data = await dnetwork.getJson("http://jsonstring.com");
|
|
42
|
+
expect(data).toEqual({ a: 1 });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should throw if getJson response is not valid JSON", async () => {
|
|
46
|
+
mockedAxios.get.mockResolvedValueOnce({ status: 200, data: "notjson" });
|
|
47
|
+
await expect(dnetwork.getJson("http://badjson.com")).rejects.toThrow(
|
|
48
|
+
"Response is not valid JSON"
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should perform a POST request and return data", async () => {
|
|
53
|
+
mockedAxios.post.mockResolvedValueOnce({ status: 201, data: { ok: true } });
|
|
54
|
+
const data = await dnetwork.post("http://post.com", { x: 1 });
|
|
55
|
+
expect(data).toEqual({ ok: true });
|
|
56
|
+
expect(mockedAxios.post).toHaveBeenCalledWith(
|
|
57
|
+
"http://post.com",
|
|
58
|
+
{ x: 1 },
|
|
59
|
+
undefined
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should throw if POST request fails", async () => {
|
|
64
|
+
mockedAxios.post.mockResolvedValueOnce({ status: 500, data: {} });
|
|
65
|
+
await expect(dnetwork.post("http://failpost.com", {})).rejects.toThrow(
|
|
66
|
+
"POST request to URL http://failpost.com failed with status 500"
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should perform a POST request and parse JSON", async () => {
|
|
71
|
+
mockedAxios.post.mockResolvedValueOnce({ status: 200, data: { ok: 1 } });
|
|
72
|
+
const data = await dnetwork.postJson("http://postjson.com", { y: 2 });
|
|
73
|
+
expect(data).toEqual({ ok: 1 });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should parse string JSON response in postJson", async () => {
|
|
77
|
+
mockedAxios.post.mockResolvedValueOnce({ status: 200, data: '{"b":2}' });
|
|
78
|
+
const data = await dnetwork.postJson("http://postjsonstring.com", { y: 2 });
|
|
79
|
+
expect(data).toEqual({ b: 2 });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should throw if postJson response is not valid JSON", async () => {
|
|
83
|
+
mockedAxios.post.mockResolvedValueOnce({ status: 200, data: "notjson" });
|
|
84
|
+
await expect(
|
|
85
|
+
dnetwork.postJson("http://badpostjson.com", {})
|
|
86
|
+
).rejects.toThrow("Response is not valid JSON");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should get SimpleStore success", async () => {
|
|
90
|
+
mockedAxios.get.mockResolvedValueOnce({
|
|
91
|
+
status: 200,
|
|
92
|
+
data: { status: "success", value: 42 },
|
|
93
|
+
});
|
|
94
|
+
const data = await dnetwork.getSimpleStore("http://store.com");
|
|
95
|
+
expect(data).toEqual({ status: "success", value: 42 });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("should throw on SimpleStore GET failure", async () => {
|
|
99
|
+
mockedAxios.get.mockResolvedValueOnce({
|
|
100
|
+
status: 200,
|
|
101
|
+
data: { status: "fail", msg: "bad" },
|
|
102
|
+
});
|
|
103
|
+
await expect(
|
|
104
|
+
dnetwork.getSimpleStore("http://failstore.com")
|
|
105
|
+
).rejects.toThrow("bad");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should post SimpleStore success", async () => {
|
|
109
|
+
mockedAxios.post.mockResolvedValueOnce({
|
|
110
|
+
status: 200,
|
|
111
|
+
data: { status: "success", value: 99 },
|
|
112
|
+
});
|
|
113
|
+
const data = await dnetwork.postSimpleStore("http://store.com", { z: 3 });
|
|
114
|
+
expect(data).toEqual({ status: "success", value: 99 });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("should throw on SimpleStore POST failure", async () => {
|
|
118
|
+
mockedAxios.post.mockResolvedValueOnce({
|
|
119
|
+
status: 200,
|
|
120
|
+
data: { status: "error", msg: "failmsg" },
|
|
121
|
+
});
|
|
122
|
+
await expect(
|
|
123
|
+
dnetwork.postSimpleStore("http://failstore.com", {})
|
|
124
|
+
).rejects.toThrow("failmsg");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("should perform a GET as fake browser", async () => {
|
|
128
|
+
mockedAxios.get.mockResolvedValueOnce({ status: 200, data: "browser" });
|
|
129
|
+
const data = await dnetwork.getAsFakeBrowser("http://browser.com");
|
|
130
|
+
expect(data).toBe("browser");
|
|
131
|
+
expect(mockedAxios.get).toHaveBeenCalledWith(
|
|
132
|
+
"http://browser.com",
|
|
133
|
+
expect.objectContaining({
|
|
134
|
+
headers: expect.objectContaining({
|
|
135
|
+
"user-agent": expect.any(String),
|
|
136
|
+
}),
|
|
137
|
+
})
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
});
|