@soleil-se/app-util 5.5.2 → 5.6.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 +7 -0
- package/README.md +42 -1
- package/client/fetch-json/index.js +18 -11
- package/common/index.js +28 -26
- package/common/legacy/getRouteUri.js +3 -2
- package/package.json +2 -2
- package/server/index.js +7 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [5.6.0] - 2023-11-23
|
|
9
|
+
|
|
10
|
+
- New constants: `isServer` and `isBrowser`.
|
|
11
|
+
- New function: `generateId`.
|
|
12
|
+
- Throw more clear error when JSON response can't be parsed in `fetchJson`.
|
|
13
|
+
- Webpack support for usage with `@sitevision/sitevision-scripts`.
|
|
14
|
+
|
|
8
15
|
## [5.5.2] - 2023-08-28
|
|
9
16
|
|
|
10
17
|
- Respect hash when updating URL parameters with `updateUrlParams` and `setUrlParams`.
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ Utility functions for WebApps.
|
|
|
11
11
|
## Requirements
|
|
12
12
|
|
|
13
13
|
* Sitevision 9.1.0 or later.
|
|
14
|
-
* `@soleil-se/sv-app-build
|
|
14
|
+
* `@soleil-se/sv-app-build@^1.0.0` or `@sitevision/sitevision-scripts@^3.0.0`.
|
|
15
15
|
* WebApps 2 enabled app.
|
|
16
16
|
|
|
17
17
|
### Manifest
|
|
@@ -86,6 +86,26 @@ import { isOnline } from '@soleil-se/webapp-util';
|
|
|
86
86
|
console.log(isOnline); // true or false
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
+
### isServer : `Boolean`
|
|
90
|
+
|
|
91
|
+
If the current code is running on the server.
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
import { isServer } from '@soleil-se/webapp-util';
|
|
95
|
+
|
|
96
|
+
console.log(isServer); // true or false
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### isBrowser : `Boolean`
|
|
100
|
+
|
|
101
|
+
If the current code is running in the browser.
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
import { isBrowser } from '@soleil-se/webapp-util';
|
|
105
|
+
|
|
106
|
+
console.log(isBrowser); // true or false
|
|
107
|
+
```
|
|
108
|
+
|
|
89
109
|
### getNamespace([prefix]) ⇒ `String`
|
|
90
110
|
|
|
91
111
|
Get a prefixed namespace unique for app.
|
|
@@ -110,6 +130,27 @@ console.log(getNamespace('decoration'));
|
|
|
110
130
|
// For example: decoration_10_3871c02f1754f3aa8f9d4eb_12_70c3d424173b4900fc550e1c
|
|
111
131
|
```
|
|
112
132
|
|
|
133
|
+
### generateId([prefix]) ⇒ `String`
|
|
134
|
+
|
|
135
|
+
Generate a unique identifier with a random UUID without dashes.
|
|
136
|
+
|
|
137
|
+
**Returns**: `String` - Unique identifier.
|
|
138
|
+
|
|
139
|
+
| Param | Type | Default |
|
|
140
|
+
| --- | --- | --- |
|
|
141
|
+
| [prefix] | `string` | `"'id'"` |
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import { generateId } from '@soleil-se/webapp-util';
|
|
145
|
+
|
|
146
|
+
console.log(generateId());
|
|
147
|
+
// For example: id_550e8400e29b41d4a716446655440000
|
|
148
|
+
|
|
149
|
+
console.log(generateId('input'));
|
|
150
|
+
// For example: input_550e8400e29b41d4a716446655440000
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
|
|
113
154
|
### getRouteUri(route, [query]) ⇒ `String`
|
|
114
155
|
|
|
115
156
|
Get URI for a route.
|
|
@@ -7,25 +7,32 @@ function getUrl(uri, params) {
|
|
|
7
7
|
return getRouteUri(uri, params);
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
function toJson(response) {
|
|
10
|
+
async function toJson(response) {
|
|
11
|
+
const text = await response.text();
|
|
11
12
|
try {
|
|
12
|
-
return
|
|
13
|
+
return JSON.parse(text);
|
|
13
14
|
} catch (e) {
|
|
14
|
-
return
|
|
15
|
+
return undefined;
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
async function handleError(response) {
|
|
20
|
+
const json = await toJson(response) || {};
|
|
21
|
+
const error = new Error(json?.message || response?.statusText);
|
|
22
|
+
Object.entries(json).forEach(([key, value]) => {
|
|
23
|
+
error[key] = value;
|
|
24
|
+
});
|
|
25
|
+
error.status = response.status;
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
|
|
18
29
|
async function handleResponse(response) {
|
|
30
|
+
if (!response.ok) handleError(response);
|
|
31
|
+
|
|
19
32
|
const json = await toJson(response);
|
|
20
|
-
if (!
|
|
21
|
-
|
|
22
|
-
Object.entries(json).forEach(([key, value]) => {
|
|
23
|
-
error[key] = value;
|
|
24
|
-
});
|
|
25
|
-
error.status = response.status;
|
|
26
|
-
throw error;
|
|
33
|
+
if (!json) {
|
|
34
|
+
throw new Error('Response could not be parsed as JSON.');
|
|
27
35
|
}
|
|
28
|
-
|
|
29
36
|
return json;
|
|
30
37
|
}
|
|
31
38
|
|
package/common/index.js
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
|
-
/* eslint-disable global-require */
|
|
2
1
|
import router from '@sitevision/api/common/router';
|
|
3
2
|
import app from '@sitevision/api/common/app';
|
|
4
3
|
|
|
4
|
+
import { nativeRequire } from '../server';
|
|
5
5
|
import getLegacyRouteUri from './legacy/getRouteUri';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Get an ID for the app.
|
|
9
|
-
* @return {String} ID
|
|
10
|
-
*/
|
|
11
|
-
const getAppId = () => {
|
|
12
|
-
const id = (process.server
|
|
13
|
-
? require('PortletContextUtil')?.getCurrentPortlet()?.getIdentifier()
|
|
14
|
-
: app?.portletId) || 'preview';
|
|
15
|
-
|
|
16
|
-
return id.replace('.', '_');
|
|
17
|
-
};
|
|
18
|
-
|
|
19
7
|
/**
|
|
20
8
|
* Regex for selecting leading slashes
|
|
21
9
|
* @constant {Regex}
|
|
@@ -28,18 +16,30 @@ const leadingSlashes = /^(\/)*/g;
|
|
|
28
16
|
*/
|
|
29
17
|
const trailingSlashes = /(\/)*$/g;
|
|
30
18
|
|
|
19
|
+
/**
|
|
20
|
+
* If the current code is running on the server.
|
|
21
|
+
* @constant {boolean}
|
|
22
|
+
*/
|
|
23
|
+
export const isServer = typeof process !== 'undefined' ? process.server : typeof window === 'undefined';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* If the current code is running in the browser.
|
|
27
|
+
* @constant {boolean}
|
|
28
|
+
*/
|
|
29
|
+
export const isBrowser = !isServer;
|
|
30
|
+
|
|
31
31
|
/**
|
|
32
32
|
* DOM friendly unique identifier for the WebApp.
|
|
33
33
|
* @constant {string}
|
|
34
34
|
*/
|
|
35
|
-
export const appId =
|
|
35
|
+
export const appId = (app?.portletId || 'preview').replace('.', '_');
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* If the WebApp is running in offline mode or not.
|
|
39
39
|
* @constant {boolean}
|
|
40
40
|
*/
|
|
41
|
-
export const isOffline =
|
|
42
|
-
?
|
|
41
|
+
export const isOffline = isServer
|
|
42
|
+
? nativeRequire('VersionUtil').getCurrentVersion() === nativeRequire('VersionUtil').OFFLINE_VERSION
|
|
43
43
|
: document.documentElement.classList.contains('sv-edit-mode');
|
|
44
44
|
|
|
45
45
|
/**
|
|
@@ -51,12 +51,22 @@ export const isOnline = !isOffline;
|
|
|
51
51
|
/**
|
|
52
52
|
* Get a prefixed namespace unique for app.
|
|
53
53
|
* @param {string} [prefix='app']
|
|
54
|
-
* @return {
|
|
54
|
+
* @return {string} - Prefixed namespace.
|
|
55
55
|
*/
|
|
56
56
|
export function getNamespace(prefix = 'app') {
|
|
57
57
|
return `${prefix}_${appId}`;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Generate a unique identifier with a random UUID without dashes.
|
|
62
|
+
* @param {string} prefix Prefix for the identifier.
|
|
63
|
+
* @returns {string} Unique identifier.
|
|
64
|
+
*/
|
|
65
|
+
export function generateId(prefix = 'id') {
|
|
66
|
+
const uuid = isServer ? Packages.java.util.UUID.randomUUID() : window.crypto.randomUUID();
|
|
67
|
+
return `${prefix}_${uuid}`.replace(/-/g, '');
|
|
68
|
+
}
|
|
69
|
+
|
|
60
70
|
/**
|
|
61
71
|
* Stringify an object to a query string compatible with Sitevision.
|
|
62
72
|
* @param {Object} params Object with parameters to stringify.
|
|
@@ -147,15 +157,7 @@ export function getViewUri(route = '', params = undefined) {
|
|
|
147
157
|
* @returns {string} URI for a resource.
|
|
148
158
|
*/
|
|
149
159
|
export function getResourceUri(resource = '') {
|
|
150
|
-
|
|
151
|
-
let version;
|
|
152
|
-
|
|
153
|
-
if (process.server) {
|
|
154
|
-
({ appIdentifier: id, appVersion: version } = require('appInfo'));
|
|
155
|
-
} else {
|
|
156
|
-
({ webAppId: id, webAppVersion: version } = app);
|
|
157
|
-
}
|
|
158
|
-
|
|
160
|
+
const { webAppId: id, webAppVersion: version } = app;
|
|
159
161
|
const path = resource.replace(leadingSlashes, '');
|
|
160
162
|
return `/webapp-files/${id}/${version}/${path}`;
|
|
161
163
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { nativeRequire } from '../../server';
|
|
2
|
+
|
|
2
3
|
export default function getRouteUri(route) {
|
|
3
|
-
const PortletContextUtil =
|
|
4
|
+
const PortletContextUtil = nativeRequire('PortletContextUtil');
|
|
4
5
|
const currentPage = PortletContextUtil.getCurrentPage();
|
|
5
6
|
const currentPortlet = PortletContextUtil.getCurrentPortlet();
|
|
6
7
|
if (currentPage && currentPortlet) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soleil-se/app-util",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.6.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": "d3fa2582d5479445835e6d6df052f7160b1eae1b",
|
|
18
18
|
"dependencies": {}
|
|
19
19
|
}
|
package/server/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/* Make native requires work with @sitevision/sitevision-scripts that uses Webpack */
|
|
2
|
+
/* eslint-disable camelcase, no-undef, import/no-dynamic-require, global-require */
|
|
3
|
+
export function nativeRequire(module) {
|
|
4
|
+
if (typeof __non_webpack_require__ !== 'undefined') return __non_webpack_require__(module);
|
|
5
|
+
return require(module);
|
|
6
|
+
}
|
|
7
|
+
/* eslint-enable camelcase, no-undef, import/no-dynamic-require, global-require */
|