src/country-picker.service.ts
Properties |
Methods |
|
constructor(config: CountryPickerConfig, _http: HttpClient)
|
|||||||||
Defined in src/country-picker.service.ts:33
|
|||||||||
Parameters :
|
Private _loadData |
_loadData()
|
Defined in src/country-picker.service.ts:60
|
Returns :
Observable<ICountry[]>
|
Public getBaseUrl |
getBaseUrl()
|
Defined in src/country-picker.service.ts:56
|
Returns :
string
|
Public getCountries |
getCountries()
|
Defined in src/country-picker.service.ts:44
|
Returns :
Observable<ICountry[]>
|
Protected Static handleError | ||||||
handleError(error: HttpResponse
|
||||||
Defined in src/country-picker.service.ts:17
|
||||||
Parameters :
Returns :
Observable<never>
|
Private Readonly _baseUrl |
Type : string
|
Defined in src/country-picker.service.ts:12
|
Private _data |
Type : Observable<ICountry[]> | null
|
Default value : null
|
Defined in src/country-picker.service.ts:14
|
Private Readonly _filename |
Type : string
|
Defined in src/country-picker.service.ts:13
|
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),
);
}
}