@soleil-se/app-util 5.8.3 → 5.9.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,11 @@ 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.9.0] - 2025-03-17
11
+
12
+ - Add `idPrefix` when rendering Svelte on the server to avoid conflicts with other apps.
13
+ - Add type definitions.
14
+
10
15
  ## [5.8.3] - 2025-02-14
11
16
 
12
17
  - Update license.
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Fetches JSON data from a specified URI.
3
+ * URI:s starting with `/rest-api`, `/appresource`, `/edit-app-config` or a protocol, for example
4
+ * `https://` will be left as is. Other URI:s willbe converted to match a route in the current app
5
+ * with `getRouteUri`.
6
+ *
7
+ * @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.
16
+ */
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>;
@@ -55,6 +55,14 @@ function getWidgetOptions(options) {
55
55
  };
56
56
  }
57
57
 
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
+
58
66
  /**
59
67
  * Fetches JSON data from a specified URI.
60
68
  * URI:s starting with `/rest-api`, `/appresource`, `/edit-app-config` or a protocol, for example
@@ -62,13 +70,13 @@ function getWidgetOptions(options) {
62
70
  * with `getRouteUri`.
63
71
  *
64
72
  * @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>.
73
+ * @param {Options} options - The options for the fetch request with some extensions.
66
74
  * @param {string} options.method - The HTTP method to use in the request.
67
75
  * @param {object} options.params - The parameters to be included in the request URL.
68
76
  * @param {object} options.body - The body to include in the request.
69
77
  * @param {number} options.retries - The number of retries to attempt in case of a timeout error.
70
78
  * @param {object} options.headers - The headers to be included in the request.
71
- * @returns {Promise<object>} A promise that resolves to the JSON response.
79
+ * @returns {Promise<Object>} A promise that resolves to the JSON response.
72
80
  * @throws {Error} If the response is not successful or cannot be parsed as JSON.
73
81
  */
