Skip to content

Commit

Permalink
Add types to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chiamakaikeanyi committed Jun 20, 2023
1 parent 5bf272b commit e8a50b7
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/services/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type ApiClientType = {
};
};

type ApiResponse<T> = Promise<T>;

export const apiClient = async ({
path,
options = {
Expand All @@ -21,7 +23,7 @@ export const apiClient = async ({
"cca3,flags,name,population,region,subregion,capital,tld,currencies,languages,car,timezones,borders",
},
},
}: ApiClientType) => {
}: ApiClientType): ApiResponse<any> => {
return await axios({
url: `${config.apiUrl}${path}`,
method: options?.method,
Expand All @@ -35,17 +37,19 @@ export const apiClient = async ({
.catch((error) => console.error(`Error retrieving data: ${error}`));
};

export const getCountries = () => {
export const getCountries = (): ApiResponse<ICountry[]> => {
return apiClient({
path: "/all",
});
};

export const getCountryByCode = (code: string) => {
export const getCountryByCode = (code: string): ApiResponse<ICountry> => {
return apiClient({ path: `/alpha/${code}` });
};

export const getBorderCountriesByCodes = async (codes: string[]) => {
export const getBorderCountriesByCodes = async (
codes: string[]
): ApiResponse<ICountry[]> => {
const response = await getCountries();

return response?.filter((country: ICountry) => codes.includes(country.cca3));
Expand Down

0 comments on commit e8a50b7

Please sign in to comment.