@arena-ui/services 1.0.0 → 1.0.2
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/index.js +3 -3
- package/package.json +1 -1
- package/post_request.js +24 -6
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import get_service from "./get_service";
|
|
2
|
-
import load_services from "./load_services";
|
|
3
|
-
import post_request from "./post_request";
|
|
1
|
+
import get_service from "./get_service.js";
|
|
2
|
+
import load_services from "./load_services.js";
|
|
3
|
+
import post_request from "./post_request.js";
|
|
4
4
|
|
|
5
5
|
export { post_request, get_service, load_services };
|
package/package.json
CHANGED
package/post_request.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { PROD } from "@env";
|
|
2
1
|
import get_service from "./get_service.js";
|
|
3
2
|
|
|
4
|
-
const
|
|
3
|
+
const cached = {};
|
|
4
|
+
|
|
5
|
+
const post_request = async (url, body, options = {}) => {
|
|
5
6
|
let response;
|
|
7
|
+
let { cache, method } = options;
|
|
8
|
+
method = method || "POST";
|
|
6
9
|
|
|
7
10
|
if (url.startsWith("$")) {
|
|
8
11
|
url = url.split("$");
|
|
@@ -10,9 +13,7 @@ const post_request = async (url, body) => {
|
|
|
10
13
|
|
|
11
14
|
let config = get_service(url[0]);
|
|
12
15
|
|
|
13
|
-
let domain = config
|
|
14
|
-
|
|
15
|
-
domain = domain[PROD ? "prod" : "dev"];
|
|
16
|
+
let domain = config?.url;
|
|
16
17
|
|
|
17
18
|
if (!domain) {
|
|
18
19
|
return {
|
|
@@ -23,9 +24,18 @@ const post_request = async (url, body) => {
|
|
|
23
24
|
url = `${domain}/${url[1]}`;
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
if (cache) {
|
|
28
|
+
let cached_ = cached[url];
|
|
29
|
+
if (cached_ && method === cached_?.method) {
|
|
30
|
+
if (Date.now() - cached_.then <= cache) {
|
|
31
|
+
if (cached_.content) return cached_.content;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
26
36
|
try {
|
|
27
37
|
let ftch = await fetch(url, {
|
|
28
|
-
method
|
|
38
|
+
method,
|
|
29
39
|
headers: {
|
|
30
40
|
"Content-Type": "application/json",
|
|
31
41
|
Accept: "application/json",
|
|
@@ -34,6 +44,14 @@ const post_request = async (url, body) => {
|
|
|
34
44
|
});
|
|
35
45
|
|
|
36
46
|
response = await ftch.json();
|
|
47
|
+
|
|
48
|
+
if (cache) {
|
|
49
|
+
cached = {
|
|
50
|
+
content: response,
|
|
51
|
+
then: Date.now(),
|
|
52
|
+
method,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
37
55
|
} catch (e) {
|
|
38
56
|
console.log(e);
|
|
39
57
|
response = {
|