# globalAppData

Since 5.11.0

A type-safe wrapper for `@sitevision/api/server/globalAppData` 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.

## getString(key)

Get a string value from app data.

| Param | Type     | Description                      |
| ----- | -------- | -------------------------------- |
| key   | `string` | The key of app data to retrieve. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const name = globalAppData.getString('name');
console.log(`Hello, ${name}!`);


// Default values are set with nullish coalescing (??)
const name = globalAppData.getString('name') ?? 'Default name';
```

## getBoolean(key)

Get a boolean value from app data.

| Param | Type     | Description                      |
| ----- | -------- | -------------------------------- |
| key   | `string` | The key of app data to retrieve. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const isEnabled = globalAppData.getBoolean('isEnabled');
if (isEnabled) {
  // Something is enabled
}


// Default values are set with nullish coalescing (??)
const isEnabled = globalAppData.getBoolean('isEnabled') ?? false;
```

## getNumber(key)

Get a number value from app data.

| Param | Type     | Description                      |
| ----- | -------- | -------------------------------- |
| key   | `string` | The key of app data to retrieve. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const maxItems = globalAppData.getNumber('maxItems');
console.log(`Maximum items: ${maxItems}`);


// Default values are set with nullish coalescing (??)
const maxItems = globalAppData.getNumber('maxItems') ?? 10;
```

## getNodeArray(key)

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

| Param | Type     | Description                      |
| ----- | -------- | -------------------------------- |
| key   | `string` | The key of app data to retrieve. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const contentNodes = globalAppData.getNodes('nodes');
contentNodes.forEach(node => {
  // Process each node
  console.log(node.getName());
});
```

## getStringArray(key)

Get an array of strings from app data.

| Param | Type     | Description                      |
| ----- | -------- | -------------------------------- |
| key   | `string` | The key of app data to retrieve. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const tags = globalAppData.getStrings('tags');
tags.forEach(tag => {
  console.log(`Tag: ${tag}`);
});
```

## getNode(key)

Get a single JCR Node from app data.

| Param | Type     | Description                          |
| ----- | -------- | ------------------------------------ |
| key   | `string` | The key of the property to retrieve. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const node = globalAppData.getNode('node');
console.log(node?.getIdentifier());


// Default values are set with nullish coalescing (??)
const node = globalAppData.getNode('node') ?? defaultNode;
```

## getStringProperty(key, property)

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

| Param    | Type     | Description                             |
| -------- | -------- | --------------------------------------- |
| key      | `string` | The key of the app data value.          |
| property | `string` | The property to retrieve from the node. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const displayName = globalAppData.getStringProperty('node', 'displayName');
console.log(`Page display name: ${displayName}`);


// Default values are set with nullish coalescing (??)
const displayName = globalAppData.getStringProperty('node', 'displayName') ?? 'Default Display Name';
```

## getStringArrayProperty(key, property)

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

| Param    | Type     | Description                             |
| -------- | -------- | --------------------------------------- |
| key      | `string` | The key of the app data value.          |
| property | `string` | The property to retrieve from the node. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const multipleValueMetadata = globalAppData.getStringArrayProperty('node', 'multipleValueMetadata');
console.log(`Multiple value metadata: ${multipleValueMetadata.join(', ')}`);
```

## getBooleanProperty(key, property)

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

| Param    | Type     | Description                             |
| -------- | -------- | --------------------------------------- |
| key      | `string` | The key of the app data value.          |
| property | `string` | The property to retrieve from the node. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const isPublished = globalAppData.getBooleanProperty('node', 'published');
if (isPublished) {
  // Content is published
}


// Default values are set with nullish coalescing (??)
const isPublished = globalAppData.getBooleanProperty('node', 'published') ?? false;
```

## getNumberProperty(key, property)

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

| Param    | Type     | Description                             |
| -------- | -------- | --------------------------------------- |
| key      | `string` | The key of the app data value.          |
| property | `string` | The property to retrieve from the node. |

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const publishDateTimestamp = globalAppData.getNumberProperty('node', 'publishDate');
console.log(`Publish date timestamp: ${publishDateTimestamp}`);


// Default values are set with nullish coalescing (??)
const publishDate = globalAppData.getNumberProperty('node', 'publishDate') ?? Date.now();
```

## get(key, …properties)

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

| Param       | Type       | Description                                             |
| ----------- | ---------- | ------------------------------------------------------- |
| key         | `string`   | The key of app data to retrieve.                        |
| …properties | `string[]` | 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.

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const value = globalAppData.get('value');
const displayName = globalAppData.get('node', 'displayName');


// Default values are set with nullish coalescing (??)
const value = appData.get('value') ?? 'Default value';
```

## getArray(key)

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

| Param | Type     | Description                      |
| ----- | -------- | -------------------------------- |
| key   | `string` | The 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.

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const nodes = globalAppData.getArray('nodes');
```

## Default values

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

```js
import globalAppData from '@soleil-se/app-util/server/global-app-data';


const title = globalAppData.getString('title') ?? 'Default Title';
console.log(`Title: ${title}`);
```