@soleil-se/app-util 5.2.2 → 5.3.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,29 +5,25 @@ 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.2.2] - 2023-01-31
8
+ ## [5.3.0] - 2023-07-06
9
+
10
+ - Added helper functions for managing parameters in the URL-field.
9
11
 
10
- ### Changed
12
+ ## [5.2.2] - 2023-01-31
11
13
 
12
14
  - Fix `parseParams` export.
13
15
 
14
16
  ## [5.2.1] - 2023-01-31
15
17
 
16
- ### Changed
17
-
18
18
  - Fix homepage url.
19
19
  - More lenient with peerDependency `@sitevision/api`.
20
20
 
21
21
  ## [5.2.0] - 2023-01-31
22
22
 
23
- ### Changed
24
-
25
23
  - New function `parseParams` to parse query parameters from an URL, URI or query string.
26
24
 
27
25
  ## [5.1.0] - 2022-05-27
28
26
 
29
- ### Changed
30
-
31
27
  - New function `stringifyParams` to stringify query parameters to a Sitevision compatible format.
32
28
  - Possible to pass a parameters object to `getRouteUri`.
33
29
  - All common constants and functions are now usable in hooks.
@@ -35,74 +31,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
35
31
 
36
32
  ## [5.0.0] - 2022-02-08
37
33
 
38
- ### Changed
39
-
40
34
  - Using WebApps 2 for rendering apps.
41
-
42
- ### Removed
43
-
44
- - `getAppData`, use `getAppProps` instead.
45
- - `server/render`, use `res.agnosticRender` for framework agnostic rendering.
46
- - `server/renderTemplate`, used for rendering underscore templates.
35
+ - Removed `getAppData`, use `getAppProps` instead.
36
+ - Removed `server/render`, use `res.agnosticRender` for framework agnostic rendering.
37
+ - Removed `server/renderTemplate`, used for rendering underscore templates.
47
38
 
48
39
  ## [4.2.1] - 2021-09-06
49
40
 
50
- ### Fixed
51
-
52
41
  - Underscore is misspelled in `renderTemplate`.
53
42
 
54
43
  ## [4.2.0] - 2021-08-20
55
44
 
56
- ### Added
57
-
58
- - `getAppProps` for getting props passed to the app when rendering.
59
-
60
- ### Changed
61
-
45
+ - Added `getAppProps` for getting props passed to the app when rendering.
62
46
  - `getAppData` deprecated due to confusion, use `getAppProps` instead.
63
47
 
64
48
  ## [4.1.4] - 2021-05-25
65
49
 
66
- ### Fixed
67
-
68
50
  - Return empty object instead of undefined when app data isn't set.
69
-
70
- ### Removed
71
-
72
51
  - Remove `vue` as optional dependency.
73
52
 
74
53
  ## [4.1.3] - 2021-03-31
75
54
 
76
- ### Fixed
77
-
78
55
  - Consistent rendering when using `serverServer` and `render` for Svelte.
79
-
80
- ### Added
81
-
82
56
  - Docs for Svelte `renderServer` function.
83
57
 
84
58
  ## [4.1.2] - 2021-02-24
85
59
 
86
- ### Fixed
87
-
88
60
  - Escape tags when parsing data to prevent closure of script tags.
89
61
 
90
62
  ## [4.1.1] - 2021-02-17
91
63
 
92
- ### Fixed
93
-
94
64
  - Validation error for `id` on script tags, even though it's allowed.
95
65
 
96
66
  ## [4.1.0] - 2021-02-17
97
67
 
98
- ### Changed
99
-
100
68
  - Put app data and metadata in script elements instead of data attributes.
101
69
 
102
70
  ## [4.0.1] - 2021-01-28
103
71
 
104
- ### Fixed
105
-
106
72
  - Crashes when no data is passed to app and then trying to call `getAppData`.
107
73
 
108
74
  ## [4.0.0] - 2021-01-27
@@ -110,17 +76,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
110
76
  A much needed major overhaul of the package.
111
77
  See [MIGRATION](./MIGRATION.md).
112
78
 
113
- ### Changed
114
-
115
79
  - Major refactoring of package.
116
80
  - Exports for render functions are moved.
117
81
  - All documented constants and functions from base import (`@soleil-api/webapp-util`) now works both in a server and client context.
118
82
  - Settings for selector is moved from server to client rendering functions.
