File

assets/angular/shared/base-service.ts

Description

Base class for all client-side services...

Author: Shilo Banihit

Index

Properties
Methods
Accessors

Constructor

constructor(http: any, configService: any)
Parameters :
Name Type Optional Description
http any
configService any

Methods

Protected emitInit
emitInit()
Returns : void
Protected extractData
extractData(res: Response, parentField: any)
Parameters :
Name Type Optional Description
res Response
parentField any
Returns : any
getConfig
getConfig()
Returns : any
Protected getOptions
getOptions(headersObj: any)
Parameters :
Name Type Optional Description
headersObj any
Returns : any
Protected getOptionsClient
getOptionsClient(headersObj: any)
Parameters :
Name Type Optional Description
headersObj any
Returns : any
Public waitForInit
waitForInit(handler: any)
Parameters :
Name Type Optional Description
handler any
Returns : any

Properties

Protected __config
__config: any
Type : any
Protected baseUrl
baseUrl: string
Type : string
Protected brandingAndPortalUrl
brandingAndPortalUrl: string
Type : string
Protected config
config: any
Type : any
Protected configService
configService: any
Type : any
Protected http
http: any
Type : any
Protected initSubject
initSubject: any
Type : any
Protected options
options: any
Type : any

Accessors

getBrandingAndPortalUrl
getgetBrandingAndPortalUrl()
getBaseUrl
getgetBaseUrl()
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/mergeMap';
import * as _ from "lodash-lib";
import { Subject } from 'rxjs/Subject';
/**
 * Base class for all client-side services...
 *
 * Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
 *
 */
export class BaseService {
  protected http: any;
  protected config: any;
  protected configService: any;
  protected baseUrl:string;
  protected brandingAndPortalUrl:string;
  protected options: any;
  protected static __config: any;
  protected initSubject: any;

  constructor (http: any , configService: any) {
    this.http = http;
    this.configService = configService;
    this.initSubject = new Subject();
    this.configService.getConfig((config:any) => {
      this.config = config;
      this.baseUrl = this.config.baseUrl;
      this.brandingAndPortalUrl = `${this.baseUrl}/${this.config.branding}/${this.config.portal}`;
      this.options = this.getOptionsClient();
      this.emitInit();
    });
  }

  public get getBrandingAndPortalUrl() {
    return this.brandingAndPortalUrl;
  }

  public get getBaseUrl() {
    return this.baseUrl;
  }

  public waitForInit(handler: any) {
    const subs = this.initSubject.subscribe(handler);
    this.emitInit();
    return subs;
  }

  protected emitInit() {
    if (this.config) {
      this.initSubject.next('');
    }
  }

  getConfig() {
    return this.config;
  }

  protected extractData(res: Response, parentField: any = null) {
    let body = res.json();
    if (parentField) {
        return body[parentField] || {};
    } else {
        return body || {};
    }
  }

  protected getOptions(headersObj: any) {
    let headers = new Headers(headersObj);
    return new RequestOptions({ headers: headers });
  }

  protected getOptionsClient(headersObj: any = {}) {
    headersObj['X-Source'] = 'jsclient';
    headersObj['Content-Type'] = 'application/json;charset=utf-8';
    return this.getOptions(headersObj);
  }
}

results matching ""

    No results matching ""