-
Notifications
You must be signed in to change notification settings - Fork 913
Expand file tree
/
Copy pathCountryService.ts
More file actions
42 lines (38 loc) · 1.34 KB
/
CountryService.ts
File metadata and controls
42 lines (38 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Service } from 'typedi';
import { Logger, LoggerInterface } from '../../decorators/Logger';
import axios from 'axios';
import { ICountries } from '../types/Country';
import { env } from '../../env';
@Service()
export class CountryService {
constructor(
@Logger(__filename) private log: LoggerInterface
) { }
public async countryList(): Promise<ICountries[]> {
this.log.info('Get Country List');
const url = env.external.countryDataUrl;
const headers = {
'Content-Type': 'application/json',
};
const countriesList: ICountries[] = [];
try {
const response = await axios({
method: 'GET',
url: url,
headers: headers,
});
if (response.status== 200) {
response.data.forEach((country: { name: any; currencies: any }) => {
countriesList.push({
name: country.name.common,
currencies: country.currencies,
});
});
this.log.info(`countriesList: ${JSON.stringify(countriesList)}`);
}
} catch (err) {
this.log.error(`exception caught while getting countriesList : ${JSON.stringify(err)}`);
}
return countriesList;
}
}