import { Injectable } from '@angular/core'; import { HttpHeaders } from '@angular/common/http'; import { switchMap, take } from 'rxjs/operators'; import { Observable } from 'rxjs'; import { isArray, mergeWith } from 'lodash-es'; import { MwHttpErrorHandlerOptions } from './http-error-handler.service'; import { RequestOptions } from '../models'; import { MwHttpBaseService } from './http-base.service'; import { MwCoreDateTimeService } from 'projects/core/src//services/date-time.service'; import { AggHttpService } from './agg-http.service'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Injectable() export class MwHttpService extends MwHttpBaseService { constructor( public override aggService: AggHttpService, public oidcSecurityService: OidcSecurityService, public dateService: MwCoreDateTimeService ) { super(aggService); } rawAuthRequest( method: string, url: string, options?: RequestOptions ): Observable { return this.oidcSecurityService.getAccessToken().pipe( take(1), switchMap((accessToken) => { const errorHandlerOptions = this.getErrorHandlerOptions(options); const headers = this.getHeaders( accessToken, method, options?.companyId ); const httpOptions = { headers, body: options && options.body, responseType: options && options.responseType, }; return this.rawRequest( method, url, httpOptions, errorHandlerOptions ); }) ); } authGet(url: string, options?: RequestOptions): Observable { return this.rawAuthRequest('GET', url, options); } authPost(url: string, options?: RequestOptions): Observable { return this.rawAuthRequest('POST', url, options); } authPut(url: string, options?: RequestOptions): Observable { return this.rawAuthRequest('PUT', url, options); } authPatch(url: string, options?: RequestOptions): Observable { return this.rawAuthRequest('PATCH', url, options); } authDelete(url: string, options?: RequestOptions): Observable { return this.rawAuthRequest('DELETE', url, options); } protected getHeaderItems( accessToken: string, companyId?: number ): { Authorization: string; 'mw-language': string; tz: string; } { let headers: { Authorization: string; ['mw-language']: string; tz: string; } = { Authorization: 'Bearer ' + accessToken, 'mw-language': this.aggService.translateService.currentLang, tz: this.dateService.getLocalOlsonTimeZone(), }; if (accessToken) { headers.Authorization = `Bearer ${accessToken}`; } if (this.httpConfig && this.httpConfig.customHeaders) { this.httpConfig.customHeaders.forEach((t) => { t.value.pipe(take(1)).subscribe((rawValue) => Object.assign(headers, { [t.name]: String(rawValue), }) ); }); } // If companyId is specified, override mw-company-id to make query to a different company than the logged in company. // Example, setup integration on a different Company the user has access to. if (companyId) { headers = Object.assign({}, headers, { ['mw-company-id']: String(companyId), }); } return headers; } private getHeaders( accessToken: string, method: string, companyId?: number ): HttpHeaders { let headerItems = this.getHeaderItems(accessToken, companyId); headerItems = Object.assign({}, headerItems, { Accept: 'application/json', }); if (method !== 'GET') { headerItems = Object.assign({}, headerItems, { ['Content-Type']: 'application/json', }); } return new HttpHeaders(headerItems); } private getErrorHandlerOptions( requestOptions?: RequestOptions, additionalRequestOptions?: RequestOptions ): MwHttpErrorHandlerOptions { let errorHandlerOptions: MwHttpErrorHandlerOptions = { interpolateParams: requestOptions?.interpolateParamsForError || [], ignoreNotifyErrorCodes: requestOptions?.ignoreNotifyErrorCodes || [], ignoreNotifyStatusCodes: requestOptions?.ignoreNotifyStatusCodes || [], }; if (additionalRequestOptions) { errorHandlerOptions = mergeWith( errorHandlerOptions, additionalRequestOptions, (objValue: any, srcValue: any) => { if (isArray(objValue) && isArray(srcValue)) { return objValue.concat(srcValue); } return objValue; } ); } return errorHandlerOptions; } }