File

angular/projects/researchdatabox/portal-ng-common/src/lib/httpClient.service.ts

Description

Convenience base class for services that use HTTP client.

Notes:

Beware when using typed responses, as per: https://angular.io/guide/http#requesting-a-typed-response

To specify the response object type, first define an interface with the required properties. Use an interface rather than a class, because the response is a plain object that cannot be automatically converted to an instance of a class.

Author: Shilo Banihit

Implements

Service

Index

Properties
Methods

Constructor

constructor(http: HttpClient, rootContext: string, utilService: UtilityService, configService: ConfigService)
Parameters :
Name Type Optional
http HttpClient No
rootContext string No
utilService UtilityService No
configService ConfigService No

Properties

Public baseUrl
Type : string
Public baseUrlWithContext
Type : string
Public brandingAndPortalUrl
Type : string
Protected config
Type : any
Protected httpContext
Type : HttpContext
Default value : null as any
Protected initSubject
Type : any
Protected reqOptsJsonBodyOnly
Type : any
Default value : {responseType: 'json', observe: 'body'}
Public rootContext
Type : string
Decorators :
@Inject(APP_BASE_HREF)

Methods

Protected enableCsrfHeader
enableCsrfHeader()

Call from extending class to enable CSRF in the header

Returns : void
Public getConfig
getConfig()

Returns the config block

Returns : any
Public getInitSubject
getInitSubject()
Returns : Subject<any>
Public isInitializing
isInitializing()

Default checks if we've loaded the config. Extensions can add more conditions as needed.

Returns : boolean

true if service is ready

Public Async waitForInit
waitForInit()

Returns itself when all dependent services/components/data is/are available.

Returns : Promise<any>

itself

import { Subject } from 'rxjs';
import { Inject } from '@angular/core';
import { HttpClient, HttpContext } from '@angular/common/http';
import { APP_BASE_HREF } from '@angular/common';
import { isEmpty as _isEmpty, isUndefined as _isUndefined } from 'lodash-es';

import { Service } from './service.interface';
import { ConfigService } from './config.service';
import { UtilityService } from './utility.service';
import { RB_HTTP_INTERCEPTOR_AUTH_CSRF } from './csrf.interceptor';


/**
 * Convenience base class for services that use HTTP client.
 * 
 * Notes: 
 * 
 * Beware when using typed responses, as per: https://angular.io/guide/http#requesting-a-typed-response
 * 
 * ```To specify the response object type, first define an interface with the required properties. Use an interface rather than a class, because the response is a plain object that cannot be automatically converted to an instance of a class.```
 *
 *
 * Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
 */

export class HttpClientService implements Service {
  protected initSubject: any;
  protected config: any;
  public baseUrl:string;
  public brandingAndPortalUrl:string;
  public baseUrlWithContext: string;
  protected httpContext: HttpContext = null as any;
  // define some predefined request options here for extensions can use,clone,modify,etc.
  protected reqOptsJsonBodyOnly:any = {responseType: 'json', observe: 'body'};

  constructor( 
  @Inject(HttpClient) protected http: HttpClient, 
  @Inject(APP_BASE_HREF) public rootContext: string,
  @Inject(UtilityService) protected utilService: UtilityService,
  @Inject(ConfigService) protected configService: ConfigService
  ) {
    this.initSubject = new Subject();
    if (_isEmpty(this.rootContext)) {
      this.rootContext = ""
    }
    this.baseUrl = "";
    this.brandingAndPortalUrl = "";
    this.baseUrlWithContext = "";
  }

  /**
   * Returns the config block 
   * 
   * @returns 
   */
  public getConfig() {
    return this.config;
  }

  public getInitSubject(): Subject<any> {
    return this.initSubject;
  }

  /**
   * Returns itself when all dependent services/components/data is/are available.
   * 
   * @returns itself
   */
  public async waitForInit(): Promise<any> {
    if (!this.isInitializing()) {
      return this;
    }
    await this.utilService.waitForDependencies([this.configService]);
    this.config = await this.configService.getConfig();
    this.baseUrl = this.config.baseUrl;
    this.brandingAndPortalUrl = `${this.baseUrl}${this.rootContext}/${this.config.branding}/${this.config.portal}`;
    this.baseUrlWithContext = `${this.baseUrl}${this.rootContext}`;
    this.httpContext = new HttpContext();
    return this;
  }  
  /**
   * Default checks if we've loaded the config. Extensions can add more conditions as needed.
   * 
   * @returns true if service is ready
   */
  public isInitializing(): boolean {
    return _isUndefined(this.config) || _isEmpty(this.config);
  }
  /**
   * Call from extending class to enable CSRF in the header
   */
  protected enableCsrfHeader() {
    this.httpContext.set(RB_HTTP_INTERCEPTOR_AUTH_CSRF, this.config.csrfToken);
  }
}

results matching ""

    No results matching ""