> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Host data

Read the data your native app passes in, as signals, and transform it before your app sees it. `import { LynxInitData, LynxGlobalData, registerDataProcessors, type InitData, type GlobalData, type InitDataRaw, type DataProcessorDefinition } from '@blotch/angular-lynx';`

The [host app](/guide/glossary.md#host-app) (the native iOS or Android app that runs your bundle) sends two kinds of data:

- **[InitData](/guide/glossary.md#initdata):** data for this one page, passed when the host opens it.
- **[GlobalData](/guide/glossary.md#globaldata):** app-wide values, such as the theme or locale, shared by every page.

## Usage

```typescript title="src/lynx-data.d.ts"
// This export makes the file a module, so the block below adds to
// @blotch/angular-lynx's types instead of replacing them.
export {};

declare module '@blotch/angular-lynx' {
  interface InitData {
    userId: string;
  }
  interface GlobalData {
    theme: 'Light' | 'Dark';
  }
}
```

```typescript title="src/app/profile.ts"
import { Component, computed, inject } from '@angular/core';
import { LYNX_ELEMENTS, LynxInitData } from '@blotch/angular-lynx';

@Component({
  selector: 'app-profile',
  imports: [LYNX_ELEMENTS],
  template: `<text>User {{ userId() }}</text>`,
})
export class Profile {
  readonly #initData = inject(LynxInitData);
  // initData() is {} when the host sends nothing, as in Lynx Explorer.
  readonly userId = computed(() => this.#initData.initData().userId ?? 'guest');
}
```

## API

### `LynxInitData`

```typescript
@Injectable({ providedIn: 'root' })
class LynxInitData {}
```

The page's InitData as a signal.

### `LynxInitData.initData`

```typescript
readonly initData: WritableSignal<InitData>;
```

Starts with the InitData the host passed when it opened the page, after your [default data processor](#registerdataprocessors) has run on it.

- **It's `{}`** when the host passes none, and outside Lynx.
- **AngularLynx also keeps the value** as `lynx.__initData`.
- **When the host sends new data,** it's merged into the current value.
- **When the host resets the data,** the new data replaces it.

### `LynxGlobalData`

```typescript
@Injectable({ providedIn: 'root' })
class LynxGlobalData {}
```

The app-wide GlobalData as a signal. `LynxTheme`, `LynxLocale` and `LynxSafeArea` read their values from it.

### `LynxGlobalData.globalData`

```typescript
readonly globalData: WritableSignal<GlobalData>;
```

Starts with the global values at launch, or `{}` when the host sets none. When the host changes them, the new values are merged into the current ones.

### `registerDataProcessors()`

```typescript
function registerDataProcessors(definition: DataProcessorDefinition): void;
```

Registers functions that transform the host's InitData before your app receives it. Call it in `main.ts` before `bootstrapApplication()`.

```typescript title="src/main.ts"
import {
  bootstrapApplication,
  registerDataProcessors,
} from '@blotch/angular-lynx';
import { App } from './app/app';
import { appConfig } from './app/app.config';

registerDataProcessors({
  defaultDataProcessor: (raw) => ({ ...raw, userId: String(raw['user_id']) }),
});

bootstrapApplication(App, appConfig);
```

- **Default processor:** used when the host sends data without naming a processor.
- **Named processors:** used when the host names one. An unknown name passes the data through unchanged.
- **A processor that throws** gives your app `{}` instead of crashing it.
- **Calling it again** replaces the earlier registration.

## Types

### `InitDataRaw`

```typescript
interface InitDataRaw extends Record<string, unknown> {}
```

The InitData exactly as the host sends it. A processor's input. Augment it to type the raw fields.

### `InitData`

```typescript
interface InitData extends Record<string, unknown> {}
```

The InitData your app reads, after processing. Augment it, as in [Usage](#usage), to type `initData()`.

Put every augmentation in a file that contains `export {};`. Without it, the `declare module` block replaces the package's types instead of adding to them.

### `GlobalData`

```typescript
interface GlobalData extends Record<string, unknown> {}
```

The GlobalData your app reads. Augment it to type `globalData()`.

### `DataProcessorDefinition`

```typescript
type DataProcessorDefinition = {
  defaultDataProcessor?: (rawInitData: InitDataRaw) => InitData;
  dataProcessors?: Record<string, (...args: any[]) => any>;
};
```

| Field                  | Description                                                       |
| ---------------------- | ----------------------------------------------------------------- |
| `defaultDataProcessor` | Transforms InitData when the host names no processor.             |
| `dataProcessors`       | Processors the host can call by name. Each receives the raw data. |

## Related

- [Pass data from your native app](/guide/embedding/host-data.md): how the host sends data, step by step.
- [Embed in an existing native app](/guide/embedding/index.md)
- [Theme](/api/angular-lynx/device/theme.md), [Locale](/api/angular-lynx/device/locale.md) and [Safe area](/api/angular-lynx/device/safe-area.md): built on `LynxGlobalData`.
