File

src/country-picker.service.ts

Index

Properties
Methods

Constructor

constructor(config: CountryPickerConfig, _http: HttpClient)
Parameters :
Name Type Optional
config CountryPickerConfig No
_http HttpClient No

Methods

Private _loadData
_loadData()
Public getBaseUrl
getBaseUrl()
Returns : string
Public getCountries
getCountries()
Protected Static handleError
handleError(error: HttpResponse | any)
Parameters :
Name Type Optional
error HttpResponse<any> | any No
Returns : Observable<never>

Properties

Private Readonly _baseUrl
Type : string
Private _data
Type : Observable<ICountry[]> | null
Default value : null
Private Readonly _filename
Type : string
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

import { COUNTRY_PICKER_CONFIG, CountryPickerConfig } from './country-picker.config';
import { ICountry } from './country.interface';

@Injectable()
export class CountryPickerService {

  private readonly _baseUrl: string;
  private readonly _filename: string;
  private _data: Observable<ICountry[]> | null = null;

  /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
  protected static handleError(error: HttpResponse<any> | any): Observable<never> {
    let errMsg: string;
    if (error instanceof HttpResponse) {
      if (error.status === 404) {
        errMsg = 'Error loading countries.json file.'
          + ' Please configure WebPack and load countries.json assets to your root folder';
      } else {
        const body = error.body || '';
        const err = body.error || JSON.stringify(body);
        errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
      }
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return throwError(() => new Error(errMsg));
  }

  constructor(
    @Inject(COUNTRY_PICKER_CONFIG) config: CountryPickerConfig,
    private _http: HttpClient
  ) {
    this._baseUrl = config.baseUrl;
    this._filename = config.filename;
    this._data = this._loadData();
  }

  public getCountries(): Observable<ICountry[]> {
    return this._data
      .pipe(
        map((countries: ICountry[]) =>
          countries.map((country: ICountry) => {
            country.name.native[0] = country.name.native[Object.keys(country.name.native)[0]];
            return country;
          })
        )
      );
  }

  public getBaseUrl(): string {
    return this._baseUrl;
  }

  private _loadData(): Observable<ICountry[]> {
    return this._http.get<ICountry[]>(this._baseUrl + this._filename)
      .pipe(
        catchError(CountryPickerService.handleError),
      );
  }
}

results matching ""

    No results matching ""