Files
marketplaces/src/app/widgets/registry/widget-manifest.service.ts

34 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, catchError, map, of, shareReplay } from 'rxjs';
import { WidgetManifestEntry, WidgetManifestFile } from '../contracts/widget-manifest.contract';
@Injectable({ providedIn: 'root' })
export class WidgetManifestService {
private readonly manifestUrl = '/assets/mock/bootstrap/widget-manifest.json';
private manifest$?: Observable<WidgetManifestFile>;
constructor(private readonly http: HttpClient) {}
getManifest(): Observable<WidgetManifestFile> {
if (!this.manifest$) {
this.manifest$ = this.http.get<WidgetManifestFile>(this.manifestUrl).pipe(
shareReplay({ bufferSize: 1, refCount: true }),
catchError(() => {
this.manifest$ = undefined;
return of({ widgets: [] });
})
);
}
return this.manifest$;
}
getWidgets(): Observable<WidgetManifestEntry[]> {
return this.getManifest().pipe(map((manifest) => manifest.widgets ?? []));
}
getWidget(type: string): Observable<WidgetManifestEntry | undefined> {
return this.getWidgets().pipe(map((widgets) => widgets.find((widget) => widget.type === type)));
}
}