corexxx 1.0.133 → 1.0.134
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.d.ts +34 -3
- package/dist/dnetwork.js +64 -39
- package/dist/dnetwork.js.map +1 -1
- package/package.json +1 -1
- package/src/dnetwork.ts +108 -47
- package/test/dnetwork.test.ts +140 -0
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,CAAC,CAAC,OAAO,GAAG,EAAE,CAAC,CAAC;YACrB,MAAM,MAAM,GAAG,MAAM,eAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YAC5C,iBAAO,CAAC,aAAa,CACnB,MAAM,CAAC,MAAM,IAAI,GAAG,IAAI,MAAM,CAAC,MAAM,GAAG,GAAG,EAC3C,sBAAsB,GAAG,uBAAuB,MAAM,CAAC,MAAM,EAAE,CAChE,CAAC;YACF,OAAO,MAAM,CAAC,IAAI,CAAC;QACrB,CAAC;KAAA;IAXqB,YAAG,MAWxB,CAAA;IAED;;OAEG;IACH,SAAsB,OAAO,CAC3B,GAAW,EACX,MAA2B;;YAE3B,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YACnC,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG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AepC,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
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,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
|
+
});
|