Lime Web Components API Documentation - v7.4.0
    Preparing search index...

    Interface UserPreferencesRepository

    Service for managing structured user preferences defined by a schema

    The UserPreferencesRepository provides a managed key-value store for user preferences that are declared by a schema. Unlike UserDataRepository, which allows storing arbitrary data under any key, the keys here are the ones the system already knows about.

    Each preference has a default that comes from the application configuration. Reads fall back to that default until the user stores a value of their own, so a preference always has a value even before anyone changes it.

    import { PlatformServiceName } from '@limetech/lime-web-components';

    const userPreferences = platform.get(
    PlatformServiceName.UserPreferencesRepository
    );

    const emailNotifications = userPreferences.get<boolean>('emailNotifications');

    await userPreferences.set('emailNotifications', !emailNotifications);
    import { PlatformServiceName } from '@limetech/lime-web-components';

    const userPreferences = platform.get(
    PlatformServiceName.UserPreferencesRepository
    );

    const formValues: Record<string, unknown> = {
    language: 'sv-SE',
    pageSize: 25,
    unknownSetting: 'ignored',
    };

    // Only save the values the schema knows about
    for (const key of Object.keys(userPreferences.getSchema())) {
    const value = formValues[key];

    if (value !== undefined) {
    await userPreferences.set(key, value);
    }
    }

    UserDataRepository for storing arbitrary data

    interface UserPreferencesRepository {
        get<T = any>(key?: string): T;
        getSchema(): Record<string, unknown>;
        set<T = any>(key: string, data?: T): Promise<void>;
    }
    Index

    Methods

    Methods

    • Get user preference values

      This method can retrieve either all preferences or a specific preference value. When called without arguments, it returns an object containing all preference values. When called with a key, it returns only that specific preference value.

      Values the user has not changed fall back to the default from the application configuration. The returned object is built fresh on every call, so changing it does not change the stored preferences. Use UserPreferencesRepository.set for that.

      Type Parameters

      • T = any

        The expected type of the preference value(s)

      Parameters

      • Optionalkey: string

        Optional key for a specific preference. If omitted, all preferences are returned

      Returns T

      If a key is supplied, the value stored under that key, or undefined when the key has neither a stored value nor a default. If no key is supplied, an object containing all preference values

      import { PlatformServiceName } from '@limetech/lime-web-components';

      const userPreferences = platform.get(
      PlatformServiceName.UserPreferencesRepository
      );

      const allPreferences = userPreferences.get();
      console.log('Language:', allPreferences.language);
      console.log('Timezone:', allPreferences.timezone);
      import { PlatformServiceName } from '@limetech/lime-web-components';

      const userPreferences = platform.get(
      PlatformServiceName.UserPreferencesRepository
      );

      const language = userPreferences.get<string>('language');
      const showHints = userPreferences.get<boolean>('showHints');
    • Get the JSON schema defining available user preferences

      Returns the schema that declares which preferences exist and what shape their values have. This is useful for building a preferences UI without hard-coding the list of keys.

      The schema is served by the backend and is not applied to the values passed to UserPreferencesRepository.set, which are stored as given. Default values are not part of the schema either, they come from the application configuration and are already applied by UserPreferencesRepository.get.

      Returns Record<string, unknown>

      An object representing the JSON schema for user preferences

      import { PlatformServiceName } from '@limetech/lime-web-components';

      const userPreferences = platform.get(
      PlatformServiceName.UserPreferencesRepository
      );

      // Build the form from the keys the schema declares
      for (const [key, value] of Object.entries(userPreferences.getSchema())) {
      // The schema is plain JSON, so a consumer has to state its shape
      const definition = value as { type: string };

      console.log(`Preference: ${key}`);
      console.log(`Type: ${definition.type}`);
      console.log('Current value:', userPreferences.get(key));
      }

      UserPreferencesRepository.get to read the current values

    • Set or clear a user preference value

      Saves a preference value to the server. The value must be JSON-serializable. To go back to the default, pass undefined as the data. The key is dropped when the preferences are sent to the server, so reads return undefined rather than the default until the request has completed.

      Type Parameters

      • T = any

        The type of the preference value

      Parameters

      • key: string

        The preference key to set

      • Optionaldata: T

        The value to save. Pass undefined to clear the stored value

      Returns Promise<void>

      A promise that resolves when the preference has been saved

      An error if the request to the server fails

      import { PlatformServiceName } from '@limetech/lime-web-components';

      const userPreferences = platform.get(
      PlatformServiceName.UserPreferencesRepository
      );

      await userPreferences.set('language', 'sv-SE');
      await userPreferences.set('pageSize', 25);
      await userPreferences.set('showAdvancedFeatures', true);
      import { PlatformServiceName } from '@limetech/lime-web-components';

      interface DashboardLayout {
      widgets: string[];
      columns: number;
      }

      const userPreferences = platform.get(
      PlatformServiceName.UserPreferencesRepository
      );

      const layout: DashboardLayout = {
      widgets: ['calendar', 'tasks', 'notifications'],
      columns: 3,
      };

      await userPreferences.set('dashboardLayout', layout);
      import { PlatformServiceName } from '@limetech/lime-web-components';

      const userPreferences = platform.get(
      PlatformServiceName.UserPreferencesRepository
      );

      // Clear the stored value so the configured default applies again
      await userPreferences.set('fontSize', undefined);

      The value must be serializable as JSON. Non-serializable data (like functions, circular references, etc.) may result in errors or data loss

      UserPreferencesRepository.get to read a preference