-
Notifications
You must be signed in to change notification settings - Fork 913
Expand file tree
/
Copy pathCountry.ts
More file actions
41 lines (36 loc) · 1.24 KB
/
Country.ts
File metadata and controls
41 lines (36 loc) · 1.24 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
import {Get, JsonController} from 'routing-controllers';
import {ResponseSchema} from 'routing-controllers-openapi';
export class Country {
public name: string;
public currency: string;
}
@ResponseSchema(Country, { isArray: true })
class CountryListResponse {
public countries: Country[];
}
@JsonController('/countries')
export class CountryController {
@Get()
@ResponseSchema(CountryListResponse)
public async getCountries(): Promise<CountryListResponse> {
const countries: Country[] = await this.fetchCountries();
return {countries};
}
private async fetchCountries(): Promise<Country[]> {
try {
const response = await fetch('https://restcountries.com/v3.1/all');
const data = await response.json();
return data.map((country: any) => {
const currencyCode = Object.keys(country.currencies)[0];
const currency = `${country.currencies[currencyCode].name} (${currencyCode})`;
return {
name: country.name.official,
currency,
};
});
} catch (error) {
console.error('Error fetching countries:', error);
throw error;
}
}
}