Hoppa till innehåll

appData

Since 5.11.0

A type-safe wrapper for @sitevision/api/server/appData that provides convenient methods for retrieving and converting app data values with proper type handling.

All methods in will return undefined or empty arrays instead of inconsistent values when values are missing or cannot be converted to the expected type.

Get a string value from app data.

ParamTypeDescription
keystringThe key of app data to retrieve.

Returns: string | undefined - The string value or undefined if not set.

import appData from '@soleil-se/app-util/server/app-data';
const name = appData.getString('name');
console.log(`Hello, ${name}!`);
// Default values are set with nullish coalescing (??)
const name = appData.getString('name') ?? 'Default name';

Get a boolean value from app data.

ParamTypeDescription
keystringThe key of app data to retrieve.

Returns: boolean | undefined - The boolean value or undefined if not set.

import appData from '@soleil-se/app-util/server/app-data';
const isEnabled = appData.getBoolean('isEnabled');
if (isEnabled) {
// Something is enabled
}
// Default values are set with nullish coalescing (??)
const isEnabled = appData.getBoolean('isEnabled') ?? false;

Get a number value from app data.

ParamTypeDescription
keystringThe key of app data to retrieve.

Returns: number | undefined - The number value, undefined if not set or not a valid number.

import appData from '@soleil-se/app-util/server/app-data';
const maxItems = appData.getNumber('maxItems');
console.log(`Maximum items: ${maxItems}`);
// Default values are set with nullish coalescing (??)
const maxItems = appData.getNumber('maxItems') ?? 10;

Get an array of JCR Nodes from app data, filtering out non-node values.

ParamTypeDescription
keystringThe key of app data to retrieve.

Returns: Node[] - The array of nodes or an empty array if not set.

import appData from '@soleil-se/app-util/server/app-data';
const contentNodes = appData.getNodes('nodes');
contentNodes.forEach(node => {
// Process each node
console.log(node.getName());
});

Get an array of strings from app data.

ParamTypeDescription
keystringThe key of app data to retrieve.

Returns: string[] - The array of strings or an empty array if not set.

import appData from '@soleil-se/app-util/server/app-data';
const tags = appData.getStrings('tags');
tags.forEach(tag => {
console.log(`Tag: ${tag}`);
});

Get a single JCR Node from app data.

ParamTypeDescription
keystringThe key of the property to retrieve.

Returns: Node | undefined - The JCR Node or undefined if not set.

import appData from '@soleil-se/app-util/server/app-data';
const node = appData.getNode('node');
console.log(node?.getIdentifier());
// Default values are set with nullish coalescing (??)
const node = appData.getNode('node') ?? defaultNode;

Get a string property from a JCR Node referenced in app data.

ParamTypeDescription
keystringThe key of the app data value.
propertystringThe property to retrieve from the node.

Returns: string | undefined - The string property value or undefined if not set.

import appData from '@soleil-se/app-util/server/app-data';
const displayName = appData.getStringProperty('node', 'displayName');
console.log(`Page display name: ${displayName}`);
// Default values are set with nullish coalescing (??)
const displayName = appData.getStringProperty('node', 'displayName') ?? 'Default Display Name';

Get a string array property or a single string wrapped in an array from a JCR Node referenced in app data.

ParamTypeDescription
keystringThe key of the app data value.
propertystringThe property to retrieve from the node.

Returns: string[] - The array of strings or an empty array if not set.

import appData from '@soleil-se/app-util/server/app-data';
const multipleValueMetadata = appData.getStringsProperty('node', 'multipleValueMetadata');
console.log(`Multiple value metadata: ${multipleValueMetadata.join(', ')}`);

Get a boolean property from a JCR Node referenced in app data.

ParamTypeDescription
keystringThe key of the app data value.
propertystringThe property to retrieve from the node.

Returns: boolean | undefined - The boolean property value or undefined if not set.

import appData from '@soleil-se/app-util/server/app-data';
const isPublished = appData.getBooleanProperty('node', 'published');
if (isPublished) {
// Content is published
}
// Default values are set with nullish coalescing (??)
const isPublished = appData.getBooleanProperty('node', 'published') ?? false;

Get a number property from a JCR Node referenced in app data.

ParamTypeDescription
keystringThe key of the app data value.
propertystringThe property to retrieve from the node.

Returns: number | undefined - The number property value, undefined if not set or not a valid number.

import appData from '@soleil-se/app-util/server/app-data';
const publishDateTimestamp = appData.getNumberProperty('node', 'publishDate');
console.log(`Publish date timestamp: ${publishDateTimestamp}`);
// Default values are set with nullish coalescing (??)
const publishDate = appData.getNumberProperty('node', 'publishDate') ?? Date.now();

Get a raw value from app data.
This is identical to the get method in @sitevision/api/server/appData.

ParamTypeDescription
keystringThe key of app data to retrieve.
…propertiesstring[]The keys of the properties to retrieve from a JCR Node.

Returns: unknown - The value of the app data or properties of a JCR Node.

import appData from '@soleil-se/app-util/server/app-data';
const value = appData.get('value');
const displayName = appData.get('node', 'displayName');
// Default values are set with nullish coalescing (??)
const value = appData.get('value') ?? 'Default value';

Get a value from app data as an array.
This is identical to the getArray method in @sitevision/api/server/appData.

ParamTypeDescription
keystringThe key of app data to retrieve.

Returns: Node[] | string[] - An array of JCR Nodes for list components or value wrapped in an array for single type components.

import appData from '@soleil-se/app-util/server/app-data';
const nodes = appData.getArray('nodes');

Default values can be set using the nullish coalescing operator (??).

import appData from '@soleil-se/app-util/server/app-data';
const title = appData.getString('title') ?? 'Default Title';
console.log(`Title: ${title}`);