File

libs/core/src/lib/services/building-block-meta.service.ts

Description

Service to access any given meta data for a Building Block page injected by BUILDING_BLOCK_META_DATA during the generation of a page. The service is used by Building Block Pages and needs access to the page-local BUILDING_BLOCK_META_DATA, hence we chose the any strategy to always create a local service instance. Working alternative would be injecting it directly into each of the page modules but this lazy approach supports tree shaking way better.

Index

Methods
Accessors

Constructor

constructor(metaData: BuildingBlockPageMetaData)
Parameters :
Name Type Optional
metaData BuildingBlockPageMetaData No

Methods

assertBuildingBlockExistence
assertBuildingBlockExistence(id: string)
Parameters :
Name Type Optional
id string No
Returns : void
getBusinessEvents
getBusinessEvents(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
getData
getData(buildingBlockId: string, key)
Parameters :
Name Type Optional
buildingBlockId string No
key No
Returns : Record<string, BuildingBlockMetaData>
getNavigationData
getNavigationData(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
getResources
getResources(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
Returns : Record<string, BuildingBlockMetaData>
hasData
hasData(buildingBlockId: string, key)
Parameters :
Name Type Optional
buildingBlockId string No
key No
Returns : boolean
hasMetaData
hasMetaData(id: string)
Parameters :
Name Type Optional
id string No
Returns : any
hasResources
hasResources(id: string)
Parameters :
Name Type Optional
id string No
Returns : boolean

Accessors

metaData
getmetaData()
import { Inject, Injectable, Optional } from '@angular/core';
import { BUILDING_BLOCK_NAVIGATION_TYPE } from '../building-block/navigation';
import { type ValidationConfigItem } from '../form-support/validation.model';
import { BUILDING_BLOCK_META_DATA } from '../tokens';
import { type BusinessEventConfig } from './taly-business-event.model';

export type BuildingBlockPageMetaData = Record<string, BuildingBlockMetaData>;

export interface BuildingBlockMetaData {
  resources: unknown;
  businessEvents?: Record<string, string>;
  navigation?: Record<string, string>;
  validators?: ValidationConfigItem[];
}

/**
 * Service to access any given meta data for a Building Block page
 * injected by BUILDING_BLOCK_META_DATA during the generation of a page.
 * The service is used by Building Block Pages and needs access to the page-local
 * BUILDING_BLOCK_META_DATA, hence we chose the any strategy to always create a local service instance.
 * Working alternative would be injecting it directly into each of the page modules but this lazy approach
 * supports tree shaking way better.
 */
@Injectable({
  providedIn: 'any'
})
export class BuildingBlockMetaService {
  private _metaData: BuildingBlockPageMetaData = {};

  constructor(
    @Optional()
    @Inject(BUILDING_BLOCK_META_DATA)
    metaData: BuildingBlockPageMetaData
  ) {
    this._metaData = metaData;
  }

  get metaData() {
    return this._metaData;
  }

  getData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
    this.assertBuildingBlockExistence(buildingBlockId);
    return this.metaData[buildingBlockId]?.[key];
  }

  hasData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
    return this.hasMetaData(buildingBlockId) && !!this.metaData[buildingBlockId][key];
  }

  getResources(buildingBlockId: string) {
    return this.getData(buildingBlockId, 'resources');
  }

  getBusinessEvents(buildingBlockId: string) {
    return this.getData(buildingBlockId, 'businessEvents') as Record<string, BusinessEventConfig>;
  }

  getNavigationData(buildingBlockId: string) {
    return (this.getData(buildingBlockId, 'navigation') ?? {}) as Record<
      string,
      BUILDING_BLOCK_NAVIGATION_TYPE
    >;
  }

  hasResources(id: string) {
    return this.hasMetaData(id) && !!this.metaData[id].resources;
  }

  hasMetaData(id: string) {
    return Object.keys(this.metaData).includes(id);
  }

  assertBuildingBlockExistence(id: string) {
    if (!this.hasMetaData(id)) {
      throw new Error(`Could not find key ${id} in metadata list`);
    }
  }
}

results matching ""

    No results matching ""