2020-07-09 11:45:24 +00:00
|
|
|
import {Inject, Injectable, LOCALE_ID} from '@angular/core';
|
2020-06-22 11:12:02 +00:00
|
|
|
import {HttpClient} from '@angular/common/http';
|
|
|
|
import {AppConfig} from '../shared/app.config';
|
2020-07-09 11:45:24 +00:00
|
|
|
import {Observable, of} from 'rxjs';
|
|
|
|
import {catchError, switchMap} from 'rxjs/operators';
|
2020-06-22 11:12:02 +00:00
|
|
|
|
2020-06-22 11:37:38 +00:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root',
|
|
|
|
})
|
2020-06-22 11:12:02 +00:00
|
|
|
export class SchemaService {
|
2020-07-09 11:45:24 +00:00
|
|
|
constructor(private httpClient: HttpClient, private appConfig: AppConfig,
|
|
|
|
@Inject(LOCALE_ID) private locale) {
|
2020-06-22 11:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ApiEndpoint() {
|
|
|
|
return this.appConfig.getConfig('apiEndPoint');
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:52:05 +00:00
|
|
|
/** Takes decoded schema url id */
|
2020-06-30 14:30:33 +00:00
|
|
|
public getSchema(id): Observable<any> {
|
2020-06-24 09:52:05 +00:00
|
|
|
const encodedId = encodeURIComponent(id);
|
|
|
|
return this.httpClient.get<any>(`${this.ApiEndpoint()}/api/v1/schema/${encodedId}`);
|
2020-06-22 11:12:02 +00:00
|
|
|
}
|
2020-07-09 11:45:24 +00:00
|
|
|
|
|
|
|
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(
|
|
|
|
schemaJson => this.getSchemaLayout(schemaUrl, locale)
|
|
|
|
.pipe(catchError(err => {
|
|
|
|
return of(undefined);
|
|
|
|
})),
|
|
|
|
(schemaJson, schemaLayout) => {
|
|
|
|
return ({schemaJson, schemaLayout});
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
2020-06-22 11:12:02 +00:00
|
|
|
}
|