import { Injectable, Injector, Inject } from '@angular/core'; import { environment } from '../environments/environment'; import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http'; import { Observable, throwError, catchError, map, tap, lastValueFrom } from 'rxjs'; import { ActivatedRoute, Router } from '@angular/router'; import { Location } from "@angular/common"; import { IEnvironmentsInfo } from '../Models/environmentsModel.info'; @Injectable({ providedIn: 'root' }) export class BaseMenuService { constructor(@Inject('config') private config: IEnvironmentsInfo, private http: HttpClient, private location: Location, private route: Router ) { try { this.serviceUrl = this.config?.CBMSServiceUrl || environment.CBMSServiceUrl; } catch (error) { // Fallback to environment if config is not available this.serviceUrl = environment.CBMSServiceUrl; } } get options(): IEnvironmentsInfo { return this.config || environment as any; } private serviceUrl: string; private formatErrors(error: any) { return throwError(error.error); } get(path: string, body: Object = {}, params: HttpParams = new HttpParams()): Observable { return this.http.get( this.serviceUrl + path, body ).pipe(catchError(this.formatErrors)); } put(path: string, body: Object = {}): Observable { return this.http.put( this.serviceUrl + path, JSON.stringify(body) ).pipe(catchError(this.formatErrors)); } post(path: string, body: Object = {}): Observable { return this.http.post( this.serviceUrl + path, body).pipe(catchError(this.formatErrors)); } postExcel(path: string, body: Object = {}): Observable { return this.http.post( this.serviceUrl + path, body, { responseType: 'blob' as 'json' }).pipe(catchError(this.formatErrors)); } contractPost(path: string, body: Object = {}): Observable { return this.http.post( this.serviceUrl + path, body, { responseType: 'text' } ).pipe(catchError(this.formatErrors)); } delete(path: string, body: Object = {}): Observable { return this.http.delete( this.serviceUrl + path, body ).pipe(catchError(this.formatErrors)); } patch(path: string, body: Object = {}): Observable { return this.http.patch( this.serviceUrl + path, body, { responseType: 'text' } ).pipe(catchError(this.formatErrors)); } async postasync(path: string, body: Object = {}) { let loading = false; const res$ = await this.http .post(this.serviceUrl + path, body) .pipe( map(response => response), tap(() => loading = false)); const res = await lastValueFrom(res$); return res; } }