FarmMapsLib/projects/common/src/fm/services/schema.service.ts

42 lines
1.4 KiB
TypeScript

import {Inject, Injectable, LOCALE_ID} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {AppConfig} from '../shared/app.config';
import {Observable, of} from 'rxjs';
import {catchError, switchMap} from 'rxjs/operators';
@Injectable({
providedIn: 'root',
})
export class SchemaService {
constructor(private httpClient: HttpClient, private appConfig: AppConfig,
@Inject(LOCALE_ID) private locale) {
}
ApiEndpoint() {
return this.appConfig.getConfig('apiEndPoint');
}
/** Takes decoded schema url id */
public getSchema(id): Observable<any> {
const encodedId = encodeURIComponent(id);
return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/schema/${encodedId}`);
}
public getSchemaLayout(schemaUrl: string, locale: string): Observable<any> {
const encodedId = encodeURIComponent(schemaUrl);
return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/schema/${encodedId}/layout/${locale}`);
}
public getSchemaAndLayout(schemaUrl: string, locale: string = this.locale):
Observable<{schemaJson: any, schemaLayout: any}> {
return this.getSchema(schemaUrl).pipe(
switchMap(
() => this.getSchemaLayout(schemaUrl, locale)
.pipe(catchError(() => of(undefined))),
(schemaJson, schemaLayout) => {
return ({schemaJson, schemaLayout});
}
));
}
}