@soleil-se/app-util 5.6.4 → 5.7.0
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/CHANGELOG.md +8 -0
- package/client/fetch-json/index.js +45 -8
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,14 @@ All notable changes to this project will be documented in this file.
|
|
|
7
7
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
8
8
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
9
9
|
|
|
10
|
+
## [5.7.0] - 2024-05-23
|
|
11
|
+
|
|
12
|
+
- Add widget support for `fetchJson`.
|
|
13
|
+
|
|
14
|
+
## [5.6.5] - 2024-01-25
|
|
15
|
+
|
|
16
|
+
- Fix asynchronous handling of error in `fetchJson`.
|
|
17
|
+
|
|
10
18
|
## [5.6.4] - 2024-01-18
|
|
11
19
|
|
|
12
20
|
- Cast `Packages.java.util.UUID.randomUUID()` to `string`.
|
|
@@ -16,19 +16,20 @@ async function toJson(response) {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
async function
|
|
19
|
+
async function responseError(response) {
|
|
20
20
|
const json = await toJson(response) || {};
|
|
21
21
|
const error = new Error(json?.message || response?.statusText);
|
|
22
22
|
Object.entries(json).forEach(([key, value]) => {
|
|
23
23
|
error[key] = value;
|
|
24
24
|
});
|
|
25
25
|
error.status = response.status;
|
|
26
|
-
|
|
26
|
+
return error;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
async function handleResponse(response) {
|
|
30
|
-
if (!response.ok)
|
|
31
|
-
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw await responseError(response);
|
|
32
|
+
}
|
|
32
33
|
const json = await toJson(response);
|
|
33
34
|
if (!json) {
|
|
34
35
|
throw new Error('Response could not be parsed as JSON.');
|
|
@@ -36,13 +37,49 @@ async function handleResponse(response) {
|
|
|
36
37
|
return json;
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
return
|
|
40
|
+
function isWidget() {
|
|
41
|
+
return window.document.location.pathname.startsWith('/edit-dashboard');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getWidgetOptions(options) {
|
|
45
|
+
return {
|
|
46
|
+
...options,
|
|
47
|
+
params: {
|
|
48
|
+
svAjaxReqParam: 'ajax',
|
|
49
|
+
...options.params,
|
|
50
|
+
},
|
|
51
|
+
headers: {
|
|
52
|
+
...options.headers,
|
|
53
|
+
'X-Requested-With': 'XMLHttpRequest',
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Fetches JSON data from a specified URI.
|
|
60
|
+
* URI:s starting with `/rest-api`, `/appresource`, `/edit-app-config` or a protocol, for example
|
|
61
|
+
* `https://` will be left as is. Other URI:s willbe converted to match a route in the current app
|
|
62
|
+
* with `getRouteUri`.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} uri - The URI to fetch JSON data from.
|
|
65
|
+
* @param {object} options - The options for the fetch request, see <https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters>.
|
|
66
|
+
* @param {string} options.method - The HTTP method to use in the request.
|
|
67
|
+
* @param {object} options.params - The parameters to be included in the request URL.
|
|
68
|
+
* @param {object} options.body - The body to include in the request.
|
|
69
|
+
* @param {number} options.retries - The number of retries to attempt in case of a timeout error.
|
|
70
|
+
* @param {object} options.headers - The headers to be included in the request.
|
|
71
|
+
* @returns {Promise<object>} A promise that resolves to the JSON response.
|
|
72
|
+
* @throws {Error} If the response is not successful or cannot be parsed as JSON.
|
|
73
|
+
*/
|
|
74
|
+
export default function fetchJson(uri, options) {
|
|
75
|
+
const { params = {}, retries = 0, ...rest } = isWidget() ? getWidgetOptions(options) : options;
|
|
76
|
+
|
|
77
|
+
return fetch(getUrl(uri, params), rest)
|
|
41
78
|
.then(handleResponse)
|
|
42
79
|
.catch((error) => {
|
|
43
|
-
const isTimeout = error.status === 504 || error.status === 408 || error.message
|
|
80
|
+
const isTimeout = error.status === 504 || error.status === 408 || error.message?.includes('SocketTimeoutException');
|
|
44
81
|
if (isTimeout && retries > 0) {
|
|
45
|
-
return fetchJson(uri, { ...
|
|
82
|
+
return fetchJson(uri, { ...rest, params, retries: retries - 1 });
|
|
46
83
|
}
|
|
47
84
|
// eslint-disable-next-line no-param-reassign
|
|
48
85
|
error.aborted = error.name === 'AbortError';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soleil-se/app-util",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.0",
|
|
4
4
|
"description": "Utility and rendering functions for WebApps.",
|
|
5
5
|
"main": "./common/index.js",
|
|
6
6
|
"author": "Soleil AB",
|
|
@@ -14,6 +14,6 @@
|
|
|
14
14
|
"peerDependencies": {
|
|
15
15
|
"@sitevision/api": "*"
|
|
16
16
|
},
|
|
17
|
-
"gitHead": "
|
|
17
|
+
"gitHead": "d555d9d6df015ae9876ff6d7d9c127a14b39a132",
|
|
18
18
|
"dependencies": {}
|
|
19
19
|
}
|