-
Notifications
You must be signed in to change notification settings - Fork 915
/
Copy pathCountry.ts
45 lines (36 loc) · 1.19 KB
/
Country.ts
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
43
44
45
import { Authorized, Get, JsonController } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
export class Country {
public name: string;
public currency: string;
}
@ResponseSchema(Country, { isArray: true })
export class CountryResponse {
public countries: Country[];
}
@Authorized()
@JsonController('/countries')
@OpenAPI({ security: [{ basicAuth: [] }] })
export class PetController {
@Get()
@ResponseSchema(CountryResponse, { isArray: true })
public async getCountries(): Promise<CountryResponse> {
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) => {
return {
name: country.name.official,
currency: country.currencies,
};
});
} catch (error) {
console.error('Error fetching countries:', error);
throw error;
}
}
}