74
82
  export default function fetchJson(uri, options = {}) {
@@ -0,0 +1,2 @@
1
+ export { default as fetchJson } from "./fetch-json";
2
+ export { getUrlParam, getUrlParams, setUrlParams, updateUrlParams, clearUrlParams } from "./url-params";
@@ -0,0 +1 @@
1
+ export { render } from "../4";
@@ -0,0 +1,21 @@
1
+ /** @typedef {import('svelte').SvelteComponent} Component */
2
+ /**
3
+ * Renders a client side Svelte application.
4
+ * @param {Component} App Svelte app root component.
5
+ * @param {object} [settings={}] Settings object.
6
+ * @param {string} [settings.target] Target where app should be mounted.
7
+ * @param {string} [settings.props] Root component props.
8
+ * @param {boolean} [settings.hydrate=target.hasChildNodes()] Instructs Svelte to upgrade existing
9
+ * DOM (usually from server-side rendering) rather than creating new elements. By default the app
10
+ * will hydrate when the target has any child nodes.
11
+ * @param {boolean} [settings.intro=false] If true, will play transitions on initial render,
12
+ * rather than waiting for subsequent state changes.
13
+ * @return {Component} Initialized Svelte component.
14
+ */
15
+ export function render(App: Component, { target, props, intro, hydrate, }?: {
16
+ target?: string;
17
+ props?: string;
18
+ hydrate?: boolean;
19
+ intro?: boolean;
20
+ }): Component;
21
+ export type Component = import('svelte').SvelteComponent;
@@ -1,8 +1,8 @@
1
1
  import { setAppProps } from '../../../common';
2
-
2
+ /** @typedef {import('svelte').SvelteComponent} Component */
3
3
  /**
4
4
  * Renders a client side Svelte application.
5
- * @param {import('svelte').SvelteComponent} App Svelte app root component.
5
+ * @param {Component} App Svelte app root component.
6
6
  * @param {object} [settings={}] Settings object.
7
7
  * @param {string} [settings.target] Target where app should be mounted.
8
8
  * @param {string} [settings.props] Root component props.
@@ -11,7 +11,7 @@ import { setAppProps } from '../../../common';
11
11
  * will hydrate when the target has any child nodes.
12
12
  * @param {boolean} [settings.intro=false] If true, will play transitions on initial render,
13
13
  * rather than waiting for subsequent state changes.
14
- * @return {import('svelte').SvelteComponent} Initialized Svelte component.
14
+ * @return {Component} Initialized Svelte component.
15
15
  */
16
16
  export function render(App, {
17
17
  target,
@@ -0,0 +1,20 @@
1
+ /** @typedef {import('svelte').Component} Component */
2
+ /**
3
+ * Renders a client side Svelte application.
4
+ * @param {Component} App Svelte app root component.
5
+ * @param {object} [settings={}] Settings object.
6
+ * @param {string} [settings.target] Target where app should be mounted.
7
+ * @param {string} [settings.props] Root component props.
8
+ * @param {boolean} [settings.hydrate=target.hasChildNodes()] Instructs Svelte to upgrade existing
9
+ * DOM (usually from server-side rendering) rather than creating new elements. By default the app
10
+ * will hydrate when the target has any child nodes.
11
+ * @param {boolean} [settings.intro=false] If true, will play transitions on initial render,
12
+ * rather than waiting for subsequent state changes.
13
+ */
14
+ export function render(App: Component, { target, props, hydrate, intro, }?: {
15
+ target?: string;
16
+ props?: string;
17
+ hydrate?: boolean;
18
+ intro?: boolean;
19
+ }): void;
20
+ export type Component = import('svelte').Component;
@@ -3,9 +3,11 @@ import { mount as svelteMount, hydrate as svelteHydrate } from 'svelte';
3
3
 
4
4
  import { setAppProps } from '../../../common';
5
5
 
6
+ /** @typedef {import('svelte').Component} Component */
7
+
6
8
  /**
7
9
  * Renders a client side Svelte application.
8
- * @param {import('svelte').Component} App Svelte app root component.
10
+ * @param {Component} App Svelte app root component.
9
11
  * @param {object} [settings={}] Settings object.
10
12
  * @param {string} [settings.target] Target where app should be mounted.
11
13
  * @param {string} [settings.props] Root component props.
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Renders a client side Svelte 3 or 4 application.
3
+ * @deprecated Use rendering function for specific Svelte version instead.
4
+ * `import { render } from '@soleil-api/webapp-util/client/svelte/{3|4|5}';`
5
+ * @param {import('svelte').SvelteComponent} App Svelte app root component.
6
+ * @param {object} [settings={}] Settings object.
7
+ * @param {string} [settings.target] Target where app should be mounted.
8
+ * @param {string} [settings.props] Root component props.
9
+ * @param {boolean} [settings.hydrate=target.hasChildNodes()] Instructs Svelte to upgrade existing
10
+ * DOM (usually from server-side rendering) rather than creating new elements. By default the app
11
+ * will hydrate when the target has any child nodes.
12
+ * @param {boolean} [settings.intro=false] If true, will play transitions on initial render,
13
+ * rather than waiting for subsequent state changes.
14
+ * @return {import('svelte').SvelteComponent} Initialized Svelte component.
15
+ */
16
+ export function render(App: import('svelte').SvelteComponent, settings?: {
17
+ target?: string;
18
+ props?: string;
19
+ hydrate?: boolean;
20
+ intro?: boolean;
21
+ }): import('svelte').SvelteComponent;
@@ -0,0 +1,32 @@
1
+ /**
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.
6
+ */
7
+ export function setUrlParams(params: any): void;
8
+ /**
9
+ * Get query parameter from the url-field.
10
+ */
11
+ export function getUrlParam(key: any): any;
12
+ /**
13
+ * Get query parameters from the url-field.
14
+ */
15
+ export function getUrlParams(): {};
16
+ /**
17
+ * Clear query parameters in the url-field.
18
+ */
19
+ export function clearUrlParams(): void;
20
+ /**
21
+ * Updates query parameters in the url-field.
22
+ * @param {Object} params - Values to be updated.
23
+ */
24
+ export function updateUrlParams(params: any): void;
25
+ declare namespace _default {
26
+ export { getUrlParam };
27
+ export { setUrlParams };
28
+ export { getUrlParams };
29
+ export { clearUrlParams };
30
+ export { updateUrlParams };
31
+ }
32
+ export default _default;
@@ -0,0 +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;
@@ -0,0 +1 @@
1
+ export default function getRouteUri(route: any): string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Require a module natively.
3
+ * @param {string} module
4
+ * @returns any
5
+ */
6
+ export function nativeRequire(module: string): any;
package/package.json CHANGED
@@ -1,18 +1,28 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "5.8.3",
3
+ "version": "5.9.0",
4
4
  "description": "Utility functions for WebApps, RESTApps and Widgets in Sitevision.",
5
5
  "main": "./common/index.js",
6
6
  "author": "Soleil AB",
7
7
  "license": "UNLICENSED",
8
8
  "private": false,
9
+ "type": "module",
9
10
  "homepage": "https://docs.soleil.se/packages/app-util",
10
- "keywords": ["sitevision", "webapps", "restapps", "widgets", "svelte"],
11
+ "keywords": [
12
+ "sitevision",
13
+ "webapps",
14
+ "restapps",
15
+ "widgets",
16
+ "svelte"
17
+ ],
11
18
  "devDependencies": {
12
- "@sitevision/api": "^2023.9.2",
13
- "svelte": "^4.2.18"
19
+ "@sitevision/api": "^2025.1.2",
20
+ "svelte": "^5.22.6"
14
21
  },
15
22
  "peerDependencies": {
16
23
  "@sitevision/api": "*"
24
+ },
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"
17
27
  }
18
28
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Require a module natively.
3
+ * @param {string} module
4
+ * @returns any
5
+ */
6
+ export function nativeRequire(module: string): any;
package/server/index.js CHANGED
@@ -1,5 +1,10 @@
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
+ /**
4
+ * Require a module natively.
5
+ * @param {string} module
6
+ * @returns any
7
+ */
3
8
  export function nativeRequire(module) {
4
9
  if (typeof __non_webpack_require__ !== 'undefined') return __non_webpack_require__(module);
5
10
  return require(module);
@@ -0,0 +1 @@
1
+ export { render } from "../4";
@@ -0,0 +1,9 @@
1
+ /** @typedef {import('svelte').SvelteComponent} Component */
2
+ /**
3
+ * Returns HTML for a server rendered Svelte app.
4
+ * @param {Component} App Svelte component that is root of app.
5
+ * @param {object} props Props passed to root component.
6
+ * @return {string} HTML for the server rendered app.
7
+ */
8
+ export function render(App: Component, props: object): string;
9
+ export type Component = import('svelte').SvelteComponent;
@@ -1,8 +1,8 @@
1
1
  import { setAppProps } from '../../../common';
2
-
2
+ /** @typedef {import('svelte').SvelteComponent} Component */
3
3
  /**
4
4
  * Returns HTML for a server rendered Svelte app.
5
- * @param {import('svelte').SvelteComponent} App Svelte component that is root of app.
5
+ * @param {Component} App Svelte component that is root of app.
6
6
  * @param {object} props Props passed to root component.
7
7
  * @return {string} HTML for the server rendered app.
8
8
  */
@@ -0,0 +1,9 @@
1
+ /** @typedef {import('svelte').Component} Component */
2
+ /**
3
+ * Returns HTML for a server rendered Svelte app.
4
+ * @param {Component} App Svelte component that is root of app.
5
+ * @param {object} props Props passed to root component.
6
+ * @return {string} HTML for the server rendered app.
7
+ */
8
+ export function render(App: Component, props: object): string;
9
+ export type Component = import('svelte').Component;
@@ -1,15 +1,19 @@
1
1
  import { render as svelteRender } from 'svelte/server';
2
- import { setAppProps } from '../../../common';
2
+ import { appId, setAppProps } from '../../../common';
3
3
 
4
+ /** @typedef {import('svelte').Component} Component */
4
5
  /**
5
6
  * Returns HTML for a server rendered Svelte app.
6
- * @param {import('svelte').Component} App Svelte component that is root of app.
7
+ * @param {Component} App Svelte component that is root of app.
7
8
  * @param {object} props Props passed to root component.
8
9
  * @return {string} HTML for the server rendered app.
9
10
  */
10
11
  export function render(App, props) {
11
12
  setAppProps(props);
12
13
 
13
- const { body } = svelteRender(App, { props });
14
+ const { body } = svelteRender(App, {
15
+ props,
16
+ idPrefix: `app${appId}`,
17
+ });
14
18
  return body;
15
19
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Returns HTML for a server rendered Svelte 3 or 4 app.
3
+ * @deprecated Use rendering function for specific Svelte version instead.
4
+ * `import { render } from '@soleil-api/webapp-util/server/svelte/{3|4|5}';`
5
+ * @param {import('svelte').SvelteComponent} App Svelte component that is root of app.
6
+ * @param {object} props Props passed to root component.
7
+ * @return {string} HTML for the server rendered app.
8
+ */
9
+ export function render(App: import('svelte').SvelteComponent, props: object): string;