forked from easyappscompany/ROOMIEONE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiService.js
47 lines (44 loc) · 1.26 KB
/
apiService.js
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
46
47
const API_KEY = 'eVBSWXBNcVhlelVsRDBPR1puemxOV3FVS1JFUzV5RjRrM1RtcmJ6aw==';
const BASE_URL = 'https://api.countrystatecity.in/v1';
export const getCountries = async () => {
try {
const response = await fetch(`${BASE_URL}/countries`, {
headers: {
'X-CSCAPI-KEY': API_KEY,
},
});
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching countries:', error);
throw error;
}
};
export const getStates = async (countryIso2) => {
try {
const response = await fetch(`${BASE_URL}/countries/${countryIso2}/states`, {
headers: {
'X-CSCAPI-KEY': API_KEY,
},
});
const data = await response.json();
return data;
} catch (error) {
console.error(`Error fetching states for country ${countryIso2}:`, error);
throw error;
}
};
export const getCities = async (countryIso2, stateIso2) => {
try {
const response = await fetch(`${BASE_URL}/countries/${countryIso2}/states/${stateIso2}/cities`, {
headers: {
'X-CSCAPI-KEY': API_KEY,
},
});
const data = await response.json();
return data;
} catch (error) {
console.error(`Error fetching cities for state ${stateIso2} in country ${countryIso2}:`, error);
throw error;
}
};