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.
Methods
|
getBusinessEvents
|
getBusinessEvents(buildingBlockId: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| buildingBlockId |
string
|
No
|
|
|
getData
|
getData(buildingBlockId: string, key: keyof BuildingBlockMetaData)
|
|
|
Parameters :
| Name |
Type |
Optional |
| buildingBlockId |
string
|
No
|
| key |
keyof BuildingBlockMetaData
|
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: keyof BuildingBlockMetaData)
|
|
|
Parameters :
| Name |
Type |
Optional |
| buildingBlockId |
string
|
No
|
| key |
keyof BuildingBlockMetaData
|
No
|
|
|
hasMetaData
|
hasMetaData(id: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| id |
string
|
No
|
|
|
hasResources
|
hasResources(id: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| id |
string
|
No
|
|
import {
type ValidationConfigItem,
BUILDING_BLOCK_META_DATA,
BusinessEventConfig
} from '@allianz/taly-core';
import { Injectable, inject } from '@angular/core';
import { BUILDING_BLOCK_NAVIGATION_TYPE } from '../building-block/navigation';
import { BuildingBlockMetaServiceInterface } from './building-block-meta-service-interface';
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 implements BuildingBlockMetaServiceInterface {
private _metaData: BuildingBlockPageMetaData;
constructor() {
const metaData = inject<BuildingBlockPageMetaData>(BUILDING_BLOCK_META_DATA, {
optional: true
});
this._metaData = metaData ?? {};
}
get metaData() {
return this._metaData;
}
getData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
if (!this.hasMetaData(buildingBlockId)) {
throw new Error(`Could not find key ${buildingBlockId} in metadata list`);
}
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);
}
}