@soleil-se/app-util 5.10.1 → 5.12.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
@@ -7,6 +7,15 @@ 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.12.0] - 2025-10-09
11
+
12
+ - Add `iteratorToArray`, `nodeIteratorToArray`, `listToArray` and `setToArray` utility functions to convert these data structures into arrays.
13
+
14
+ ## [5.11.0] - 2025-09-04
15
+
16
+ - Add type-safe wrapper for `@sitevision/api/server/appData` and `@sitevision/api/server/globalAppData`.
17
+ - Adjust types for `fetchJson` and `urlParams` to work better with TypeScript.
18
+
10
19
  ## [5.10.1] - 2025-06-26
11
20
 
12
21
  - Adjust types for rendering functions.
@@ -4,20 +4,53 @@
4
4
  * `https://` will be left as is. Other URI:s willbe converted to match a route in the current app
5
5
  * with `getRouteUri`.
6
6
  *
7
+ * @template T
7
8
  * @param {string} uri - The URI to fetch JSON data from.
8
- * @param {object} options - The options for the fetch request, see <https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters>.
9
- * @param {string} options.method - The HTTP method to use in the request.
10
- * @param {object} options.params - The parameters to be included in the request URL.
11
- * @param {object} options.body - The body to include in the request.
12
- * @param {number} options.retries - The number of retries to attempt in case of a timeout error.
13
- * @param {object} options.headers - The headers to be included in the request.
14
- * @returns {Promise<object>} A promise that resolves to the JSON response.
15
- * @throws {Error} If the response is not successful or cannot be parsed as JSON.
9
+ * @param {Options} [options] - The options for the fetch request with some extensions.
10
+ * @returns {Promise<T>} A promise that resolves to the JSON response.
11
+ * @throws {FetchError} If the response is not successful or cannot be parsed as JSON.
16
12
  */
17
- export default function fetchJson(uri: string, options?: {
18
- method: string;
19
- params: object;
20
- body: object;
21
- retries: number;
22
- headers: object;
23
- }): Promise<object>;
13
+ export default function fetchJson<T>(uri: string, options?: Options): Promise<T>;
14
+ /**
15
+ * @typedef {Object} ExtensionOptions
16
+ * @property {{ [key: string]: unknown }} [params] - The parameters to be included in the request
17
+ * URL.
18
+ * @property {number} [retries=0] - The number of retries to attempt in case of a timeout error.
19
+ *
20
+ * @typedef {RequestInit & ExtensionOptions} Options
21
+ */
22
+ /**
23
+ * Custom error class for fetch-related errors with additional HTTP context.
24
+ */
25
+ export class FetchError extends Error {
26
+ /**
27
+ * @param {string} message - The error message.
28
+ * @param {Object} [options] - Error options.
29
+ * @param {number} [options.status] - HTTP status code.
30
+ * @param {boolean} [options.aborted=false] - Whether the request was aborted.
31
+ * @param {Object.<string, unknown>} [options.additionalProps] - Additional properties.
32
+ */
33
+ constructor(message: string, { status, aborted, ...additionalProps }?: {
34
+ status?: number;
35
+ aborted?: boolean;
36
+ additionalProps?: {
37
+ [x: string]: unknown;
38
+ };
39
+ });
40
+ status: number;
41
+ aborted: boolean;
42
+ }
43
+ export type ExtensionOptions = {
44
+ /**
45
+ * - The parameters to be included in the request
46
+ * URL.
47
+ */
48
+ params?: {
49
+ [key: string]: unknown;
50
+ };
51
+ /**
52
+ * - The number of retries to attempt in case of a timeout error.
53
+ */
54
+ retries?: number;
55
+ };
56
+ export type Options = RequestInit & ExtensionOptions;
@@ -1,5 +1,46 @@
1
1
  import { getRouteUri, stringifyParams } from '../../common';
2
2
 
