diff --git a/projects/nshmp-ws/src/app/swagger/app.component.ts b/projects/nshmp-ws/src/app/swagger/app.component.ts index b72bdc11b8b2a2df9a40f3371aa2ab0370b98d92..58009820d80758807989d6bcf1341095a1e9f6dd 100644 --- a/projects/nshmp-ws/src/app/swagger/app.component.ts +++ b/projects/nshmp-ws/src/app/swagger/app.component.ts @@ -1,3 +1,4 @@ +import {HttpClient} from '@angular/common/http'; import { Component, ElementRef, @@ -11,8 +12,10 @@ import { NshmpLibNgAlertComponent, NshmpLibNgSpinnerComponent, NshmpLibNgTemplateComponent, + NshmpService, SpinnerService, } from '@ghsc/nshmp-lib-ng/nshmp'; +import {catchError} from 'rxjs'; import {Spec, SwaggerUIBundle} from 'swagger-ui-dist'; import {NavigationService} from '../navigation.service'; @@ -37,6 +40,8 @@ export class AppComponent implements OnInit { constructor( public navigationService: NavigationService, private spinnerService: SpinnerService, + private httpClient: HttpClient, + private nshmpService: NshmpService, ) {} ngOnInit(): void { @@ -45,23 +50,36 @@ export class AppComponent implements OnInit { const contextPath = location.pathname; const url = `${location.href}/swagger`; - SwaggerUIBundle({ - deepLinking: false, - defaultModelsExpandDepth: 0, - domNode: this.swagger().nativeElement, - layout: 'BaseLayout', - plugins: [ - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - SwaggerUIBundle?.plugins?.DownloadUrl, - this.updateContextPath(contextPath), - ], - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - presets: [SwaggerUIBundle?.presets?.apis], - tagsSorter: 'alpha', - tryItOutEnabled: true, - url, - validatorUrl: null, - }); + // Check swagger endpoint first + this.httpClient + .get(url) + .pipe( + catchError((error: Error) => { + this.spinnerRef.close(); + return this.nshmpService.throwError$( + new Error(`Failed to get swagger endpoint. ${error.message}`), + ); + }), + ) + .subscribe(() => { + SwaggerUIBundle({ + deepLinking: false, + defaultModelsExpandDepth: 0, + domNode: this.swagger().nativeElement, + layout: 'BaseLayout', + plugins: [ + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + SwaggerUIBundle?.plugins?.DownloadUrl, + this.updateContextPath(contextPath), + ], + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + presets: [SwaggerUIBundle?.presets?.apis], + tagsSorter: 'alpha', + tryItOutEnabled: true, + url, + validatorUrl: null, + }); + }); } updateContextPath(contextPath: string) {