-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.runtime.js
More file actions
71 lines (65 loc) · 1.5 KB
/
util.runtime.js
File metadata and controls
71 lines (65 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module.exports = function (waw) {
// wait
waw.wait = async (time) => {
return await new Promise((resolve) => setTimeout(resolve, time));
};
// http
waw.http = function (hostname, port = 443) {
const post = (method) => {
return async function (path, body, callback) {
try {
const response = await fetch(hostname + ":" + port + path, {
method,
headers: {
...this.headers,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) {
callback(false);
return false;
}
const resp = await response.json();
callback(resp);
return resp;
} catch (error) {
callback(false);
return false;
}
};
};
return {
headers: {},
get: async function (path, callback = (resp) => {}) {
try {
const response = await fetch(hostname + ":" + port + path, {
method: "GET",
headers: this.headers,
});
if (!response.ok) {
callback(false);
return false;
}
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
const resp = await response.json();
callback(resp);
return resp;
} else {
const resp = await response.text();
callback(resp);
return resp;
}
} catch (error) {
callback(false);
return false;
}
},
post: post("POST"),
put: post("PUT"),
patch: post("PATCH"),
delete: post("DELETE"),
};
};
};