3
+ /**
4
+ * @typedef {Object} ExtensionOptions
5
+ * @property {{ [key: string]: unknown }} [params] - The parameters to be included in the request
6
+ * URL.
7
+ * @property {number} [retries=0] - The number of retries to attempt in case of a timeout error.
8
+ *
9
+ * @typedef {RequestInit & ExtensionOptions} Options
10
+ */
11
+
12
+ /**
13
+ * Custom error class for fetch-related errors with additional HTTP context.
14
+ */
15
+ export class FetchError extends Error {
16
+ /**
17
+ * @param {string} message - The error message.
18
+ * @param {Object} [options] - Error options.
19
+ * @param {number} [options.status] - HTTP status code.
20
+ * @param {boolean} [options.aborted=false] - Whether the request was aborted.
21
+ * @param {Object.<string, unknown>} [options.additionalProps] - Additional properties.
22
+ */
23
+ constructor(message, { status, aborted = false, ...additionalProps } = {}) {
24
+ super(message);
25
+ this.name = 'FetchError';
26
+ this.status = status;
27
+ this.aborted = aborted;
28
+
29
+ // Add any additional properties from the error response
30
+ Object.entries(additionalProps).forEach(([key, value]) => {
31
+ if (key !== 'message' && key !== 'status' && key !== 'aborted') {
32
+ this[key] = value;
33
+ }
34
+ });
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Builds the URL for the request, handling different URI formats.
40
+ * @param {string} uri - The URI to convert to a full URL.
41
+ * @param {{ [key: string]: unknown }} params - Query parameters to include in the URL.
42
+ * @returns {string} The complete URL for the request.
43
+ */
3
44
  function getUrl(uri, params) {
4
45
  if (uri.startsWith('/rest-api') || uri.startsWith('/appresource') || uri.startsWith('/edit-app-config') || !uri.startsWith('/')) {
5
46
  return uri + stringifyParams(params, { addQueryPrefix: true });
@@ -7,6 +48,11 @@ function getUrl(uri, params) {
7
48
  return getRouteUri(uri, params);
8
49
  }
9
50
 
51
+ /**
52
+ * Attempts to parse response text as JSON.
53
+ * @param {Response} response - The fetch Response object.
54
+ * @returns {Promise<unknown | undefined>} The parsed JSON object or undefined if parsing fails.
55
+ */
10
56
  async function toJson(response) {
11
57
  const text = await response.text();
12
58
  try {
@@ -16,31 +62,51 @@ async function toJson(response) {
16
62
  }
17
63
  }
18
64
 
65
+ /**
66
+ * Creates a FetchError object from a failed response, including JSON error details.
67
+ * @param {Response} response - The failed fetch Response object.
68
+ * @returns {Promise<FetchError>} A FetchError with additional properties from the response JSON.
69
+ */
19
70
  async function responseError(response) {
20
71
  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;
72
+ const message = json?.message || response?.statusText;
73
+ return new FetchError(message, {
74
+ status: response.status,
75
+ aborted: false,
76
+ ...json,
24
77
  });
25
- error.status = response.status;
26
- return error;
27
78
  }
28
79
 
80
+ /**
81
+ * Handles the fetch response, throwing errors for non-OK responses and parsing JSON.
82
+ * @param {Response} response - The fetch Response object.
83
+ * @returns {Promise<unknown>} The parsed JSON response.
84
+ * @throws {FetchError} If the response is not OK or cannot be parsed as JSON.
85
+ */
29
86
  async function handleResponse(response) {
30
87
  if (!response.ok) {
31
88
  throw await responseError(response);
32
89
  }
33
90
  const json = await toJson(response);
34
91
  if (!json) {
35
- throw new Error('Response could not be parsed as JSON.');
92
+ throw new FetchError('Response could not be parsed as JSON.');
36
93
  }
37
94
  return json;
38
95
  }
39
96
 
97
+ /**
98
+ * Checks if the current context is a SiteVision widget/dashboard.
99
+ * @returns {boolean} True if running in a widget context.
100
+ */
40
101
  function isWidget() {
41
102
  return !!window?.sv?.PageContext?.dashboardId;
42
103
  }
43
104
 
105
+ /**
106
+ * Modifies request options for widget context by adding required parameters and headers.
107
+ * @param {Options} options - The original request options.
108
+ * @returns {Options} Modified options with widget-specific parameters and headers.
109
+ */
44
110
  function getWidgetOptions(options) {
45
111
  return {
46
112
  ...options,
@@ -55,29 +121,17 @@ function getWidgetOptions(options) {
55
121
  };
56
122
  }
57
123
 
58
- /**
59
- * @typedef {Object} ExtensionOptions
60
- * @property {Object<string,any>} params - The parameters to be included in the request URL.
61
- * @property {number} retries - The number of retries to attempt in case of a timeout error.
62
- *
63
- * @typedef {RequestInit & ExtensionOptions} Options
64
- */
65
-
66
124
  /**
67
125
  * Fetches JSON data from a specified URI.
68
126
  * URI:s starting with `/rest-api`, `/appresource`, `/edit-app-config` or a protocol, for example
69
127
  * `https://` will be left as is. Other URI:s willbe converted to match a route in the current app
70
128
  * with `getRouteUri`.
71
129
  *
130
+ * @template T
72
131
  * @param {string} uri - The URI to fetch JSON data from.
73
- * @param {Options} options - The options for the fetch request with some extensions.
74
- * @param {string} options.method - The HTTP method to use in the request.
75
- * @param {object} options.params - The parameters to be included in the request URL.
76
- * @param {object} options.body - The body to include in the request.
77
- * @param {number} options.retries - The number of retries to attempt in case of a timeout error.
78
- * @param {object} options.headers - The headers to be included in the request.
79
- * @returns {Promise<Object>} A promise that resolves to the JSON response.
80
- * @throws {Error} If the response is not successful or cannot be parsed as JSON.
132
+ * @param {Options} [options] - The options for the fetch request with some extensions.
133
+ * @returns {Promise<T>} A promise that resolves to the JSON response.
134
+ * @throws {FetchError} If the response is not successful or cannot be parsed as JSON.
81
135
  */
82
136
  export default function fetchJson(uri, options = {}) {
83
137
  const { params = {}, retries = 0, ...rest } = isWidget() ? getWidgetOptions(options) : options;
@@ -89,8 +143,20 @@ export default function fetchJson(uri, options = {}) {
89
143
  if (isTimeout && retries > 0) {
90
144
  return fetchJson(uri, { ...rest, params, retries: retries - 1 });
91
145
  }
92
- // eslint-disable-next-line no-param-reassign
93
- error.aborted = error.name === 'AbortError';
94
- return Promise.reject(error);
146
+
147
+ // Handle abort errors
148
+ if (error.name === 'AbortError') {
149
+ return Promise.reject(new FetchError(error.message, {
150
+ status: error.status,
151
+ aborted: true,
152
+ }));
153
+ }
154
+
155
+ // Re-throw FetchError as-is, convert other errors
156
+ if (error instanceof FetchError) {
157
+ return Promise.reject(error);
158
+ }
159
+
160
+ return Promise.reject(new FetchError(error.message, { status: error.status || 500 }));
95
161
  });
96
162
  }
package/client/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { default as fetchJson } from "./fetch-json";
2
1
  export { default as preventDefault } from "./prevent-default";
3
2
  export { getUrlParam, getUrlParams, setUrlParams, updateUrlParams, clearUrlParams } from "./url-params";
3
+ export { default as fetchJson, FetchError } from "./fetch-json";
package/client/index.js CHANGED
@@ -6,5 +6,5 @@ export {
6
6
  clearUrlParams,
7
7
  } from './url-params';
8
8
 
9
- export { default as fetchJson } from './fetch-json';
9
+ export { default as fetchJson, FetchError } from './fetch-json';
10
10
  export { default as preventDefault } from './prevent-default';
@@ -1,14 +1,15 @@
1
1
  /**
2
2
  * Set query parameters in the url-field.
3
- * @param {Object} obj - Values to be set.
4
- * @param {Object} [options] Optional options.
5
- * @param {Boolean} [options.scoped = false] If parameter keys should be scoped to current app.
3
+ * @param {{ [key: string]: unknown }} params - Values to be set.
6
4
  */
7
- export function setUrlParams(params: any): void;
5
+ export function setUrlParams(params: {
6
+ [key: string]: unknown;
7
+ }): void;
8
8
  /**
9
9
  * Get query parameter from the url-field.
10
+ * @param {string} key - The key of the parameter to get.
10
11
  */
11
- export function getUrlParam(key: any): any;
12
+ export function getUrlParam(key: string): any;
12
13
  /**
13
14
  * Get query parameters from the url-field.
14
15
  */
@@ -19,9 +20,11 @@ export function getUrlParams(): {};
19
20
  export function clearUrlParams(): void;
20
21
  /**
21
22
  * Updates query parameters in the url-field.
22
- * @param {Object} params - Values to be updated.
23
+ * @param {{ [key: string]: unknown }} params - Values to be updated.
23
24
  */
24
- export function updateUrlParams(params: any): void;
25
+ export function updateUrlParams(params: {
26
+ [key: string]: unknown;
27
+ }): void;
25
28
  declare namespace _default {
26
29
  export { getUrlParam };
27
30
  export { setUrlParams };
@@ -4,9 +4,7 @@ const isEmpty = (obj) => Object.keys(obj).length === 0 && obj.constructor === Ob
4
4
 
5
5
  /**
6
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.
7
+ * @param {{ [key: string]: unknown }} params - Values to be set.
10
8
  */
11
9
  export function setUrlParams(params) {
12
10
  const path = (isEmpty(params) ? window.location.pathname : `?${stringifyParams(params)}`);
@@ -16,6 +14,7 @@ export function setUrlParams(params) {
16
14
 
17
15
  /**
18
16
  * Get query parameter from the url-field.
17
+ * @param {string} key - The key of the parameter to get.
19
18
  */
20
19
  export function getUrlParam(key) {
21
20
  const params = parseParams(window.location.search);
@@ -40,7 +39,7 @@ export function clearUrlParams() {
40
39
 
41
40
  /**
42
41
  * Updates query parameters in the url-field.
43
- * @param {Object} params - Values to be updated.
42
+ * @param {{ [key: string]: unknown }} params - Values to be updated.
44
43
  */
45
44
  export function updateUrlParams(params) {
46
45
  setUrlParams(Object.assign(getUrlParams(), params));
package/common/index.d.ts CHANGED
@@ -1,81 +1,81 @@
1
- /**
2
- * Get a prefixed namespace unique for app.
3
- * @param {string} [prefix='app']
4
- * @return {string} - Prefixed namespace.
5
- */
6
- export function getNamespace(prefix?: string): string;
7
- /**
8
- * Generate a unique identifier with a random UUID without dashes.
9
- * @param {string} prefix Prefix for the identifier.
10
- * @returns {string} Unique identifier.
11
- */
12
- export function generateId(prefix?: string): string;
13
- /**
14
- * Stringify an object to a query string compatible with Sitevision.
15
- * @param {Object} params Object with parameters to stringify.
16
- * @param {Object} [options] Optional options.
17
- * @param {Boolean} [options.addQueryPrefix = false] If a leading ? should be added to the string.
18
- * @returns Stringified parameters.
19
- */
20
- export function stringifyParams(params?: any, { addQueryPrefix }?: {
21
- addQueryPrefix?: boolean;
22
- }): string;
23
- /**
24
- * Parse an URL or URI to an object containing its query parameters.
25
- * @param {String} url URL or URI to be parsed, must start with or contain "?".
26
- * @returns Object with parsed parameters.
27
- */
28
- export function parseParams(url?: string): {};
29
- /**
30
- * Get URI for a route, same as `getStandaloneUrl` in Sitevision router.
31
- * @param {string} route A route.
32
- * @param {object} params Query parameters.
33
- * @returns {string} URI for route.
34
- */
35
- export function getRouteUri(route?: string, params?: object): string;
36
- /**
37
- * Get URI for a view, same as `getUrl` in Sitevision router.
38
- * @deprecated Not used in WebApps 2.
39
- * @param {string} route A route.
40
- * @returns {string} URI for view.
41
- */
42
- export function getViewUri(route?: string, params?: any): string;
43
- /**
44
- * Get URI for a resource.
45
- * @param {string} resource A resource.
46
- * @returns {string} URI for a resource.
47
- */
48
- export function getResourceUri(resource?: string): string;
49
- export function setAppProps(data: any): void;
50
- /**
51
- * Get appData value or object that is passed to app when rendering.
52
- * @export
53
- * @param {string} [key] - Key for value.
54
- * @return {*|object} - Value or object.
55
- */
56
- export function getAppProps(key?: string): any | object;
57
- /**
58
- * If the current code is running on the server.
59
- * @constant {boolean}
60
- */
61
- export const isServer: any;
62
- /**
63
- * If the current code is running in the browser.
64
- * @constant {boolean}
65
- */
66
- export const isBrowser: boolean;
67
- /**
68
- * DOM friendly unique identifier for the WebApp.
69
- * @constant {string}
70
- */
71
- export const appId: string;
72
- /**
73
- * If the WebApp is running in offline mode or not.
74
- * @constant {boolean}
75
- */
76
- export const isOffline: boolean;
77
- /**
78
- * If the WebApp is running in online mode or not.
79
- * @constant {boolean}
80
- */
81
- export const isOnline: boolean;
1
+ /**
2
+ * Get a prefixed namespace unique for app.
3
+ * @param {string} [prefix='app']
4
+ * @return {string} - Prefixed namespace.
5
+ */
6
+ export function getNamespace(prefix?: string): string;
7
+ /**
8
+ * Generate a unique identifier with a random UUID without dashes.
9
+ * @param {string} prefix Prefix for the identifier.
10
+ * @returns {string} Unique identifier.
11
+ */
12
+ export function generateId(prefix?: string): string;
13
+ /**
14
+ * Stringify an object to a query string compatible with Sitevision.
15
+ * @param {Object} params Object with parameters to stringify.
16
+ * @param {Object} [options] Optional options.
17
+ * @param {Boolean} [options.addQueryPrefix = false] If a leading ? should be added to the string.
18
+ * @returns Stringified parameters.
19
+ */
20
+ export function stringifyParams(params?: any, { addQueryPrefix }?: {
21
+ addQueryPrefix?: boolean;
22
+ }): string;
23
+ /**
24
+ * Parse an URL or URI to an object containing its query parameters.
25
+ * @param {String} url URL or URI to be parsed, must start with or contain "?".
26
+ * @returns Object with parsed parameters.
27
+ */
28
+ export function parseParams(url?: string): {};
29
+ /**
30
+ * Get URI for a route, same as `getStandaloneUrl` in Sitevision router.
31
+ * @param {string} route A route.
32
+ * @param {object} params Query parameters.
33
+ * @returns {string} URI for route.
34
+ */
35
+ export function getRouteUri(route?: string, params?: object): string;
36
+ /**
37
+ * Get URI for a view, same as `getUrl` in Sitevision router.
38
+ * @deprecated Not used in WebApps 2.
39
+ * @param {string} route A route.
40
+ * @returns {string} URI for view.
41
+ */
42
+ export function getViewUri(route?: string, params?: any): string;
43
+ /**
44
+ * Get URI for a resource.
45
+ * @param {string} resource A resource.
46
+ * @returns {string} URI for a resource.
47
+ */
48
+ export function getResourceUri(resource?: string): string;
49
+ export function setAppProps(data: any): void;
50
+ /**
51
+ * Get appData value or object that is passed to app when rendering.
52
+ * @export
53
+ * @param {string} [key] - Key for value.
54
+ * @return {*|object} - Value or object.
55
+ */
56
+ export function getAppProps(key?: string): any | object;
57
+ /**
58
+ * If the current code is running on the server.
59
+ * @constant {boolean}
60
+ */
61
+ export const isServer: any;
62
+ /**
63
+ * If the current code is running in the browser.
64
+ * @constant {boolean}
65
+ */
66
+ export const isBrowser: boolean;
67
+ /**
68
+ * DOM friendly unique identifier for the WebApp.
69
+ * @constant {string}
70
+ */
71
+ export const appId: string;
72
+ /**
73
+ * If the WebApp is running in offline mode or not.
74
+ * @constant {boolean}
75
+ */
76
+ export const isOffline: boolean;
77
+ /**
78
+ * If the WebApp is running in online mode or not.
79
+ * @constant {boolean}
80
+ */
81
+ export const isOnline: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "5.10.1",
3
+ "version": "5.12.0",
4
4
  "description": "Utility functions for WebApps, RESTApps and Widgets in Sitevision.",
5
5
  "main": "./common/index.js",
6
6
  "author": "Soleil AB",
@@ -23,7 +23,7 @@
23
23
  "@sitevision/api": "*"
24
24
  },
25
25
  "scripts": {
26
- "create-type-definitions": "node ../../utils/createTypeDefinitions.js ./common/index.js;./ ./client/index.js ./client/svelte/index.js ./client/svelte/3/index.js ./client/svelte/4/index.js ./client/svelte/5/index.js ./server/index.js ./server/svelte/index.js ./server/svelte/3/index.js ./server/svelte/4/index.js ./server/svelte/5/index.js"
26
+ "create-type-definitions": "node ../../utils/createTypeDefinitions.js ./common/index.js ./client/index.js ./client/svelte/index.js ./client/svelte/3/index.js ./client/svelte/4/index.js ./client/svelte/5/index.js ./server/index.js ./server/svelte/index.js ./server/svelte/3/index.js ./server/svelte/4/index.js ./server/svelte/5/index.js ./server/app-data/index.js ./server/global-app-data/index.js"
27
27
  },
28
- "gitHead": "b5a77ddf78f3a1d2204c44bdc4faef035e7961e9"
28
+ "gitHead": "b307141c40e0116c6f2f2c3354656904d9fedb79"
29
29
  }
@@ -0,0 +1,84 @@
1
+ export default appData;
2
+ declare namespace appData {
3
+ /**
4
+ * Get a value from app data, same as get in \@sitevision/api/server/appData.
5
+ * @param {string} key - The key of app data to retrieve.
6
+ * @param {...string} [properties] - The keys of the properties to retrieve from a JCR Node.
7
+ * @returns {unknown} The value of the app data or a properties of a JCR Node.
8
+ */
9
+ function get(key: string, ...properties?: string[]): unknown;
10
+ /**
11
+ * Get a value from app data as an array, same as getArray in \@sitevision/api/server/appData.
12
+ * @param {string} key - The key of app data to retrieve.
13
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[] | string[]} An array of JCR
14
+ * Nodes for list components or value wrapped in an array for single type components.
15
+ */
16
+ function getArray(key: string): string[] | import("@sitevision/api/types/javax/jcr/Node").Node[];
17
+ /**
18
+ * Get a string value from app data.
19
+ * @param {string} key - The key of app data to retrieve.
20
+ * @returns {string | undefined} The string value or undefined if not set.
21
+ */
22
+ function getString(key: string): string | undefined;
23
+ /**
24
+ * Get a boolean value from app data.
25
+ * @param {string} key - The key of app data to retrieve.
26
+ * @returns {boolean | undefined} The boolean value or undefined if not set.
27
+ */
28
+ function getBoolean(key: string): boolean | undefined;
29
+ /**
30
+ * Get a number value from app data.
31
+ * @param {string} key - The key of app data to retrieve.
32
+ * @returns {number | undefined} The number value, undefined if not set or not a number.
33
+ */
34
+ function getNumber(key: string): number | undefined;
35
+ /**
36
+ * Get an array of nodes from app data.
37
+ * @param {string} key - The key of app data to retrieve.
38
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[]} The array of nodes or an
39
+ * empty array if not set.
40
+ */
41
+ function getNodes(key: string): import("@sitevision/api/types/javax/jcr/Node").Node[];
42
+ /**
43
+ * Get an array of strings from app data.
44
+ * @param {string} key - The key of app data to retrieve.
45
+ * @returns {string[]} The array of strings or an empty array if not set.
46
+ */
47
+ function getStrings(key: string): string[];
48
+ /**
49
+ * Get a JCR Node from app data.
50
+ * @param {string} key - The key of the property to retrieve.
51
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node | undefined} The JCR Node or
52
+ * undefined if not set.
53
+ */
54
+ function getNode(key: string): import("@sitevision/api/types/javax/jcr/Node").Node;
55
+ /**
56
+ * Get a string property from a JCR Node referenced in app data.
57
+ * @param {string} key - The key of the app data value.
58
+ * @param {string} property - The property to retrieve from the node.
59
+ * @returns {string | undefined} The string property value or undefined if not set.
60
+ */
61
+ function getStringProperty(key: string, property: string): string | undefined;
62
+ /**
63
+ * Get a string array property or a single string wrapped in an array from a JCR Node referenced
64
+ * in app data.
65
+ * @param {string} key - The key of app data to retrieve.
66
+ * @param {string} property - The property to retrieve from the node.
67
+ * @returns {string[]} The array of strings or an empty array if not set.
68
+ */
69
+ function getStringsProperty(key: string, property: string): string[];
70
+ /**
71
+ * Get a boolean property from a JCR Node referenced in app data.
72
+ * @param {string} key - The key of the app data value.
73
+ * @param {string} property - The property to retrieve from the node.
74
+ * @returns {boolean | undefined} The boolean property value or undefined if not set.
75
+ */
76
+ function getBooleanProperty(key: string, property: string): boolean | undefined;
77
+ /**
78
+ * Get a number property from a JCR Node referenced in app data.
79
+ * @param {string} key - The key of the app data value.
80
+ * @param {string} property - The property to retrieve from the node.
81
+ * @returns {number | undefined} The number property value, undefined if not set or not a number.
82
+ */
83
+ function getNumberProperty(key: string, property: string): number | undefined;
84
+ }
@@ -0,0 +1,155 @@
1
+ import appDataSv from '@sitevision/api/server/appData';
2
+ import InstanceTypeUtil from '@sitevision/api/server/InstanceTypeUtil';
3
+
4
+ /**
5
+ * Converts the value returned by appData.get to a string representation.
6
+ * @param {any} value - The value to convert.
7
+ * @returns {string | undefined} - The string representation of the value or undefined if not a
8
+ * string.
9
+ */
10
+ function toString(value) {
11
+ if (value === null || value === undefined) return undefined;
12
+ return value.toString();
13
+ }
14
+
15
+ /**
16
+ * Converts the value returned by appData.get to a boolean representation.
17
+ * @param {any} value - The value to convert.
18
+ * @returns {boolean | undefined} - The boolean representation of the value or undefined if not a
19
+ * boolean.
20
+ */
21
+ function toBoolean(value) {
22
+ if (value === undefined || value === null) return undefined;
23
+ return !!value;
24
+ }
25
+
26
+ /**
27
+ * Converts the value returned by appData.get to a number representation.
28
+ * @param {any} value - The value to convert.
29
+ * @returns {number | undefined} - The number representation of the value or undefined if not a
30
+ * number.
31
+ */
32
+ function toNumber(value) {
33
+ if (value === undefined || value === null) return undefined;
34
+ const num = +value;
35
+ return Number.isNaN(num) ? undefined : num;
36
+ }
37
+
38
+ const appData = {
39
+ /**
40
+ * Get a value from app data, same as get in \@sitevision/api/server/appData.
41
+ * @param {string} key - The key of app data to retrieve.
42
+ * @param {...string} [properties] - The keys of the properties to retrieve from a JCR Node.
43
+ * @returns {unknown} The value of the app data or a properties of a JCR Node.
44
+ */
45
+ get(key, ...properties) {
46
+ return appDataSv.get(key, ...properties);
47
+ },
48
+ /**
49
+ * Get a value from app data as an array, same as getArray in \@sitevision/api/server/appData.
50
+ * @param {string} key - The key of app data to retrieve.
51
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[] | string[]} An array of JCR
52
+ * Nodes for list components or value wrapped in an array for single type components.
53
+ */
54
+ getArray(key) {
55
+ return appDataSv.getArray(key);
56
+ },
57
+ /**
58
+ * Get a string value from app data.
59
+ * @param {string} key - The key of app data to retrieve.
60
+ * @returns {string | undefined} The string value or undefined if not set.
61
+ */
62
+ getString(key) {
63
+ return toString(appDataSv.get(key));
64
+ },
65
+ /**
66
+ * Get a boolean value from app data.
67
+ * @param {string} key - The key of app data to retrieve.
68
+ * @returns {boolean | undefined} The boolean value or undefined if not set.
69
+ */
70
+ getBoolean(key) {
71
+ return toBoolean(appDataSv.get(key));
72
+ },
73
+ /**
74
+ * Get a number value from app data.
75
+ * @param {string} key - The key of app data to retrieve.
76
+ * @returns {number | undefined} The number value, undefined if not set or not a number.
77
+ */
78
+ getNumber(key) {
79
+ return toNumber(appDataSv.get(key));
80
+ },
81
+ /**
82
+ * Get an array of nodes from app data.
83
+ * @param {string} key - The key of app data to retrieve.
84
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[]} The array of nodes or an
85
+ * empty array if not set.
86
+ */
87
+ getNodes(key) {
88
+ return appDataSv.getArray(key)?.filter((v) => InstanceTypeUtil.isNode(v)) || [];
89
+ },
90
+ /**
91
+ * Get an array of strings from app data.
92
+ * @param {string} key - The key of app data to retrieve.
93
+ * @returns {string[]} The array of strings or an empty array if not set.
94
+ */
95
+ getStrings(key) {
96
+ return appDataSv.getArray(key)
97
+ ?.map((v) => toString(v))
98
+ ?.filter((v) => v !== undefined) || [];
99
+ },
100
+ /**
101
+ * Get a JCR Node from app data.
102
+ * @param {string} key - The key of the property to retrieve.
103
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node | undefined} The JCR Node or
104
+ * undefined if not set.
105
+ */
106
+ getNode(key) {
107
+ return appDataSv.getNode(key) ?? undefined;
108
+ },
109
+ /**
110
+ * Get a string property from a JCR Node referenced in app data.
111
+ * @param {string} key - The key of the app data value.
112
+ * @param {string} property - The property to retrieve from the node.
113
+ * @returns {string | undefined} The string property value or undefined if not set.
114
+ */
115
+ getStringProperty(key, property) {
116
+ return toString(appDataSv.get(key, property));
117
+ },
118
+ /**
119
+ * Get a string array property or a single string wrapped in an array from a JCR Node referenced
120
+ * in app data.
121
+ * @param {string} key - The key of app data to retrieve.
122
+ * @param {string} property - The property to retrieve from the node.
123
+ * @returns {string[]} The array of strings or an empty array if not set.
124
+ */
125
+ getStringsProperty(key, property) {
126
+ let value = appDataSv.get(key, property);
127
+ if (Array.isArray(value)) {
128
+ return value
129
+ ?.map((v) => toString(v))
130
+ ?.filter((v) => v !== undefined) || [];
131
+ }
132
+ value = toString(value);
133
+ return value ? [value] : [];
134
+ },
135
+ /**
136
+ * Get a boolean property from a JCR Node referenced in app data.
137
+ * @param {string} key - The key of the app data value.
138
+ * @param {string} property - The property to retrieve from the node.
139
+ * @returns {boolean | undefined} The boolean property value or undefined if not set.
140
+ */
141
+ getBooleanProperty(key, property) {
142
+ return toBoolean(appDataSv.get(key, property));
143
+ },
144
+ /**
145
+ * Get a number property from a JCR Node referenced in app data.
146
+ * @param {string} key - The key of the app data value.
147
+ * @param {string} property - The property to retrieve from the node.
148
+ * @returns {number | undefined} The number property value, undefined if not set or not a number.
149
+ */
150
+ getNumberProperty(key, property) {
151
+ return toNumber(appDataSv.get(key, property));
152
+ },
153
+ };
154
+
155
+ export default appData;
@@ -0,0 +1,84 @@
1
+ export default globalAppData;
2
+ declare namespace globalAppData {
3
+ /**
4
+ * Get a value from app data, same as get in \@sitevision/api/server/appData.
5
+ * @param {string} key - The key of app data to retrieve.
6
+ * @param {...string} [properties] - The keys of the properties to retrieve from a JCR Node.
7
+ * @returns {unknown} The value of the app data or a properties of a JCR Node.
8
+ */
9
+ function get(key: string, ...properties?: string[]): unknown;
10
+ /**
11
+ * Get a value from app data as an array, same as getArray in \@sitevision/api/server/appData.
12
+ * @param {string} key - The key of app data to retrieve.
13
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[] | string[]} An array of JCR
14
+ * Nodes for list components or value wrapped in an array for single type components.
15
+ */
16
+ function getArray(key: string): string[] | import("@sitevision/api/types/javax/jcr/Node").Node[];
17
+ /**
18
+ * Get a string value from app data.
19
+ * @param {string} key - The key of app data to retrieve.
20
+ * @returns {string | undefined} The string value or undefined if not set.
21
+ */
22
+ function getString(key: string): string | undefined;
23
+ /**
24
+ * Get a boolean value from app data.
25
+ * @param {string} key - The key of app data to retrieve.
26
+ * @returns {boolean | undefined} The boolean value or undefined if not set.
27
+ */
28
+ function getBoolean(key: string): boolean | undefined;
29
+ /**
30
+ * Get a number value from app data.
31
+ * @param {string} key - The key of app data to retrieve.
32
+ * @returns {number | undefined} The number value, undefined if not set or not a number.
33
+ */
34
+ function getNumber(key: string): number | undefined;
35
+ /**
36
+ * Get an array of nodes from app data.
37
+ * @param {string} key - The key of app data to retrieve.
38
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[]} The array of nodes or an
39
+ * empty array if not set.
40
+ */
41
+ function getNodes(key: string): import("@sitevision/api/types/javax/jcr/Node").Node[];
42
+ /**
43
+ * Get an array of strings from app data.
44
+ * @param {string} key - The key of app data to retrieve.
45
+ * @returns {string[]} The array of strings or an empty array if not set.
46
+ */
47
+ function getStrings(key: string): string[];
48
+ /**
49
+ * Get a JCR Node from app data.
50
+ * @param {string} key - The key of the property to retrieve.
51
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node | undefined} The JCR Node or
52
+ * undefined if not set.
53
+ */
54
+ function getNode(key: string): import("@sitevision/api/types/javax/jcr/Node").Node;
55
+ /**
56
+ * Get a string property from a JCR Node referenced in app data.
57
+ * @param {string} key - The key of the app data value.
58
+ * @param {string} property - The property to retrieve from the node.
59
+ * @returns {string | undefined} The string property value or undefined if not set.
60
+ */
61
+ function getStringProperty(key: string, property: string): string | undefined;
62
+ /**
63
+ * Get a string array property or a single string wrapped in an array from a JCR Node referenced
64
+ * in app data.
65
+ * @param {string} key - The key of app data to retrieve.
66
+ * @param {string} property - The property to retrieve from the node.
67
+ * @returns {string[]} The array of strings or an empty array if not set.
68
+ */
69
+ function getStringsProperty(key: string, property: string): string[];
70
+ /**
71
+ * Get a boolean property from a JCR Node referenced in app data.
72
+ * @param {string} key - The key of the app data value.
73
+ * @param {string} property - The property to retrieve from the node.
74
+ * @returns {boolean | undefined} The boolean property value or undefined if not set.
75
+ */
76
+ function getBooleanProperty(key: string, property: string): boolean | undefined;
77
+ /**
78
+ * Get a number property from a JCR Node referenced in app data.
79
+ * @param {string} key - The key of the app data value.
80
+ * @param {string} property - The property to retrieve from the node.
81
+ * @returns {number | undefined} The number property value, undefined if not set or not a number.
82
+ */
83
+ function getNumberProperty(key: string, property: string): number | undefined;
84
+ }
@@ -0,0 +1,155 @@
1
+ import appDataSv from '@sitevision/api/server/globalAppData';
2
+ import InstanceTypeUtil from '@sitevision/api/server/InstanceTypeUtil';
3
+
4
+ /**
5
+ * Converts the value returned by appData.get to a string representation.
6
+ * @param {any} value - The value to convert.
7
+ * @returns {string | undefined} - The string representation of the value or undefined if not a
8
+ * string.
9
+ */
10
+ function toString(value) {
11
+ if (value === null || value === undefined) return undefined;
12
+ return value.toString();
13
+ }
14
+
15
+ /**
16
+ * Converts the value returned by appData.get to a boolean representation.
17
+ * @param {any} value - The value to convert.
18
+ * @returns {boolean | undefined} - The boolean representation of the value or undefined if not a
19
+ * boolean.
20
+ */
21
+ function toBoolean(value) {
22
+ if (value === undefined || value === null) return undefined;
23
+ return !!value;
24
+ }
25
+
26
+ /**
27
+ * Converts the value returned by appData.get to a number representation.
28
+ * @param {any} value - The value to convert.
29
+ * @returns {number | undefined} - The number representation of the value or undefined if not a
30
+ * number.
31
+ */
32
+ function toNumber(value) {
33
+ if (value === undefined || value === null) return undefined;
34
+ const num = +value;
35
+ return Number.isNaN(num) ? undefined : num;
36
+ }
37
+
38
+ const globalAppData = {
39
+ /**
40
+ * Get a value from app data, same as get in \@sitevision/api/server/appData.
41
+ * @param {string} key - The key of app data to retrieve.
42
+ * @param {...string} [properties] - The keys of the properties to retrieve from a JCR Node.
43
+ * @returns {unknown} The value of the app data or a properties of a JCR Node.
44
+ */
45
+ get(key, ...properties) {
46
+ return appDataSv.get(key, ...properties);
47
+ },
48
+ /**
49
+ * Get a value from app data as an array, same as getArray in \@sitevision/api/server/appData.
50
+ * @param {string} key - The key of app data to retrieve.
51
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[] | string[]} An array of JCR
52
+ * Nodes for list components or value wrapped in an array for single type components.
53
+ */
54
+ getArray(key) {
55
+ return appDataSv.getArray(key);
56
+ },
57
+ /**
58
+ * Get a string value from app data.
59
+ * @param {string} key - The key of app data to retrieve.
60
+ * @returns {string | undefined} The string value or undefined if not set.
61
+ */
62
+ getString(key) {
63
+ return toString(appDataSv.get(key));
64
+ },
65
+ /**
66
+ * Get a boolean value from app data.
67
+ * @param {string} key - The key of app data to retrieve.
68
+ * @returns {boolean | undefined} The boolean value or undefined if not set.
69
+ */
70
+ getBoolean(key) {
71
+ return toBoolean(appDataSv.get(key));
72
+ },
73
+ /**
74
+ * Get a number value from app data.
75
+ * @param {string} key - The key of app data to retrieve.
76
+ * @returns {number | undefined} The number value, undefined if not set or not a number.
77
+ */
78
+ getNumber(key) {
79
+ return toNumber(appDataSv.get(key));
80
+ },
81
+ /**
82
+ * Get an array of nodes from app data.
83
+ * @param {string} key - The key of app data to retrieve.
84
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node[]} The array of nodes or an
85
+ * empty array if not set.
86
+ */
87
+ getNodes(key) {
88
+ return appDataSv.getArray(key)?.filter((v) => InstanceTypeUtil.isNode(v)) || [];
89
+ },
90
+ /**
91
+ * Get an array of strings from app data.
92
+ * @param {string} key - The key of app data to retrieve.
93
+ * @returns {string[]} The array of strings or an empty array if not set.
94
+ */
95
+ getStrings(key) {
96
+ return appDataSv.getArray(key)
97
+ ?.map((v) => toString(v))
98
+ ?.filter((v) => v !== undefined) || [];
99
+ },
100
+ /**
101
+ * Get a JCR Node from app data.
102
+ * @param {string} key - The key of the property to retrieve.
103
+ * @returns {import('@sitevision/api/types/javax/jcr/Node').Node | undefined} The JCR Node or
104
+ * undefined if not set.
105
+ */
106
+ getNode(key) {
107
+ return appDataSv.getNode(key) ?? undefined;
108
+ },
109
+ /**
110
+ * Get a string property from a JCR Node referenced in app data.
111
+ * @param {string} key - The key of the app data value.
112
+ * @param {string} property - The property to retrieve from the node.
113
+ * @returns {string | undefined} The string property value or undefined if not set.
114
+ */
115
+ getStringProperty(key, property) {
116
+ return toString(appDataSv.get(key, property));
117
+ },
118
+ /**
119
+ * Get a string array property or a single string wrapped in an array from a JCR Node referenced
120
+ * in app data.
121
+ * @param {string} key - The key of app data to retrieve.
122
+ * @param {string} property - The property to retrieve from the node.
123
+ * @returns {string[]} The array of strings or an empty array if not set.
124
+ */
125
+ getStringsProperty(key, property) {
126
+ let value = appDataSv.get(key, property);
127
+ if (Array.isArray(value)) {
128
+ return value
129
+ ?.map((v) => toString(v))
130
+ ?.filter((v) => v !== undefined) || [];
131
+ }
132
+ value = toString(value);
133
+ return value ? [value] : [];
134
+ },
135
+ /**
136
+ * Get a boolean property from a JCR Node referenced in app data.
137
+ * @param {string} key - The key of the app data value.
138
+ * @param {string} property - The property to retrieve from the node.
139
+ * @returns {boolean | undefined} The boolean property value or undefined if not set.
140
+ */
141
+ getBooleanProperty(key, property) {
142
+ return toBoolean(appDataSv.get(key, property));
143
+ },
144
+ /**
145
+ * Get a number property from a JCR Node referenced in app data.
146
+ * @param {string} key - The key of the app data value.
147
+ * @param {string} property - The property to retrieve from the node.
148
+ * @returns {number | undefined} The number property value, undefined if not set or not a number.
149
+ */
150
+ getNumberProperty(key, property) {
151
+ return toNumber(appDataSv.get(key, property));
152
+ },
153
+ };
154
+
155
+ export default globalAppData;
package/server/index.d.ts CHANGED
@@ -1,6 +1,52 @@
1
- /**
2
- * Require a module natively.
3
- * @param {string} module
4
- * @returns any
5
- */
6
- export function nativeRequire(module: string): any;
1
+ /**
2
+ * Require a module natively, bypassing Webpack bundling.
3
+ * This function behaves like CommonJS require() and is used to import
4
+ * system packages in the Rhino runtime environment where Webpack bundling interferes
5
+ * with native module resolution.
6
+ *
7
+ * @param {string} module - The module identifier
8
+ * @returns {any} The exported module
9
+ * @example
10
+ * // Import a Sitevision API package
11
+ * const PortletContextUtil = nativeRequire('PortletContextUtil');
12
+ */
13
+ export function nativeRequire(module: string): any;
14
+ /**
15
+ * @typedef {import('@sitevision/api/types/javax/jcr/Node').Node} Node
16
+ * @typedef {import('@sitevision/api/types/javax/jcr/NodeIterator').NodeIterator} NodeIterator
17
+ * @typedef {import('@sitevision/api/types/java/util/Iterator').Iterator} Iterator
18
+ * @typedef {import('@sitevision/api/types/java/util/List').List} List
19
+ * @typedef {import('@sitevision/api/types/java/util/Set').Set} Set
20
+ */
21
+ /**
22
+ * Converts an Iterator to an array
23
+ * @template T
24
+ * @param {Iterator} iterator - The Iterator to convert
25
+ * @returns {T[]} Array containing all items from the iterator
26
+ */
27
+ export function iteratorToArray<T>(iterator: Iterator): T[];
28
+ /**
29
+ * Converts a NodeIterator to an array of Nodes
30
+ * @param {NodeIterator} nodeIterator - The NodeIterator to convert
31
+ * @returns {Node[]} Array containing all nodes from the iterator
32
+ */
33
+ export function nodeIteratorToArray(nodeIterator: NodeIterator): Node[];
34
+ /**
35
+ * Converts a List to an array
36
+ * @template T
37
+ * @param {List} list - The List to convert
38
+ * @returns {T[]} Array containing all items from the list
39
+ */
40
+ export function listToArray<T>(list: List): T[];
41
+ /**
42
+ * Converts a Set to an array
43
+ * @template T
44
+ * @param {Set} set - The Set to convert
45
+ * @returns {T[]} Array containing all items from the set
46
+ */
47
+ export function setToArray<T>(set: Set): T[];
48
+ export type Node = import('@sitevision/api/types/javax/jcr/Node').Node;
49
+ export type NodeIterator = import('@sitevision/api/types/javax/jcr/NodeIterator').NodeIterator;
50
+ export type Iterator = import('@sitevision/api/types/java/util/Iterator').Iterator;
51
+ export type List = import('@sitevision/api/types/java/util/List').List;
52
+ export type Set = import('@sitevision/api/types/java/util/Set').Set;
package/server/index.js CHANGED
@@ -1,12 +1,71 @@
1
1
  /* Make native requires work with @sitevision/sitevision-scripts that uses Webpack */
2
2
  /* eslint-disable camelcase, no-undef, import/no-dynamic-require, global-require */
3
3
  /**
4
- * Require a module natively.
5
- * @param {string} module
6
- * @returns any
4
+ * Require a module natively, bypassing Webpack bundling.
5
+ * This function behaves like CommonJS require() and is used to import
6
+ * system packages in the Rhino runtime environment where Webpack bundling interferes
7
+ * with native module resolution.
8
+ *
9
+ * @param {string} module - The module identifier
10
+ * @returns {any} The exported module
11
+ * @example
12
+ * // Import a Sitevision API package
13
+ * const PortletContextUtil = nativeRequire('PortletContextUtil');
7
14
  */
8
15
  export function nativeRequire(module) {
9
16
  if (typeof __non_webpack_require__ !== 'undefined') return __non_webpack_require__(module);
10
17
  return require(module);
11
18
  }
12
19
  /* eslint-enable camelcase, no-undef, import/no-dynamic-require, global-require */
20
+
21
+ /**
22
+ * @typedef {import('@sitevision/api/types/javax/jcr/Node').Node} Node
23
+ * @typedef {import('@sitevision/api/types/javax/jcr/NodeIterator').NodeIterator} NodeIterator
24
+ * @typedef {import('@sitevision/api/types/java/util/Iterator').Iterator} Iterator
25
+ * @typedef {import('@sitevision/api/types/java/util/List').List} List
26
+ * @typedef {import('@sitevision/api/types/java/util/Set').Set} Set
27
+ */
28
+
29
+ /**
30
+ * Converts an Iterator to an array
31
+ * @template T
32
+ * @param {Iterator} iterator - The Iterator to convert
33
+ * @returns {T[]} Array containing all items from the iterator
34
+ */
35
+ export function iteratorToArray(iterator) {
36
+ const array = [];
37
+ if (!iterator) return array;
38
+ while (iterator?.hasNext()) {
39
+ array.push(iterator?.next());
40
+ }
41
+ return array;
42
+ }
43
+
44
+ /**
45
+ * Converts a NodeIterator to an array of Nodes
46
+ * @param {NodeIterator} nodeIterator - The NodeIterator to convert
47
+ * @returns {Node[]} Array containing all nodes from the iterator
48
+ */
49
+ export function nodeIteratorToArray(nodeIterator) {
50
+ return iteratorToArray(nodeIterator);
51
+ }
52
+
53
+ /**
54
+ * Converts a List to an array
55
+ * @template T
56
+ * @param {List} list - The List to convert
57
+ * @returns {T[]} Array containing all items from the list
58
+ */
59
+ export function listToArray(list) {
60
+ return iteratorToArray(list?.iterator());
61
+ }
62
+
63
+ /**
64
+ * Converts a Set to an array
65
+ * @template T
66
+ * @param {Set} set - The Set to convert
67
+ * @returns {T[]} Array containing all items from the set
68
+ */
69
+ export function setToArray(set) {
70
+ return iteratorToArray(set?.iterator());
71
+ }