corexxx 1.0.144 → 1.0.151
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/dist/dnetwork.js +64 -15
- package/dist/dnetwork.js.map +1 -1
- package/dist/test.d.ts +0 -0
- package/dist/test.js.map +1 -0
- package/package.json +1 -1
- package/publish.sh +1 -1
- package/src/dnetwork.ts +85 -30
package/dist/dnetwork.js
CHANGED
|
@@ -27,6 +27,7 @@ exports.dnetwork = exports.NetworkError = void 0;
|
|
|
27
27
|
const axios_1 = __importDefault(require("axios"));
|
|
28
28
|
const dassert_1 = require("./dassert");
|
|
29
29
|
const dlog_1 = require("./dlog");
|
|
30
|
+
let count = 0;
|
|
30
31
|
// Custom error class for network-related errors
|
|
31
32
|
class NetworkError extends dassert_1.DError {
|
|
32
33
|
constructor(message, code = "NETWORK_ERROR", data, statusCode) {
|
|
@@ -49,11 +50,11 @@ var dnetwork;
|
|
|
49
50
|
let message = "Unknown network error";
|
|
50
51
|
if (error.code === "ECONNABORTED") {
|
|
51
52
|
code = "TIMEOUT_ERROR";
|
|
52
|
-
message = `Request timeout
|
|
53
|
+
message = `Request timeout. The server took too long to respond.`;
|
|
53
54
|
}
|
|
54
55
|
else if (error.code === "ENOTFOUND" || error.code === "ECONNREFUSED") {
|
|
55
56
|
code = "SERVER_UNREACHABLE";
|
|
56
|
-
message = `Server unreachable
|
|
57
|
+
message = `Server unreachable. Check your internet connection or DNS settings.`;
|
|
57
58
|
}
|
|
58
59
|
else if (error.code === "ENETUNREACH") {
|
|
59
60
|
code = "NO_INTERNET";
|
|
@@ -61,19 +62,19 @@ var dnetwork;
|
|
|
61
62
|
}
|
|
62
63
|
else if (error.response && error.response.status >= 500) {
|
|
63
64
|
code = "SERVER_ERROR_5XX";
|
|
64
|
-
message = `Server error (${error.response.status})
|
|
65
|
+
message = `Server error (${error.response.status}) : ${error.response.statusText}`;
|
|
65
66
|
}
|
|
66
67
|
else if (error.response && error.response.status === 404) {
|
|
67
68
|
code = "NOT_FOUND";
|
|
68
|
-
message = `Resource not found (404)
|
|
69
|
+
message = `Resource not found (404) `;
|
|
69
70
|
}
|
|
70
71
|
else if (error.response && error.response.status === 403) {
|
|
71
72
|
code = "FORBIDDEN";
|
|
72
|
-
message = `Access forbidden (403)
|
|
73
|
+
message = `Access forbidden (403) `;
|
|
73
74
|
}
|
|
74
75
|
else if (error.message === "Network Error") {
|
|
75
76
|
code = "NETWORK_UNREACHABLE";
|
|
76
|
-
message = `Network is unreachable
|
|
77
|
+
message = `Network is unreachable `;
|
|
77
78
|
}
|
|
78
79
|
else {
|
|
79
80
|
message = error.message || "Unknown error occurred";
|
|
@@ -91,12 +92,13 @@ var dnetwork;
|
|
|
91
92
|
function get(url, config) {
|
|
92
93
|
return __awaiter(this, void 0, void 0, function* () {
|
|
93
94
|
try {
|
|
94
|
-
dlog_1.dlog.d(`GET ${url}`);
|
|
95
95
|
const result = yield axios_1.default.get(url, config);
|
|
96
|
+
LogNetwork(url, null, result, null);
|
|
96
97
|
dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `GET request to URL ${url} failed with status ${result.status}`, "HTTP_STATUS_ERROR");
|
|
97
98
|
return result.data;
|
|
98
99
|
}
|
|
99
100
|
catch (error) {
|
|
101
|
+
LogNetwork(url, null, null, error);
|
|
100
102
|
if (axios_1.default.isAxiosError(error)) {
|
|
101
103
|
handleAxiosError(error, url);
|
|
102
104
|
}
|
|
@@ -112,6 +114,7 @@ var dnetwork;
|
|
|
112
114
|
return __awaiter(this, void 0, void 0, function* () {
|
|
113
115
|
try {
|
|
114
116
|
const res = yield get(url, config);
|
|
117
|
+
LogNetwork(url, null, res, null);
|
|
115
118
|
if (typeof res === "object" && res !== null) {
|
|
116
119
|
return res;
|
|
117
120
|
}
|
|
@@ -123,6 +126,7 @@ var dnetwork;
|
|
|
123
126
|
}
|
|
124
127
|
}
|
|
125
128
|
catch (error) {
|
|
129
|
+
LogNetwork(url, null, null, error);
|
|
126
130
|
if (axios_1.default.isAxiosError(error)) {
|
|
127
131
|
handleAxiosError(error, url);
|
|
128
132
|
}
|
|
@@ -137,12 +141,13 @@ var dnetwork;
|
|
|
137
141
|
function post(url, data, config) {
|
|
138
142
|
return __awaiter(this, void 0, void 0, function* () {
|
|
139
143
|
try {
|
|
140
|
-
dlog_1.dlog.d(`POST ${url}`, data);
|
|
141
144
|
const result = yield axios_1.default.post(url, data, config);
|
|
145
|
+
LogNetwork(url, data, result, null);
|
|
142
146
|
dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `POST request to URL ${url} failed with status ${result.status}`, "HTTP_STATUS_ERROR");
|
|
143
147
|
return result.data;
|
|
144
148
|
}
|
|
145
149
|
catch (error) {
|
|
150
|
+
LogNetwork(url, data, null, error);
|
|
146
151
|
if (axios_1.default.isAxiosError(error)) {
|
|
147
152
|
handleAxiosError(error, url);
|
|
148
153
|
}
|
|
@@ -159,16 +164,21 @@ var dnetwork;
|
|
|
159
164
|
try {
|
|
160
165
|
const res = yield post(url, data, config);
|
|
161
166
|
if (typeof res === "object" && res !== null) {
|
|
167
|
+
LogNetwork(url, data, res, null);
|
|
162
168
|
return res;
|
|
163
169
|
}
|
|
164
170
|
try {
|
|
165
|
-
|
|
171
|
+
let output = JSON.parse(res);
|
|
172
|
+
LogNetwork(url, data, output, null);
|
|
173
|
+
return output;
|
|
166
174
|
}
|
|
167
175
|
catch (_a) {
|
|
176
|
+
LogNetwork(url, data, res, "Response is not valid JSON");
|
|
168
177
|
throw new NetworkError("Response is not valid JSON", "JSON_PARSE_ERROR", res);
|
|
169
178
|
}
|
|
170
179
|
}
|
|
171
180
|
catch (error) {
|
|
181
|
+
LogNetwork(url, data, null, error);
|
|
172
182
|
if (axios_1.default.isAxiosError(error)) {
|
|
173
183
|
handleAxiosError(error, url);
|
|
174
184
|
}
|
|
@@ -183,19 +193,18 @@ var dnetwork;
|
|
|
183
193
|
function getSimpleStore(url, config) {
|
|
184
194
|
return __awaiter(this, void 0, void 0, function* () {
|
|
185
195
|
try {
|
|
186
|
-
dlog_1.dlog.d(`GET ${url}`);
|
|
187
196
|
const response = yield axios_1.default.get(url, config);
|
|
188
197
|
const jsondata = response.data;
|
|
198
|
+
LogNetwork(url, null, jsondata, null);
|
|
189
199
|
if (jsondata.status === "success") {
|
|
190
|
-
dlog_1.dlog.d("fetch success");
|
|
191
200
|
return jsondata;
|
|
192
201
|
}
|
|
193
202
|
else {
|
|
194
|
-
dlog_1.dlog.e(`GET failed: ${url}`, jsondata);
|
|
195
203
|
throw new NetworkError(jsondata.msg || "SimpleStore request failed", "SIMPLESTORE_ERROR", jsondata);
|
|
196
204
|
}
|
|
197
205
|
}
|
|
198
206
|
catch (error) {
|
|
207
|
+
LogNetwork(url, null, null, error);
|
|
199
208
|
if (axios_1.default.isAxiosError(error)) {
|
|
200
209
|
handleAxiosError(error, url);
|
|
201
210
|
}
|
|
@@ -210,19 +219,18 @@ var dnetwork;
|
|
|
210
219
|
function postSimpleStore(url, data, config) {
|
|
211
220
|
return __awaiter(this, void 0, void 0, function* () {
|
|
212
221
|
try {
|
|
213
|
-
dlog_1.dlog.d(`POST ${url}`, data);
|
|
214
222
|
const response = yield axios_1.default.post(url, data, config);
|
|
215
223
|
const jsondata = response.data;
|
|
224
|
+
LogNetwork(url, data, jsondata, null);
|
|
216
225
|
if (jsondata.status === "success") {
|
|
217
|
-
dlog_1.dlog.d("POST success");
|
|
218
226
|
return jsondata;
|
|
219
227
|
}
|
|
220
228
|
else {
|
|
221
|
-
dlog_1.dlog.e(`POST failed: ${url}`, jsondata);
|
|
222
229
|
throw new NetworkError(jsondata.msg || "SimpleStore request failed", "SIMPLESTORE_ERROR", jsondata);
|
|
223
230
|
}
|
|
224
231
|
}
|
|
225
232
|
catch (error) {
|
|
233
|
+
LogNetwork(url, data, null, error);
|
|
226
234
|
if (axios_1.default.isAxiosError(error)) {
|
|
227
235
|
handleAxiosError(error, url);
|
|
228
236
|
}
|
|
@@ -250,5 +258,46 @@ var dnetwork;
|
|
|
250
258
|
});
|
|
251
259
|
}
|
|
252
260
|
dnetwork.getAsFakeBrowser = getAsFakeBrowser;
|
|
261
|
+
function LogNetwork(url, req, resp, error) {
|
|
262
|
+
count++;
|
|
263
|
+
dlog_1.dlog.d(`=================== NETWORK REQUEST: ${count} ======================
|
|
264
|
+
URL:${url}
|
|
265
|
+
REQUEST: ${convert(req)}
|
|
266
|
+
RESP:${convert(resp)}
|
|
267
|
+
Error:${convert(error)}
|
|
268
|
+
==========================================================
|
|
269
|
+
`);
|
|
270
|
+
}
|
|
253
271
|
})(dnetwork || (exports.dnetwork = dnetwork = {}));
|
|
272
|
+
function convert(a) {
|
|
273
|
+
if (a === null)
|
|
274
|
+
return "null";
|
|
275
|
+
if (a === undefined)
|
|
276
|
+
return "undefined";
|
|
277
|
+
if (typeof a === "string")
|
|
278
|
+
return a;
|
|
279
|
+
if (typeof a === "number" ||
|
|
280
|
+
typeof a === "boolean" ||
|
|
281
|
+
typeof a === "bigint") {
|
|
282
|
+
return a.toString();
|
|
283
|
+
}
|
|
284
|
+
if (typeof a === "function") {
|
|
285
|
+
return a.name ? `[Function: ${a.name}]` : "[Function]";
|
|
286
|
+
}
|
|
287
|
+
if (a instanceof Date) {
|
|
288
|
+
return a.toISOString();
|
|
289
|
+
}
|
|
290
|
+
if (Array.isArray(a)) {
|
|
291
|
+
return `[${a.map(convert).join(", ")}]`;
|
|
292
|
+
}
|
|
293
|
+
if (typeof a === "object") {
|
|
294
|
+
try {
|
|
295
|
+
return JSON.stringify(a);
|
|
296
|
+
}
|
|
297
|
+
catch (_a) {
|
|
298
|
+
return "[Object]";
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return String(a);
|
|
302
|
+
}
|
|
254
303
|
//# sourceMappingURL=dnetwork.js.map
|
package/dist/dnetwork.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dnetwork.js","sourceRoot":"","sources":["../src/dnetwork.ts"],"names":[],"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KAAK,IAAI;QAAE,OAAO,MAAM,CAAC;IAC9B,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,WAAW,CAAC;IAExC,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,CAAC,CAAC;IACpC,IACE,OAAO,CAAC,KAAK,QAAQ;QACrB,OAAO,CAAC,KAAK,SAAS;QACtB,OAAO,CAAC,KAAK,QAAQ,EACrB,CAAC;QACD,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC;IACtB,CAAC;IAED,IAAI,OAAO,CAAC,KAAK,UAAU,EAAE,CAAC;QAC5B,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,YAAY,CAAC;IACzD,CAAC;IAED,IAAI,CAAC,YAAY,IAAI,EAAE,CAAC;QACtB,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC;IACzB,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;IAC1C,CAAC;IAED,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC1B,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QAC3B,CAAC;QAAC,WAAM,CAAC;YACP,OAAO,UAAU,CAAC;QACpB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC;AACnB,CAAC"}
|
package/dist/test.d.ts
ADDED
|
File without changes
|
package/dist/test.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";AAAA,qDAAqD;AACrD,uFAAuF;AACvF,0DAA0D;AAC1D,yEAAyE;AAEzE,0EAA0E;AAC1E,2EAA2E;AAE3E,sDAAsD;AACtD,sDAAsD;AACtD,sDAAsD"}
|
package/package.json
CHANGED
package/publish.sh
CHANGED
package/src/dnetwork.ts
CHANGED
|
@@ -14,6 +14,7 @@ import axios, { AxiosError, AxiosRequestConfig } from "axios";
|
|
|
14
14
|
import { dassert, DError } from "./dassert";
|
|
15
15
|
import { dlog } from "./dlog";
|
|
16
16
|
import { TObject } from "./dtypes";
|
|
17
|
+
let count = 0;
|
|
17
18
|
|
|
18
19
|
// Custom error class for network-related errors
|
|
19
20
|
export class NetworkError<T = any> extends DError {
|
|
@@ -24,7 +25,7 @@ export class NetworkError<T = any> extends DError {
|
|
|
24
25
|
message: string,
|
|
25
26
|
code: string = "NETWORK_ERROR",
|
|
26
27
|
data?: T,
|
|
27
|
-
statusCode?: number
|
|
28
|
+
statusCode?: number,
|
|
28
29
|
) {
|
|
29
30
|
super(message, code, { data, statusCode });
|
|
30
31
|
this.name = "NetworkError";
|
|
@@ -44,25 +45,25 @@ export namespace dnetwork {
|
|
|
44
45
|
|
|
45
46
|
if (error.code === "ECONNABORTED") {
|
|
46
47
|
code = "TIMEOUT_ERROR";
|
|
47
|
-
message = `Request timeout
|
|
48
|
+
message = `Request timeout. The server took too long to respond.`;
|
|
48
49
|
} else if (error.code === "ENOTFOUND" || error.code === "ECONNREFUSED") {
|
|
49
50
|
code = "SERVER_UNREACHABLE";
|
|
50
|
-
message = `Server unreachable
|
|
51
|
+
message = `Server unreachable. Check your internet connection or DNS settings.`;
|
|
51
52
|
} else if (error.code === "ENETUNREACH") {
|
|
52
53
|
code = "NO_INTERNET";
|
|
53
54
|
message = `No internet connection. Unable to reach ${url}.`;
|
|
54
55
|
} else if (error.response && error.response.status >= 500) {
|
|
55
56
|
code = "SERVER_ERROR_5XX";
|
|
56
|
-
message = `Server error (${error.response.status})
|
|
57
|
+
message = `Server error (${error.response.status}) : ${error.response.statusText}`;
|
|
57
58
|
} else if (error.response && error.response.status === 404) {
|
|
58
59
|
code = "NOT_FOUND";
|
|
59
|
-
message = `Resource not found (404)
|
|
60
|
+
message = `Resource not found (404) `;
|
|
60
61
|
} else if (error.response && error.response.status === 403) {
|
|
61
62
|
code = "FORBIDDEN";
|
|
62
|
-
message = `Access forbidden (403)
|
|
63
|
+
message = `Access forbidden (403) `;
|
|
63
64
|
} else if (error.message === "Network Error") {
|
|
64
65
|
code = "NETWORK_UNREACHABLE";
|
|
65
|
-
message = `Network is unreachable
|
|
66
|
+
message = `Network is unreachable `;
|
|
66
67
|
} else {
|
|
67
68
|
message = error.message || "Unknown error occurred";
|
|
68
69
|
}
|
|
@@ -76,7 +77,7 @@ export namespace dnetwork {
|
|
|
76
77
|
message,
|
|
77
78
|
code,
|
|
78
79
|
error.response?.data,
|
|
79
|
-
error.response?.status
|
|
80
|
+
error.response?.status,
|
|
80
81
|
);
|
|
81
82
|
}
|
|
82
83
|
|
|
@@ -85,18 +86,19 @@ export namespace dnetwork {
|
|
|
85
86
|
*/
|
|
86
87
|
export async function get(
|
|
87
88
|
url: string,
|
|
88
|
-
config?: AxiosRequestConfig
|
|
89
|
+
config?: AxiosRequestConfig,
|
|
89
90
|
): Promise<any> {
|
|
90
91
|
try {
|
|
91
|
-
dlog.d(`GET ${url}`);
|
|
92
92
|
const result = await axios.get(url, config);
|
|
93
|
+
LogNetwork(url, null, result, null);
|
|
93
94
|
dassert.verifyOrThrow(
|
|
94
95
|
result.status >= 200 && result.status < 300,
|
|
95
96
|
`GET request to URL ${url} failed with status ${result.status}`,
|
|
96
|
-
"HTTP_STATUS_ERROR"
|
|
97
|
+
"HTTP_STATUS_ERROR",
|
|
97
98
|
);
|
|
98
99
|
return result.data;
|
|
99
100
|
} catch (error) {
|
|
101
|
+
LogNetwork(url, null, null, error);
|
|
100
102
|
if (axios.isAxiosError(error)) {
|
|
101
103
|
handleAxiosError(error, url);
|
|
102
104
|
}
|
|
@@ -109,10 +111,11 @@ export namespace dnetwork {
|
|
|
109
111
|
*/
|
|
110
112
|
export async function getJson(
|
|
111
113
|
url: string,
|
|
112
|
-
config?: AxiosRequestConfig
|
|
114
|
+
config?: AxiosRequestConfig,
|
|
113
115
|
): Promise<object> {
|
|
114
116
|
try {
|
|
115
117
|
const res = await get(url, config);
|
|
118
|
+
LogNetwork(url, null, res, null);
|
|
116
119
|
if (typeof res === "object" && res !== null) {
|
|
117
120
|
return res;
|
|
118
121
|
}
|
|
@@ -122,10 +125,11 @@ export namespace dnetwork {
|
|
|
122
125
|
throw new NetworkError(
|
|
123
126
|
"Response is not valid JSON",
|
|
124
127
|
"JSON_PARSE_ERROR",
|
|
125
|
-
res
|
|
128
|
+
res,
|
|
126
129
|
);
|
|
127
130
|
}
|
|
128
131
|
} catch (error) {
|
|
132
|
+
LogNetwork(url, null, null, error);
|
|
129
133
|
if (axios.isAxiosError(error)) {
|
|
130
134
|
handleAxiosError(error, url);
|
|
131
135
|
}
|
|
@@ -139,18 +143,19 @@ export namespace dnetwork {
|
|
|
139
143
|
export async function post(
|
|
140
144
|
url: string,
|
|
141
145
|
data: any,
|
|
142
|
-
config?: AxiosRequestConfig
|
|
146
|
+
config?: AxiosRequestConfig,
|
|
143
147
|
): Promise<any> {
|
|
144
148
|
try {
|
|
145
|
-
dlog.d(`POST ${url}`, data);
|
|
146
149
|
const result = await axios.post(url, data, config);
|
|
150
|
+
LogNetwork(url, data, result, null);
|
|
147
151
|
dassert.verifyOrThrow(
|
|
148
152
|
result.status >= 200 && result.status < 300,
|
|
149
153
|
`POST request to URL ${url} failed with status ${result.status}`,
|
|
150
|
-
"HTTP_STATUS_ERROR"
|
|
154
|
+
"HTTP_STATUS_ERROR",
|
|
151
155
|
);
|
|
152
156
|
return result.data;
|
|
153
157
|
} catch (error) {
|
|
158
|
+
LogNetwork(url, data, null, error);
|
|
154
159
|
if (axios.isAxiosError(error)) {
|
|
155
160
|
handleAxiosError(error, url);
|
|
156
161
|
}
|
|
@@ -164,23 +169,28 @@ export namespace dnetwork {
|
|
|
164
169
|
export async function postJson(
|
|
165
170
|
url: string,
|
|
166
171
|
data: TObject,
|
|
167
|
-
config?: AxiosRequestConfig
|
|
172
|
+
config?: AxiosRequestConfig,
|
|
168
173
|
): Promise<object> {
|
|
169
174
|
try {
|
|
170
175
|
const res = await post(url, data, config);
|
|
171
176
|
if (typeof res === "object" && res !== null) {
|
|
177
|
+
LogNetwork(url, data, res, null);
|
|
172
178
|
return res;
|
|
173
179
|
}
|
|
174
180
|
try {
|
|
175
|
-
|
|
181
|
+
let output = JSON.parse(res);
|
|
182
|
+
LogNetwork(url, data, output, null);
|
|
183
|
+
return output;
|
|
176
184
|
} catch {
|
|
185
|
+
LogNetwork(url, data, res, "Response is not valid JSON");
|
|
177
186
|
throw new NetworkError(
|
|
178
187
|
"Response is not valid JSON",
|
|
179
188
|
"JSON_PARSE_ERROR",
|
|
180
|
-
res
|
|
189
|
+
res,
|
|
181
190
|
);
|
|
182
191
|
}
|
|
183
192
|
} catch (error) {
|
|
193
|
+
LogNetwork(url, data, null, error);
|
|
184
194
|
if (axios.isAxiosError(error)) {
|
|
185
195
|
handleAxiosError(error, url);
|
|
186
196
|
}
|
|
@@ -193,24 +203,23 @@ export namespace dnetwork {
|
|
|
193
203
|
*/
|
|
194
204
|
export async function getSimpleStore(
|
|
195
205
|
url: string,
|
|
196
|
-
config?: AxiosRequestConfig
|
|
206
|
+
config?: AxiosRequestConfig,
|
|
197
207
|
): Promise<any> {
|
|
198
208
|
try {
|
|
199
|
-
dlog.d(`GET ${url}`);
|
|
200
209
|
const response = await axios.get(url, config);
|
|
201
210
|
const jsondata = response.data;
|
|
211
|
+
LogNetwork(url, null, jsondata, null);
|
|
202
212
|
if (jsondata.status === "success") {
|
|
203
|
-
dlog.d("fetch success");
|
|
204
213
|
return jsondata;
|
|
205
214
|
} else {
|
|
206
|
-
dlog.e(`GET failed: ${url}`, jsondata);
|
|
207
215
|
throw new NetworkError(
|
|
208
216
|
jsondata.msg || "SimpleStore request failed",
|
|
209
217
|
"SIMPLESTORE_ERROR",
|
|
210
|
-
jsondata
|
|
218
|
+
jsondata,
|
|
211
219
|
);
|
|
212
220
|
}
|
|
213
221
|
} catch (error) {
|
|
222
|
+
LogNetwork(url, null, null, error);
|
|
214
223
|
if (axios.isAxiosError(error)) {
|
|
215
224
|
handleAxiosError(error, url);
|
|
216
225
|
}
|
|
@@ -224,24 +233,23 @@ export namespace dnetwork {
|
|
|
224
233
|
export async function postSimpleStore(
|
|
225
234
|
url: string,
|
|
226
235
|
data: TObject,
|
|
227
|
-
config?: AxiosRequestConfig
|
|
236
|
+
config?: AxiosRequestConfig,
|
|
228
237
|
): Promise<any> {
|
|
229
238
|
try {
|
|
230
|
-
dlog.d(`POST ${url}`, data);
|
|
231
239
|
const response = await axios.post(url, data, config);
|
|
232
240
|
const jsondata = response.data;
|
|
241
|
+
LogNetwork(url, data, jsondata, null);
|
|
233
242
|
if (jsondata.status === "success") {
|
|
234
|
-
dlog.d("POST success");
|
|
235
243
|
return jsondata;
|
|
236
244
|
} else {
|
|
237
|
-
dlog.e(`POST failed: ${url}`, jsondata);
|
|
238
245
|
throw new NetworkError(
|
|
239
246
|
jsondata.msg || "SimpleStore request failed",
|
|
240
247
|
"SIMPLESTORE_ERROR",
|
|
241
|
-
jsondata
|
|
248
|
+
jsondata,
|
|
242
249
|
);
|
|
243
250
|
}
|
|
244
251
|
} catch (error) {
|
|
252
|
+
LogNetwork(url, data, null, error);
|
|
245
253
|
if (axios.isAxiosError(error)) {
|
|
246
254
|
handleAxiosError(error, url);
|
|
247
255
|
}
|
|
@@ -254,7 +262,7 @@ export namespace dnetwork {
|
|
|
254
262
|
*/
|
|
255
263
|
export async function getAsFakeBrowser(
|
|
256
264
|
url: string,
|
|
257
|
-
config?: AxiosRequestConfig
|
|
265
|
+
config?: AxiosRequestConfig,
|
|
258
266
|
): Promise<any> {
|
|
259
267
|
try {
|
|
260
268
|
const headers = {
|
|
@@ -271,4 +279,51 @@ export namespace dnetwork {
|
|
|
271
279
|
throw error;
|
|
272
280
|
}
|
|
273
281
|
}
|
|
282
|
+
|
|
283
|
+
function LogNetwork(url: any, req: any, resp: any, error: any) {
|
|
284
|
+
count++;
|
|
285
|
+
dlog.d(`=================== NETWORK REQUEST: ${count} ======================
|
|
286
|
+
URL:${url}
|
|
287
|
+
REQUEST: ${convert(req)}
|
|
288
|
+
RESP:${convert(resp)}
|
|
289
|
+
Error:${convert(error)}
|
|
290
|
+
==========================================================
|
|
291
|
+
`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function convert(a: any): string {
|
|
296
|
+
if (a === null) return "null";
|
|
297
|
+
if (a === undefined) return "undefined";
|
|
298
|
+
|
|
299
|
+
if (typeof a === "string") return a;
|
|
300
|
+
if (
|
|
301
|
+
typeof a === "number" ||
|
|
302
|
+
typeof a === "boolean" ||
|
|
303
|
+
typeof a === "bigint"
|
|
304
|
+
) {
|
|
305
|
+
return a.toString();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (typeof a === "function") {
|
|
309
|
+
return a.name ? `[Function: ${a.name}]` : "[Function]";
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (a instanceof Date) {
|
|
313
|
+
return a.toISOString();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (Array.isArray(a)) {
|
|
317
|
+
return `[${a.map(convert).join(", ")}]`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (typeof a === "object") {
|
|
321
|
+
try {
|
|
322
|
+
return JSON.stringify(a);
|
|
323
|
+
} catch {
|
|
324
|
+
return "[Object]";
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return String(a);
|
|
274
329
|
}
|