119
-
120
- ### Removed
121
-
122
- - `noScript` option in `render` (formerly `renderApp`) has been removed. If a no script message is needed use the `html` option wrapped in `<noscript>`.
123
- - `@soleil-api/webapp-util/app-data` has been removed, use [getAppData](./README.md#getAppData) from `@soleil-api/webapp-util`.
83
+ - Removed `noScript` option in `render` (formerly `renderApp`) has been removed. If a no script message is needed use the `html` option wrapped in `<noscript>`.
84
+ - Removed `@soleil-api/webapp-util/app-data` has been removed, use [getAppData](./README.md#getAppData) from `@soleil-api/webapp-util`.
124
85
 
125
86
  ## [3.0.3] - 2020-11-03
126
87
 
@@ -132,86 +93,56 @@ See [MIGRATION](./MIGRATION.md).
132
93
 
133
94
  ## [3.0.1] - 2020-09-28
134
95
 
135
- ### Added
136
-
137
96
  - Homepage link
138
97
 
139
98
  ## [3.0.0] - 2020-09-28
140
99
 
141
- ### Added
142
-
143
100
  - Svelte support.
144
101
  - New exports:
145
102
  - `uniqueId` - A unique id for the app instance.
146
103
 
147
104
  ## [2.4.1] - 2020-09-01
148
105
 
149
- ### Fixed
150
-
151
106
  - App data with single quotes crashes app in edit mode, use double quotes instead.
152
107
 
153
108
  ## [2.4.0] - 2020-05-26
154
109
 
155
- ### Fixed
156
-
157
110
  - `getViewUri` returns correct URI when in offline mode and in the Addons view.
158
111
 
159
112
  ## [2.3.0] - 2020-05-22
160
113
 
161
- ### Added
162
-
163
114
  - `renderApp` now accepts `req` as a setting to opimize script loading when multiple instances of the app exists. If `req` is present the script will only be loaded once in all browsers except IE11 that needs a unique script src for the currentScript polyfill to work.
164
-
165
- ### Fixed
166
-
167
115
  - `noScript` in `renderApp` was not wrapped in a `<noscript>` tag.
168
116
 
169
117
  ## [2.2.0] - 2020-05-20
170
118
 
171
- ### Added
172
-
173
119
  - `renderTemplate` function is now available inside templates rendered by the function.
174
120
 
175
121
  ## [2.1.4] - 2020-04-07
176
122
 
177
- ### Fixed
178
-
179
123
  - `getViewUri` had incorrect portlet ID.
180
124
 
181
125
  ## [2.1.3] - 2020-02-25
182
126
 
183
- ### Fixed
184
-
185
127
  - Add timestamp to script url in edit mode as well.
186
128
 
187
129
  ## [2.1.2] - 2020-02-21
188
130
 
189
- ### Fixed
190
-
191
131
  - Wrapped `renderApp` script in svDocReady in edit mode to be sure it executes when other bundles are loaded.
192
132
 
193
133
  ## [2.1.1] - 2020-02-14
194
134
 
195
- ### Changed
196
-
197
135
  - Updated readme, heading levels and examples.
198
136
 
199
137
  ## [2.1.0] - 2020-02-14
200
138
 
201
- ### Added
202
-
203
139
  - App data import in client, `@soleil-api/webapp-util/app-data`.
204
140
  - `getViewUri` to get the URI that also renders the page.
205
141
  - `isOnline` to see if the app is online.
206
-
207
- ### Changed
208
-
209
142
  - `vue` is now an optional depedency.
210
143
 
211
144
  ## [2.0.0] - 2020-02-12
212
145
 
213
- ### Changed
214
-
215
146
  - Now using `currentScript` to reference the script element the app is running in to pass data when using `renderApp`.
216
147
  - `renderApp` is no longer using appName.
217
148
  - `render` is now called directly in `./app_src/client/index.js`.
package/README.md CHANGED
@@ -52,7 +52,7 @@ Integrity check failed for "@soleil-se/webapp-util" (computed integrity doesn...
52
52
  Migrating from version 4?
53
53
  See [MIGRATION](./MIGRATION.md).
54
54
 
55
- ## API
55
+ ## Common
56
56
 
57
57
  All imports from base package are available both on the server and client.
58
58
 
@@ -210,6 +210,78 @@ const params = parseParams('?foo=bar&arr[]=1&arr[]=2');
210
210
  // { foo: 'bar', arr: [1, 2] }
211
211
  ```
212
212
 
213
+ ## Client
214
+
215
+ Following API:s are available in a client context.
216
+
217
+ ### URL Parameters
218
+
219
+ Helper functions setting, getting, updating and clearing query parameters in the URL-field.
220
+
221
+ #### getUrlParams() ⇒ `Object`
222
+
223
+ Get parameters from the URL-field.
224
+
225
+ **Returns**: `Object` - Parameters in URL-field.
226
+
227
+ ```js
228
+ import { getUrlParams } from '@soleil-se/webapp-util/client';
229
+
230
+ const params = getUrlParams();
231
+ ```
232
+
233
+ #### getUrlParam() ⇒ `String|Array[String]`
234
+
235
+ Get single parameter from the URL-field.
236
+
237
+ **Returns**: `String|Array[String]` - Parameter value.
238
+
239
+ ```js
240
+ import { getUrlParam } from '@soleil-se/webapp-util/client';
241
+
242
+ const param = getUrlParam('name');
243
+ ```
244
+
245
+ #### setUrlParams(params)
246
+
247
+ Set parameters in the URL-field, overwriting other parameters.
248
+
249
+ | Param | Type | Default | Description |
250
+ | --- | --- | --- | --- |
251
+ | params | `Object` | | Object with parameters to set in the URL-field. |
252
+
253
+ ```js
254
+ import { setUrlParams } from '@soleil-se/webapp-util/client';
255
+
256
+ setUrlParams({ foo: 'bar', arr: [1, 2] });
257
+ ```
258
+
259
+ #### updateUrlParams(params)
260
+
261
+ Update parameters in the URL-field, merging with existing parameters
262
+
263
+ **Returns**: `String` - Stringified parameters.
264
+
265
+ | Param | Type | Default | Description |
266
+ | --- | --- | --- | --- |
267
+ | params | `Object` | | Object with parameters to add to the URL-field. |
268
+
269
+ ```js
270
+ import { updateUrlParams } from '@soleil-se/webapp-util/client';
271
+
272
+ updateUrlParams({ foo: 'bar', arr: [1, 2] });
273
+ ```
274
+
275
+ #### clearUrlParams()
276
+
277
+ Clear parameters in the URL-field
278
+
279
+ ```js
280
+ import { clearUrlParams } from '@soleil-se/webapp-util/client';
281
+
282
+ clearUrlParams();
283
+ ```
284
+
213
285
  ## Rendering
214
286
 
215
287
  * [Svelte](./docs/1.svelte.md)
@@ -0,0 +1,7 @@
1
+ export {
2
+ getUrlParam,
3
+ getUrlParams,
4
+ setUrlParams,
5
+ updateUrlParams,
6
+ clearUrlParams,
7
+ } from './url-params';
@@ -0,0 +1,52 @@
1
+ import { stringifyParams, parseParams } from '../../common';
2
+
3
+ const isEmpty = (obj) => Object.keys(obj).length === 0 && obj.constructor === Object;
4
+
5
+ /**
6
+ * Set query parameters in the url-field.
7
+ * @param {Object} obj - Values to be set.
8
+ * @param {Object} [options] Optional options.
9
+ * @param {Boolean} [options.scoped = false] If parameter keys should be scoped to current app.
10
+ */
11
+ export function setUrlParams(params) {
12
+ window.history.replaceState({}, '', (isEmpty(params) ? window.location.pathname : `?${stringifyParams(params)}`));
13
+ }
14
+
15
+ /**
16
+ * Get query parameter from the url-field.
17
+ */
18
+ export function getUrlParam(key) {
19
+ const params = parseParams(window.location.search.substring(1));
20
+ return params[key];
21
+ }
22
+
23
+ /**
24
+ * Get query parameters from the url-field.
25
+ */
26
+ export function getUrlParams() {
27
+ const params = parseParams(window.location.search.substring(1));
28
+ return params;
29
+ }
30
+
31
+ /**
32
+ * Clear query parameters in the url-field.
33
+ */
34
+ export function clearUrlParams() {
35
+ window.history.replaceState({}, '', window.location.pathname);
36
+ }
37
+
38
+ /**
39
+ * Updates query parameters in the url-field.
40
+ * @param {Object} params - Values to be updated.
41
+ */
42
+ export function updateUrlParams(params) {
43
+ setUrlParams(Object.assign(getUrlParams(), params));
44
+ }
45
+
46
+ export default {
47
+ getUrlParam,
48
+ setUrlParams,
49
+ getUrlParams,
50
+ clearUrlParams,
51
+ updateUrlParams,
52
+ };
package/common/index.js CHANGED
@@ -1,4 +1,3 @@
1
- /* global document console */
2
1
  /* eslint-disable global-require */
3
2
  import router from '@sitevision/api/common/router';
4
3
  import app from '@sitevision/api/common/app';
@@ -116,7 +115,7 @@ export function parseParams(url = '') {
116
115
  * @param {object} params Query parameters.
117
116
  * @returns {string} URI for route.
118
117
  */
119
- export function getRouteUri(route = '', params) {
118
+ export function getRouteUri(route = '', params = undefined) {
120
119
  const path = route.replace(leadingSlashes, '');
121
120
  const qs = stringifyParams(params, { addQueryPrefix: true });
122
121
  if (router?.getStandaloneUrl) {
@@ -133,13 +132,13 @@ export function getRouteUri(route = '', params) {
133
132
  * @param {string} route A route.
134
133
  * @returns {string} URI for view.
135
134
  */
136
- export function getViewUri(route = '', query) {
135
+ export function getViewUri(route = '', params = undefined) {
137
136
  if (!router?.getUrl) {
138
137
  console.warn('[@soleil-api/webapp-util] getViewUri requires router.getUrl support.');
139
138
  return undefined;
140
139
  }
141
140
 
142
- return router.getUrl(route, query);
141
+ return router.getUrl(route, params);
143
142
  }
144
143
 
145
144
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "5.2.2",
3
+ "version": "5.3.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": "53b207511538040495c560637273434daff093fa",
17
+ "gitHead": "1928f2e710e25b16b0e3a792bb8d6b9254d72084",
18
18
  "dependencies": {}
19
19
  }
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- extends: '@soleil/eslint-config-sitevision/client',
3
- root: true,
4
- };