@geode/opengeodeweb-front 10.30.0-rc.4 → 10.30.0-rc.6
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/app/stores/cloud.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Status } from "@ogw_front/utils/status";
|
|
2
|
+
import { fetchSchema } from "@ogw_shared/utils/fetch_schema";
|
|
2
3
|
import { setAppBaseUrl } from "@ogw_shared/scripts";
|
|
3
4
|
import { useAppStore } from "./app";
|
|
4
5
|
import { useFeedbackStore } from "./feedback";
|
|
@@ -26,7 +27,7 @@ export const useCloudStore = defineStore("cloud", {
|
|
|
26
27
|
console.log("[CLOUD] params", params);
|
|
27
28
|
const appStore = useAppStore();
|
|
28
29
|
const feedbackStore = useFeedbackStore();
|
|
29
|
-
return
|
|
30
|
+
return fetchSchema(
|
|
30
31
|
{ schema, params },
|
|
31
32
|
{
|
|
32
33
|
request_error_function: () => {
|
package/package.json
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Third party imports
|
|
2
|
+
import _ from "lodash";
|
|
3
|
+
import pTimeout from "p-timeout";
|
|
4
|
+
|
|
5
|
+
// Local imports
|
|
6
|
+
import { hasBody } from "./file.js";
|
|
7
|
+
|
|
8
|
+
function fetchRaw(
|
|
9
|
+
{ route, method, params = {}, baseURL, headers = {}, max_retry, timeout, expectEvent = false },
|
|
10
|
+
{ request_error_function, response_function, response_error_function } = {},
|
|
11
|
+
) {
|
|
12
|
+
if (expectEvent) {
|
|
13
|
+
const value = "text/event-stream";
|
|
14
|
+
if (_.isEmpty(headers)) {
|
|
15
|
+
headers["Accept"] = value;
|
|
16
|
+
} else {
|
|
17
|
+
headers["Accept"] = `${headers["Accept"]}, ${value}`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const request_options = { method, headers };
|
|
22
|
+
if (hasBody(params)) {
|
|
23
|
+
request_options.body = params;
|
|
24
|
+
}
|
|
25
|
+
if (max_retry) {
|
|
26
|
+
request_options.max_retry = max_retry;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function doFetch() {
|
|
30
|
+
return $fetch(route, {
|
|
31
|
+
baseURL,
|
|
32
|
+
...request_options,
|
|
33
|
+
onRequestError({ error }) {
|
|
34
|
+
if (request_error_function) {
|
|
35
|
+
request_error_function(error);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
onResponse({ response }) {
|
|
39
|
+
if (response.ok && response_function) {
|
|
40
|
+
response_function(response._data);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
onResponseError({ response }) {
|
|
44
|
+
if (response_error_function) {
|
|
45
|
+
response_error_function(response);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (timeout > 0) {
|
|
52
|
+
return pTimeout(doFetch(), {
|
|
53
|
+
milliseconds: timeout,
|
|
54
|
+
message: `${route}: Timed out after ${timeout}ms`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return doFetch();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { fetchRaw };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Third party imports
|
|
2
|
+
|
|
3
|
+
// Local imports
|
|
4
|
+
import { fetchRaw } from "./fetch_raw.js";
|
|
5
|
+
import { validate_schema } from "./validate_schema.js";
|
|
6
|
+
|
|
7
|
+
const ERROR_400 = 400;
|
|
8
|
+
|
|
9
|
+
function fetchSchema(
|
|
10
|
+
{ schema, params = {}, baseURL, headers, timeout, expectEvent = false },
|
|
11
|
+
{
|
|
12
|
+
request_error_function,
|
|
13
|
+
response_function,
|
|
14
|
+
response_error_function,
|
|
15
|
+
validation_error_function,
|
|
16
|
+
} = {},
|
|
17
|
+
) {
|
|
18
|
+
console.log("fetchSchema", { schema, baseURL, params, headers, timeout });
|
|
19
|
+
const { valid, error: schema_error } = validate_schema(schema, params);
|
|
20
|
+
|
|
21
|
+
if (!valid) {
|
|
22
|
+
if (process.env.NODE_ENV !== "production") {
|
|
23
|
+
console.log("Bad request", schema_error, schema, params);
|
|
24
|
+
}
|
|
25
|
+
if (validation_error_function) {
|
|
26
|
+
validation_error_function({ code: ERROR_400, name: "Bad request", error: schema_error });
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`${schema.$id}: ${schema_error}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return fetchRaw(
|
|
32
|
+
{
|
|
33
|
+
route: schema.$id,
|
|
34
|
+
method: schema.methods.find((method) => method !== "OPTIONS"),
|
|
35
|
+
params,
|
|
36
|
+
baseURL,
|
|
37
|
+
headers,
|
|
38
|
+
max_retry: schema.max_retry,
|
|
39
|
+
timeout,
|
|
40
|
+
expectEvent,
|
|
41
|
+
},
|
|
42
|
+
{ request_error_function, response_function, response_error_function },
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { fetchSchema };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
|
|
3
|
+
function assertFile(file) {
|
|
4
|
+
console.log("[ASSERT_FILE] Asserting file", { file });
|
|
5
|
+
if (!(file instanceof File)) {
|
|
6
|
+
throw new Error("file must be an instance of File");
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function hasBody(params) {
|
|
11
|
+
if (params instanceof FormData || params instanceof Blob) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
return !_.isEmpty(params);
|
|
15
|
+
}
|
|
16
|
+
export { assertFile, hasBody };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import Ajv from "ajv";
|
|
2
|
+
|
|
3
|
+
function validate_schema(schema, body) {
|
|
4
|
+
const ajv = new Ajv();
|
|
5
|
+
const list_keywords = ["methods", "route", "max_retry", "rpc"];
|
|
6
|
+
for (const keyword of list_keywords) {
|
|
7
|
+
ajv.addKeyword(keyword);
|
|
8
|
+
}
|
|
9
|
+
const valid = ajv.validate(schema, body);
|
|
10
|
+
return { valid, error: ajv.errorsText() };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { validate_schema };
|