@soleil-se/app-util 5.0.0 → 5.1.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 CHANGED
@@ -5,7 +5,16 @@ 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.0.0] - 2021-xx-xx
8
+ ## [5.1.0] - 2021-05-27
9
+
10
+ ### Changed
11
+
12
+ - New function `stringifyParams` to stringify query parameters to a Sitevision compatible format.
13
+ - Possible to pass a parameters object to `getRouteUri`.
14
+ - All common constants and functions are now usable in hooks.
15
+ - Deprecate `getViewUri` since it's not used in WebApps 2.
16
+
17
+ ## [5.0.0] - 2021-02-08
9
18
 
10
19
  ### Changed
11
20
 
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Utility functions for WebApps.
5
5
 
6
- [GitHub](https://github.com/soleilit/server-monorepo/tree/master/packages/webapp-util)
6
+ [GitHub](https://github.com/soleilit/server-monorepo/tree/master/packages/webapp-util)
7
7
  [NPM](https://www.npmjs.com/package/@soleil-se/webapp-util)
8
8
 
9
9
 
@@ -38,49 +38,13 @@ Utility functions for WebApps.
38
38
 
39
39
  ## Migration
40
40
 
41
- Migrating from version 4?
41
+ Migrating from version 4?
42
42
  See [MIGRATION](./MIGRATION.md).
43
43
 
44
44
  ## API
45
45
 
46
46
  All imports from base package are available both on the server and client.
47
47
 
48
- ### Constants
49
-
50
- <dl>
51
- <dt><a href="#appId">appId</a> : <code>String</code></dt>
52
- <dd><p>DOM friendly unique identifier for the WebApp.</p>
53
- </dd>
54
- <dt><a href="#isOffline">isOffline</a> : <code>Boolean</code></dt>
55
- <dd><p>If the WebApp is running in offline mode or not.</p>
56
- </dd>
57
- <dt><a href="#isOnline">isOnline</a> : <code>Boolean</code></dt>
58
- <dd><p>If the WebApp is running in online mode or not.</p>
59
- </dd>
60
- </dl>
61
-
62
- ### Functions
63
-
64
- <dl>
65
- <dt><a href="#getNamespace">getNamespace([prefix])</a> ⇒ <code>String</code></dt>
66
- <dd><p>Get a prefixed namespace unique for app.</p>
67
- </dd>
68
- <dt><a href="#getRouteUri">getRouteUri(route)</a> ⇒ <code>String</code></dt>
69
- <dd><p>Get URI for a route.
70
- </dd>
71
- <dt><a href="#getViewUri">getViewUri(route)</a> ⇒ <code>String</code></dt>
72
- <dd><p>Get URI for a view.
73
- </dd>
74
- <dt><a href="#getResourceUri">getResourceUri(resource)</a> ⇒ <code>String</code></dt>
75
- <dd><p>Get URI for a resource.</p>
76
- </dd>
77
- <dt><a href="#getAppProps">getAppProps([key])</a> ⇒ <code>*</code> | <code>Object</code></dt>
78
- <dd><p>Get props that are passed to app when rendering.</p>
79
- </dd>
80
- </dl>
81
-
82
- <a name="appId"></a>
83
-
84
48
  ### appId : `String`
85
49
 
86
50
  DOM friendly unique identifier for the WebApp.
@@ -91,8 +55,6 @@ import { appId } from '@soleil-se/webapp-util';
91
55
  console.log(appId); // For example: 12_682d461b1708a9bb1ea13efd
92
56
  ```
93
57
 
94
- <a name="isOffline"></a>
95
-
96
58
  ### isOffline : `Boolean`
97
59
 
98
60
  If the WebApp is running in offline mode or not.
@@ -103,8 +65,6 @@ import { isOffline } from '@soleil-se/webapp-util';
103
65
  console.log(isOffline); // true or false
104
66
  ```
105
67
 
106
- <a name="isOnline"></a>
107
-
108
68
  ### isOnline : `Boolean`
109
69
 
110
70
  If the WebApp is running in online mode or not.
@@ -139,9 +99,7 @@ console.log(getNamespace('decoration'));
139
99
  // For example: decoration_10_3871c02f1754f3aa8f9d4eb_12_70c3d424173b4900fc550e1c
140
100
  ```
141
101
 
142
- <a name="getRouteUri"></a>
143
-
144
- ### getRouteUri(route) ⇒ `String`
102
+ ### getRouteUri(route, [query]) ⇒ `String`
145
103
 
146
104
  Get URI for a route.
147
105
 
@@ -150,35 +108,22 @@ Get URI for a route.
150
108
  | Param | Type | Description |
151
109
  | --- | --- | --- |
152
110
  | route | `String` | A route. |
111
+ | query | `Object` | Object with query string parameters |
153
112
 
154
113
  ```js
155
114
  import { getRouteUri } from '@soleil-se/webapp-util';
156
115
 
157
116
  console.log(getRouteUri('/items'));
158
- // URI structure: /appresource/<pageId>/<portletId>/items
117
+ // URI structure: /appresource/{pageId}/{portletId}>/items
159
118
  ```
160
119
 
161
- <a name="getViewUri"></a>
162
-
163
- ### getViewUri(route) ⇒ `String`
164
-
165
- Get URI for a view.
166
-
167
- **Returns**: `String` - URI for view.
168
-
169
- | Param | Type | Description |
170
- | --- | --- | --- |
171
- | route | `String` | A route. |
172
-
173
120
  ```js
174
- import { getViewUri } from '@soleil-se/webapp-util';
121
+ import { getRouteUri } from '@soleil-se/webapp-util';
175
122
 
176
- console.log(getViewUri('/items'));
177
- // URI structure: ?sv.target=<id>&sv.<id>.route=/items
123
+ console.log(getRouteUri('/items', { foo: 'bar' }));
124
+ // URI structure: /appresource/{pageId}/{portletId}>/items?foo=bar
178
125
  ```
179
126
 
180
- <a name="getResourceUri"></a>
181
-
182
127
  ### getResourceUri(resource) ⇒ `String`
183
128
 
184
129
  Get URI for a resource.
@@ -196,8 +141,6 @@ console.log(getResourceUri('/image.png'));
196
141
  // URI structure: /webapp-files/<webappname>/<webappversion>/image.png
197
142
  ```
198
143
 
199
- <a name="getAppProps"></a>
200
-
201
144
  ### getAppProps([key]) ⇒ `*` | `Object`
202
145
 
203
146
  Get props that are passed to app when rendering.
@@ -217,7 +160,28 @@ const myValue = getAppProps('myValue');
217
160
  const { myValue } = getAppProps();
218
161
  ```
219
162
 
163
+ ### stringifyParams(params [, options]) ⇒ `String`
164
+
165
+ Stringify an object to a query string compatible with Sitevision.
166
+
167
+ **Returns**: `String` - Stringified parameters.
168
+
169
+ | Param | Type | Default | Description |
170
+ | --- | --- | --- | --- |
171
+ | params | `Object` | | Object with parameters to stringify. |
172
+ | [options] | `Object` | `{}` | Settings object. |
173
+ | [options.addQueryPrefix] | `Boolean` | `false` | If a leading `?` should be added to the string. |
174
+
175
+ ```js
176
+ import { stringifyParams } from '@soleil-se/webapp-util';
177
+
178
+ const queryString = stringifyParams({ foo: 'bar', num: 1 });
179
+ // foo=bar&num=1
180
+
181
+ const queryString = stringifyParams({ foo: 'bar', num: 1 }, { addQueryPrefix: true });
182
+ // ?foo=bar&num=1
183
+ ```
184
+
220
185
  ## Rendering
221
186
 
222
187
  * [Svelte](./docs/1.svelte.md)
223
- * [Vue (DEPRECATED)](./docs/2.vue.md)
package/common/index.js CHANGED
@@ -1,14 +1,21 @@
1
- /* global document */
1
+ /* global document console */
2
2
  /* eslint-disable global-require */
3
-
4
3
  import router from '@sitevision/api/common/router';
5
4
  import app from '@sitevision/api/common/app';
6
5
 
6
+ import getLegacyRouteUri from './legacy/getRouteUri';
7
+
7
8
  /**
8
9
  * Get an ID for the app.
9
10
  * @return {String} ID
10
11
  */
11
- const getAppId = () => app.portletId.replace('.', '_');
12
+ const getAppId = () => {
13
+ const id = (process.server
14
+ ? require('PortletContextUtil')?.getCurrentPortlet()?.getIdentifier()
15
+ : app?.portletId) || 'preview';
16
+
17
+ return id.replace('.', '_');
18
+ };
12
19
 
13
20
  /**
14
21
  * Regex for selecting leading slashes
@@ -51,24 +58,57 @@ export function getNamespace(prefix = 'app') {
51
58
  return `${prefix}_${appId}`;
52
59
  }
53
60
 
61
+ /**
62
+ * Stringify an object to a query string compatible with Sitevision.
63
+ * @param {Object} params Object with parameters to stringify.
64
+ * @param {Object} [options] Optional options.
65
+ * @param {Boolean} [options.addQueryPrefix = false] If a leading ? should be added to the string.
66
+ * @returns Stringified parameters.
67
+ */
68
+ export function stringifyParams(params = {}, { addQueryPrefix = false } = {}) {
69
+ const qs = Object.keys(params).map((key) => {
70
+ const value = params[key];
71
+ if (!value) return undefined;
72
+ if (Array.isArray(value)) {
73
+ return value.map((v) => `${encodeURIComponent(`${key}[]`)}=${encodeURIComponent(v)}`).join('&');
74
+ }
75
+ return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
76
+ }).filter((value) => !!value).join('&');
77
+
78
+ if (!qs) return '';
79
+ return addQueryPrefix ? `?${qs}` : qs;
80
+ }
81
+
54
82
  /**
55
83
  * Get URI for a route, same as `getStandaloneUrl` in Sitevision router.
56
84
  * @param {string} route A route.
57
- * @param {object} query Query parameters.
85
+ * @param {object} params Query parameters.
58
86
  * @returns {string} URI for route.
59
87
  */
60
- export function getRouteUri(route = '', query) {
88
+ export function getRouteUri(route = '', params) {
61
89
  const path = route.replace(leadingSlashes, '');
62
- return router.getStandaloneUrl(path !== '' ? `/${path}` : path, query).replace(trailingSlashes, '');
90
+ const qs = stringifyParams(params, { addQueryPrefix: true });
91
+ if (router?.getStandaloneUrl) {
92
+ return router.getStandaloneUrl((path !== '' ? `/${path}` : path).replace(trailingSlashes, '')) + qs;
93
+ }
94
+ /* Import of @sitevision/api/common/router is not avaliable in hooks, need to build the route URI
95
+ * manually instead of using router.getStandaloneUri */
96
+ return getLegacyRouteUri(path).replace(trailingSlashes, '') + qs;
63
97
  }
64
98
 
65
99
  /**
66
100
  * Get URI for a view, same as `getUrl` in Sitevision router.
101
+ * @deprecated Not used in WebApps 2.
67
102
  * @param {string} route A route.
68
103
  * @returns {string} URI for view.
69
104
  */
70
- export function getViewUri(route = '') {
71
- return router.getUrl(route);
105
+ export function getViewUri(route = '', query) {
106
+ if (!router?.getUrl) {
107
+ console.warn('[@soleil-api/webapp-util] getViewUri requires router.getUrl support.');
108
+ return undefined;
109
+ }
110
+
111
+ return router.getUrl(route, query);
72
112
  }
73
113
 
74
114
  /**
@@ -77,9 +117,17 @@ export function getViewUri(route = '') {
77
117
  * @returns {string} URI for a resource.
78
118
  */
79
119
  export function getResourceUri(resource = '') {
80
- const { webAppId, webAppVersion } = app;
120
+ let id;
121
+ let version;
122
+
123
+ if (process.server) {
124
+ ({ appIdentifier: id, appVersion: version } = require('appInfo'));
125
+ } else {
126
+ ({ webAppId: id, webAppVersion: version } = app);
127
+ }
128
+
81
129
  const path = resource.replace(leadingSlashes, '');
82
- return `/webapp-files/${webAppId}/${webAppVersion}/${path}`;
130
+ return `/webapp-files/${id}/${version}/${path}`;
83
131
  }
84
132
 
85
133
  let appProps = {};
@@ -0,0 +1,12 @@
1
+ /* eslint-disable global-require */
2
+ export default function getRouteUri(route) {
3
+ const PortletContextUtil = require('PortletContextUtil');
4
+ const currentPage = PortletContextUtil.getCurrentPage();
5
+ const currentPortlet = PortletContextUtil.getCurrentPortlet();
6
+ if (currentPage && currentPortlet) {
7
+ const currentPageId = currentPage.getIdentifier().replace('_sitePage', '');
8
+ const currentPortletId = currentPortlet.getIdentifier();
9
+ return `/appresource/${currentPageId}/${currentPortletId}/${route}`;
10
+ }
11
+ return '/';
12
+ }
package/package.json CHANGED
@@ -1,13 +1,9 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Utility and rendering functions for WebApps.",
5
5
  "main": "./common/index.js",
6
6
  "author": "Soleil AB",
7
- "contributors": [
8
- "Kimmy Monassar",
9
- "Jonas Gällman"
10
- ],
11
7
  "license": "UNLICENSED",
12
8
  "private": false,
13
9
  "homepage": "https://docs.soleilit.se/03.packages/@soleil-api&app-util",
@@ -18,5 +14,6 @@
18
14
  "peerDependencies": {
19
15
  "@sitevision/api": "^1.0.10"
20
16
  },
17
+ "gitHead": "3ea46889cfbe3193420d079413e20793cb183252",
21
18
  "dependencies": {}
22
19
  }